Rotate marker as per user direction on Google Maps V2 Android

前端 未结 3 577
情书的邮戳
情书的邮戳 2020-12-07 23:20

I want to rotate marker as per bearing or sensor value received from Accelerometer to show the user where actually he is moving. I have set marker icon and flat value to tru

3条回答
  •  -上瘾入骨i
    2020-12-07 23:44

    This is an old question and it appears the API has changed since then.

    I'm assuming you are able to get the devices bearing. If not here is a handy tutorial.

    First thing is to create a marker we can use for bearing updates.

    private Marker marker;
    
    // Create this marker only once; probably in your onMapReady() method
    marker = mGoogleMap.addMarker(new MarkerOptions()
            .position(new LatLng(myLatitude, myLongitude))
            .flat(true));
    

    Note the .flat(true) portion. The ensures our marker is north aligned so that our bearings will work correctly even if the user rotates the map.

    Now when you get your bearing updates you can do the following

    marker.setRotation(bearing);
    // or if following the linked tutorial
    // marker.setRotation((float) azimuth);
    

    This assumes your marker icon has the forward direction at the top. If your marker is rotated like the one pictured, you will have to adjust the bearing to compensate before setting it to the marker. Just a simple setRotation(bearing - 45) should do it.

提交回复
热议问题