Question

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 that to my app. I red about markerOption.rotation(rotation) this seems a static one. I want to rotate the arrow dynamically when I rotate the phone.

Was it helpful?

Solution

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
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top