Java 是否根据手机的方向旋转标记?
我在谷歌地图视图上的我的位置显示一个箭头(标记),我想根据手机的方向旋转箭头 当它旋转时,我认为它应该从它所处的位置平稳移动!例如,我将手机保持在N,我将有0度,因此箭头将在北方。当我将手机向右移动30度时,手机应该在它们之间平稳移动 a) 我用磁强计和加速度计的传感器值计算了方位角, 这个角度表示设备罗盘和磁N的当前方向,我试图用它来旋转表示我位置的标记,但没有成功。。。 我第一次计算天使:Java 是否根据手机的方向旋转标记?,java,android,android-studio,google-maps,android-animation,Java,Android,Android Studio,Google Maps,Android Animation,我在谷歌地图视图上的我的位置显示一个箭头(标记),我想根据手机的方向旋转箭头 当它旋转时,我认为它应该从它所处的位置平稳移动!例如,我将手机保持在N,我将有0度,因此箭头将在北方。当我将手机向右移动30度时,手机应该在它们之间平稳移动 a) 我用磁强计和加速度计的传感器值计算了方位角, 这个角度表示设备罗盘和磁N的当前方向,我试图用它来旋转表示我位置的标记,但没有成功。。。 我第一次计算天使: private SensorManager mSensorManager; priva
private SensorManager mSensorManager;
private final float[] accelerometerReading = new float[3];
private final float[] magnetometerReading = new float[3];
private final float[] rotationMatrix = new float[9];
private final float[] orientationAngles = new float[3];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_maps);
mSensorManager=(SensorManager)this.getSystemService(Context.SENSOR_SERVICE);
}
在onSensorChanged方法中,我们读取了哪些值我们从传感器中获取:
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if(sensorEvent.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
System.arraycopy(sensorEvent.values, 0, accelerometerReading,
0, accelerometerReading.length);
} else if (sensorEvent.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
System.arraycopy(sensorEvent.values, 0, magnetometerReading,
0, magnetometerReading.length);
}
updateOrientationAngles();
}
方法updateOrientationAngles();包含:
float azimuthInDegress;
private void updateOrientationAngles() {
// Update rotation matrix, which is needed to update orientation angles.
// "rotationMatrix" now has up-to-date information.
SensorManager.getRotationMatrix(rotationMatrix, null,
accelerometerReading, magnetometerReading);
// "orientationAngles" now has up-to-date information.
//contine cele 3 unghiuri
SensorManager.getOrientation(rotationMatrix, orientationAngles);
azimuthInDegress = (float) (Math.toDegrees(orientationAngles[0])+360)%360;
Log.i("Messages","orientationAngles:"+orientationAngles[0]);
}
方位角后退包含我认为我们感兴趣的使标记移动的值;
在onLocationonLocationChanged中,我将-->>
而PuttoHmapmarkerUser(coord)的方法是:
正如我在标题中所说,我想根据手机的方向进行箭头旋转,我想我已经计算了旋转角度,但是我没有使标记动画平滑旋转
你有什么想法吗?我期待您的回答:)此代码片段演示了使用渐进旋转将标记旋转到目标标题。出于演示目的,此代码使用贴图单击侦听器添加标记并开始旋转(演示中添加了3个标记):
final float tgtHdg=135.0F;//停止旋转的轴承
mMap.setOnMapClickListener(新的GoogleMap.OnMapClickListener(){
@凌驾
公共空位点击(LatLng pos){
//添加标记
MarkerOptions mo=新MarkerOptions().位置(pos);
最终标记m=mMap.addMarker(mo);
m、 setTag(新浮点数(0.0));
最终处理程序=新处理程序();
handler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
float hdg=(float)m.getTag();
m、 设置旋转(hdg);
//将每次更新的旋转量(度)更改为1.0。
航向+=1.0f;
m、 setTag(新浮动(hdg));
如果(航向
和结果
此代码片段演示如何使用渐进旋转将标记旋转到目标标题。出于演示目的,此代码使用贴图单击侦听器添加标记并开始旋转(演示中添加了3个标记):
final float tgtHdg=135.0F;//停止旋转的轴承
mMap.setOnMapClickListener(新的GoogleMap.OnMapClickListener(){
@凌驾
公共空位点击(LatLng pos){
//添加标记
MarkerOptions mo=新MarkerOptions().位置(pos);
最终标记m=mMap.addMarker(mo);
m、 setTag(新浮点数(0.0));
最终处理程序=新处理程序();
handler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
float hdg=(float)m.getTag();
m、 设置旋转(hdg);
//将每次更新的旋转量(度)更改为1.0。
航向+=1.0f;
m、 setTag(新浮动(hdg));
如果(航向
和结果
谢谢你的回答,我试试看。谢谢你的回答,我试试看。
locationListener=new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
mOurLocation=location;
// Add a marker in Our Location and move the camera
if(mOurLocationMarker!=null){
mOurLocationMarker.remove();
mMap.clear();
}
LatLng coor = new LatLng(mOurLocation.getLatitude(), mOurLocation.getLongitude());
putOnTheMapMarkerUser(coor);
}
};
private void putOnTheMapMarkerUser(LatLng latLng){
BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.arrow_me);//am convertit in BitmapDrawable
Bitmap bitmap = bitmapDrawable.getBitmap();//convertitm in bitmap
Bitmap smallMarker = Bitmap.createScaledBitmap(bitmap, 90, 90, false);//aici scalam
mOurLocationMarker=mMap.addMarker(new MarkerOptions().position(latLng).title("My position").flat(true).icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mOurLocationMarker.setRotation(mHeading);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,18));
}
final float tgtHdg = 135.0F; // bearing to stop rotation
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng pos) {
// Add marker
MarkerOptions mo = new MarkerOptions().position(pos);
final Marker m = mMap.addMarker(mo);
m.setTag(new Float(0.0));
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
float hdg = (float)m.getTag();
m.setRotation(hdg);
// change the 1.0 for amount of rotation (degrees) per update.
hdg += 1.0f;
m.setTag(new Float(hdg));
if (hdg < tgtHdg) {
// change the '50' (milliseconds) for faster/slower.
handler.postDelayed(this, 50);
}
}
}, 1000); // initial delay - can be 0 (milliseconds).
}
});