0

I am facing problem while removing the previous marker in google map. As my latitude and longitude values are updating new markers are showing in google map but does not remove any previous marker. And I have added a bus icon in the marker and I want to rotate the bus so it will move with me according to GPS. You can consider it as Uber app. In the uber app the car rotates and move. How can I do that? My full code of the marker portion is given below and the output of google map is attached here.[Output] https://i.stack.imgur.com/XnBbC.jpg

    private void findDriverLocation(Location location) {
        MarkerOptions markerOptions = new MarkerOptions();
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("DrvUserLocation");
        ValueEventListener listener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot ds: dataSnapshot.getChildren()){
                    if (ds.exists()) {
                        String dNum = ds.child("drvMobN").getValue(String.class);
                        double lat = ds.child("Latitude").getValue(Double.class);
                        double lng = ds.child("Longitude").getValue(Double.class);
                        LatLng dl = new LatLng(lat, lng);
                        mMap.addMarker(new MarkerOptions().position(dl).title(dNum).icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_icon)));
                    } else {
                        Toast.makeText(StdMapsActivity.this, "Latitude and Longitude Value Not Found!.", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(StdMapsActivity.this, "Failed to get data.", Toast.LENGTH_SHORT).show();
            }
        };
        ref.addValueEventListener(listener);
    }

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

2

the mMap.addMarker(...) method returns an object reference that you can store and use later to remove it from the map.

e.g.

Marker  markerX = mMap.addMarker(new MarkerOptions().position(dl).title(dNum).icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_icon)));

....
markerX.setMap(null);  //TO REMOVE MARKER FROM MAP

You can find a good sample for doing this here

Also, the Google Maps documentation has samples for actions like custom makers and rotation

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807