Reputation: 831
I am adding a maker on touch of map and want to remove that marker on click of some button but that marker is not removing from map .Here is my Code
// Marker of end Point
Marker endPointMarker;
onclick of map
@Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
double lat = point.latitude;
double lng = point.longitude;
// Add marker of destination point
try {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(BookCabScreen.this);
if (lat != 0 || lng != 0) {
addresses = geocoder.getFromLocation(lat, lng, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
Log.d("TAG", "address = " + address + ", city =" + city
+ ", country = " + country);
endPointMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Location").snippet("" + address));
markers.add(mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Location").snippet("" + address)));
btnStartUp.setEnabled(true);
btnStopPoint.setEnabled(true);
mJbBookCab.setEndPointLat(lat);
mJbBookCab.setEndPointLng(lng);
} else {
Toast.makeText(BookCabScreen.this,
"latitude and longitude are null",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
on click of button
if (endPointMarker != null) {
endPointMarker.remove();
endPointMarker = null;
}
But it is not removing from map ?Please help
Upvotes: 1
Views: 2019
Reputation: 22232
You are adding same marker twice:
endPointMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Location").snippet("" + address));
markers.add(mMap.addMarker(new MarkerOptions()
.position(new LatLng(lat, lng))
.title("Location").snippet("" + address)));
Just remove one call to GoogleMap.addMarker
.
Upvotes: 3
Reputation: 916
what you are doing is correct but if this is not working then You can use mMap.clear() inside your onclick method this will remove all the markers or if you want only a specific marker not to be shown then you can use endPointMarker.setVisible(false)
Upvotes: 0