How do I remove bookmarks from a map with the v3 API?

4

I am developing an application to design routes from data collected by an embedded GPS system. My server receives coordinates of different devices and I am writing this to my database.

What helped me a lot was this question in StackOverflow -en.

In this question I found the code of my application as well as a small correction to fit my needs.

My question is how to remove Map Markers.

In the image below I have a result coming from running my application:

Iwouldliketomakeitasshownintheimageeditedbelow(Nointermediatemarkers,onlystartandendmarkers):

During my research to solve this problem I found several possible solutions, among which I found plausible I found this, which is to add the following code to remove all markers.

for (i = 0;i < markers.length;i++) {
    markers[i].setMap(null);
}

I know that with changes in the variable i that could start with i = 1 and condition i <= markers.length it might be possible to preserve the start and end markers, but I do not understand why this is not removing markers.

    
asked by anonymous 05.02.2015 / 03:16

1 answer

4

Following the StackOverflow-en question ( this ) what's left in the array markers [] is just objects with the information to create the markers, not instances of google.maps.Marker.

You have to save the instance of google.maps.Marker in an array when the children (see this example );

Picking up the code sample , which you have to do is:

    var markers_inst = [];
    for (i = 0; i < markers.length; i++) {
        var data = markers[i]
        var myLatlng = new google.maps.LatLng(data.lat, data.lng);
        lat_lng.push(myLatlng);
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: data.title
        });

        markers_inst.push(marker);

        latlngbounds.extend(marker.position);
        (function (marker, data) {
            google.maps.event.addListener(marker, "click", function (e) {
                infoWindow.setContent(data.description);
                infoWindow.open(map, marker);
            });
        })(marker, data);
    }

After that you can remove the markers:

for (i = 1;i < markers_inst.length-1;i++) {
    markers_inst[i].setMap(null);
}
    
06.03.2015 / 13:22