1

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

Rishabh Bhardwaj
  • 832
  • 8
  • 20
  • Try looking at this post http://stackoverflow.com/questions/16482949/google-map-api-removing-markers – Brian P May 14 '13 at 08:26

2 Answers2

3

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.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
0

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)

prvn
  • 916
  • 5
  • 5