Android谷歌地图自动旋转

时间:2015-03-22 20:30:30

标签: android google-maps android-sensors autorotate compass

我正在尝试基于罗盘标题实现具有自动旋转功能的地图应用程序(当按下相应的按钮时)。我使用了这个问题的答案并使其适应我的项目: android maps auto-rotate

由于我的设备不支持Sensor.TYPE_ROTATION_VECTOR,因此我使用Sensor.TYPE_ORIENTATION(不建议使用)修改了代码。目前,它运作良好。唯一的问题是地图的旋转效果没有连续性。动画摇摇欲坠。我检查了一些其他应用程序,我喜欢它们的旋转效果。他们的地图顺利旋转。

为了消除这个问题,你能建议改进吗?

我的实现是:

float mDeclination;
private SensorManager mSensorManager;
Sensor sensor;
boolean isCompassOn = false;

private GoogleMap map; // Might be null if Google Play services APK is not available.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mSensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
    sensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

}


@Override
public void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
}

@Override
public void onPause() {
    mSensorManager.unregisterListener(this);
}

@Override
public void onLocationChanged(Location location) {

    GeomagneticField field = new GeomagneticField(
            (float)location.getLatitude(),
            (float)location.getLongitude(),
            (float)location.getAltitude(),
            System.currentTimeMillis()
    );

    mDeclination = field.getDeclination();
}

@Override
public void onSensorChanged(SensorEvent event) {

    if(event.sensor.getType() == Sensor.TYPE_ORIENTATION && isCompassOn) {
        updateCamera(event.values[0] + mDeclination);
    }
}

private void updateCamera(float bearing) {
    CameraPosition oldPos = map.getCameraPosition();
    CameraPosition pos = CameraPosition.builder(oldPos).bearing(bearing).build();
    GoogleMap.CancelableCallback callback = new GoogleMap.CancelableCallback() {
        @Override
        public void onFinish() {

        }

        @Override
        public void onCancel() {

        }
    };
    map.animateCamera(CameraUpdateFactory.newCameraPosition(pos), 24, callback);
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

0 个答案:

没有答案