[英]android maps auto-rotate
如果您打开 Google 地图应用程序,屏幕右上角有一个按钮,您可以按下该按钮以将地图置于当前位置的中心。 然后按钮的图标会发生变化。 如果您再次按下同一个按钮,地图会根据您的罗盘航向自动旋转。 换句话说,地图变得以自我为中心(与以异体为中心的又名北总是向上相反)。
Google 最近推出了适用于 Android 的地图 API V2,我当然比旧版本更喜欢它。 默认情况下,android maps V2 将包含“位置中心”按钮。 但是,多次按下它不会启用自动旋转; 它只是尝试再次将地图以您的位置为中心。
有谁知道我如何像谷歌地图应用程序一样使用地图 API v2 自动旋转地图? 我必须自己实现这个功能还是在 API 中实现,我只是没有看到它? 我感谢所有帮助。
好的,这是我认为应该在一年后完成的方式。 如果您发现任何问题,请纠正我。
以下大部分代码处理坐标系之间的差异。 我正在使用旋转矢量传感器。 来自文档: Y is tangential to the ground at the device's current location and points towards magnetic north.
另一方面,谷歌地图中的方位似乎指向真北。 此页面显示转换是如何完成的
1) 从您当前的 GPS 位置获取当前的磁偏角
@Override
public void onLocationChanged(Location location) {
GeomagneticField field = new GeomagneticField(
(float)location.getLatitude(),
(float)location.getLongitude(),
(float)location.getAltitude(),
System.currentTimeMillis()
);
// getDeclination returns degrees
mDeclination = field.getDeclination();
}
2)从磁偏角和磁北计算方位
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
SensorManager.getRotationMatrixFromVector(
mRotationMatrix , event.values);
float[] orientation = new float[3];
SensorManager.getOrientation(mRotationMatrix, orientation);
float bearing = Math.toDegrees(orientation[0]) + mDeclination;
updateCamera(bearing);
}
}
3)更新地图
private void updateCamera(float bearing) {
CameraPosition oldPos = mMap.getCameraPosition();
CameraPosition pos = CameraPosition.builder(oldPos).bearing(bearing).build();
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(pos));
}
我已经成功实现了 aleph_null 解决方案,在这里我将添加一些在已接受的解决方案中没有提到的细节:
要使上述解决方案起作用,您需要实现 android.hardware.SensorEventListener 接口。
您还需要在 onResume 和 onPause 方法中注册 SensorEventListener,如下所示:
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this,
mRotVectSensor,
SensorManager.SENSOR_STATUS_ACCURACY_LOW);
}
@Override
protected void onPause() {
// unregister listener
super.onPause();
mSensorManager.unregisterListener(this);
}
“@Bytecode”注意事项:为避免闪烁,采样周期使用低值,例如 SensorManager.SENSOR_STATUS_ACCURACY_LOW。
我还注意到,有时传感器发送的数据比设备可以处理的要多,因此,地图相机开始以一种奇怪的方式移动!
为了控制 onSensorChanged 处理的数据量,我建议如下实现:
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
SensorManager.getRotationMatrixFromVector(
mRotationMatrix, event.values);
float[] orientation = new float[3];
SensorManager.getOrientation(mRotationMatrix, orientation);
if (Math.abs(Math.toDegrees(orientation[0]) - angle) > 0.8) {
float bearing = (float) Math.toDegrees(orientation[0]) + mDeclination;
updateCamera(bearing);
}
angle = Math.toDegrees(orientation[0]);
}
}
可以通过使用 Sensor Listener for Orientation 注册您的应用程序并获取相对于 onSensorChanged 内的真北的角度并相应地更新相机。 角度可用于轴承。 可以使用以下代码:
Instead of using Sensor.TYPE_ORIENTATION try using getOrinetation api. Sensor.TYPE_ORIENTATION
has been deprecated.
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (sensorManager != null)
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
}
public void onSensorChanged(SensorEvent event) {
float degree = Math.round(event.values[0]);
Log.d(TAG, "Degree ---------- " + degree);
updateCamera(degree);
}
private void updateCamera(float bearing) {
CameraPosition oldPos = googleMap.getCameraPosition();
CameraPosition pos = CameraPosition.builder(oldPos).bearing(bearing)
.build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(pos));
}
如果这些函数可用于 map api V2,它们肯定会在GoogleMap或UiSettings 上
新的Location类已经自动为您提供了方位,为什么不使用它呢?
示例代码:
private void updateCameraBearing(GoogleMap googleMap, Location myLoc) {
if ( googleMap == null) return;
CameraPosition camPos = CameraPosition
.builder(googleMap.getCameraPosition())
.bearing(myLoc.getBearing())
// if you want to stay centered - then add the line below
.target(new LatLng(myLoc.getLatitude(), myLoc.getLongitude()))
.build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(camPos));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.