Ahmed
Ahmed

Reputation: 93

rotate the marker with move direction

Iam developing android app with google map implemented. I have tried to make the marker rotate with the direction of moving (like navigation in google maps) but all tried codes failed. Below are the code. Any suggestion. Note(Iam getting the bearing from method that returns the location, speed and bering and I can see the float value of the bearing changes while changing the direction.

                float bearing = setterandGetter.getBearing();
                double lat = setterandGetter.getLatitude();
                double lon = setterandGetter.getLongitude();
                LatLng currentPosition = new LatLng(latitudelongitude);

                googleMap.moveCamera(CameraUpdateFactory.newLatLng(currentPosition));

Marker marker = googleMap.addMarker(new MarkerOptions().position(currentPosition).icon(BitmapDescriptorFactory.fromBitmap(blueIcon)).flat(true).rotation(bearing));

Upvotes: 1

Views: 2397

Answers (2)

R.R.M
R.R.M

Reputation: 790

Try this:

First of all declare marker varialble globally and initialise it i onCreate with following line :

marker = googleMap.addMarker(new MarkerOptions().position(currentPosition).icon(BitmapDescriptorFactory.fromBitmap(blueIcon)).flat(true).rotation(0));

Now add following code in onLocationChanged():

map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
       @Override
       public void onMyLocationChange(final Location location) {
       marker.setPosition(new LatLng(location.getLatitude(),location.getLongitude()))
       marker.setRotation(location.getBearing());
    }
});

Upvotes: 2

Harsh Singhal
Harsh Singhal

Reputation: 567

use map.clear() to clear the map and then create marker so whenever your Location changed old marker will automatically be cleared and new marker is created so it seems like marker is moving with the location

Upvotes: 0

Related Questions