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

好久不见. 提交于 2019-12-20 15:31:07

问题


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.


回答1:


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
 }


来源:https://stackoverflow.com/questions/19826543/how-to-rotate-arrow-head-in-googlemap-v2-when-we-rotate-the-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!