Convert string to marker in google map to remove old marker on map

I have a checkboxlist where a user can select multiple routes and then receive a response from the server. I have gotoLocation method to remove the location of the markers and also add a new marker on the map when a new one is inserted into a table on the server with the same route.

I had a problem adding a new marker to the map, so when I inserted data for a new marker with the same selected route in my database table, the new inserted was not added to the map. I saved the ID and marker in a HashMap before I solved this problem by storing the marker data as String

in a HashMap, in which case the new inserted data is added to the map as a marker.

Now in Update block

I need to convert this data latit,longit, rout_dirc

to a marker to remove the old one from the map and the HashMap before updating its location. How can I do this in my case? How do I remove the old marker from the map, since I have no marker in the HashMap, now I cannot use marker.remove()

to remove the old one?

I appreciate any help

Code:

public class Map extends FragmentActivity {

    GoogleMap map;
    static HashMap<Integer, String> markerMap = new HashMap<Integer, String>();
    static String marker_string;

    static Marker marker = null;
    private void gotoLocation(int id, double lat, double lng,
            String route_direct) {
        final float zoom = 11;
        LatLng ll = null;
        if (markerMap.containsKey(id)) {
            // Update the location.
            marker_string = markerMap.get(id);              
            String[] marker_string_split = marker_string.split(",");
            double latit = Double.parseDouble(marker_string_split[0]);
            double longit = Double.parseDouble(marker_string_split[1]);
            String rout_dirc = marker_string_split[2];

            LatLng LL_2 = new LatLng(lat, lng);
            MarkerOptions markerOpt2 = new MarkerOptions().title(route_direct)
                    .position(LL_2);

            // This here doest work.
            marker.remove();

            // Remove from the HashMap tp add the new one.
            markerMap.remove(id);

            ll = new LatLng(lat, lng);
            MarkerOptions markerOpt = new MarkerOptions().title(route_direct)
                    .position(ll);
            marker = map.addMarker(markerOpt);

            String lat1 = Double.toString(lat);
            String lng1 = Double.toString(lng);
            String data = lat1 + "," + lng1 + "," + route_direct;
            markerMap.put(id, data);
            zoom();

        } else {
            // Add a new  marker 
            String lat1 = Double.toString(lat);
            String lng1 = Double.toString(lng);
            String data = lat1 + "," + lng1 + "," + route_direct;
            markerMap.put(id, data);

            ll = new LatLng(lat, lng);
            MarkerOptions markerOpt = new MarkerOptions().title(route_direct)
                    .position(ll);
            marker = map.addMarker(markerOpt);
            zoom();


        }
    }
}

      

+3


source to share


1 answer


It seems that your marker is marker.remove();

set to null, and it won't do any good if you don't have a reference to the markers you want to remove. If you want to remove all old markers, you can try this:

mMap = super.getMap();
map.clear();

      

This removes everything. Otherwise, when you add markers, you must have the link in case you need to remove it later.



For example, your marker variable:

Marker marker = map.addMarker(..);

      

Then you can uninstall it. marker.remove();

Hope this makes sense;

0


source







All Articles