android maps auto-rotate

后端 未结 5 1842
自闭症患者
自闭症患者 2020-12-12 19:45

If you open the Google maps app, there is a button on the top right of the screen that you can press to center the map on your current location. The button\'s icon then chan

5条回答
  •  伪装坚强ぢ
    2020-12-12 20:06

    It is possible by registering your application with Sensor Listener for Orientation and getting the angle relative to true north inside onSensorChanged and update camera accordingly. Angle can be used for bearing. The following code can be used:

    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));
    
    }
    

提交回复
热议问题