How to rotate arrow head in googlemap v2 when we rotate the phone

后端 未结 1 1384
天涯浪人
天涯浪人 2021-02-06 17:51

Can anybody help me with how to rotate the arrow head in google map v2? You have seen that in nevigation the arrow head is rotating to the direction we face. I want to implement

相关标签:
1条回答
  • 2021-02-06 18:22

    I was able to do that. Its so easy. below is how. This is to read the sensor and get the orientation of the phone.

    /**
     * Initialize the sensor manager.
     */
    private void setupSensorManager() {
        mSensorManager = (SensorManager) mContext
                .getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(mSensorListener,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_NORMAL);
    
        Log.d(TAG, "SensorManager setup");
    }
    
    /**
     * The sensor event listener.
     */
    SensorEventListener mSensorListener = new SensorEventListener() {
    
        @Override
        public void onSensorChanged(SensorEvent event) {
            mOrientation = event.values[0];
            Log.d(TAG, "Phone Moved "+mOrientation);
            draw(mOrientation);
        }
    
        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }
    };
    

    And this is where I really rotate. My marker is already added to the map. And I am accessing it from another class.

    public void draw(float angle) {
                // Take the relevant Marker from the marker list where available in map
        AndroidMapGoogleOverlayItem myself = (AndroidMapGoogleOverlayItem) getOverlayItem(0);
    
        if (myself == null) {
            return;
        }
        myself.getMarker().setRotation(mOrientation);  // set the orientation value returned from the senserManager
     }
    
    0 讨论(0)
提交回复
热议问题