1

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));
Ahmed
  • 93
  • 7

2 Answers2

2

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());
    }
});
R.R.M
  • 780
  • 4
  • 10
0

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

Harsh Singhal
  • 567
  • 4
  • 12
  • i want to keep all the previous markers on the map as my app shows the route were driven through, so I cannot clear every marker after locating it. – Ahmed Dec 09 '17 at 04:42