How to remove bookmarks from a map with the v3 API?

Asked

Viewed 3,617 times

4

I’m developing an app to design routes from data collected by an embedded GPS system. My server receives coordinates from different equipment and I am recording this in my database.

What has helped me so much is this question in Stackoverflow-en.

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

My question is how to remove Bookmarks from the map.

In the image below I have a result from the execution of my application:

I would like to make it as shown in the image edited below (No intermediate markers, only markers start and end):

During my research to solve this problem I found several possible solutions, among which I found plausible 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 with some changes in the variable i that could start with i = 1 and the condition i <= markers.length it might be possible to preserve the markers start and end, but I don’t understand why this isn’t removing markers.

  • 2

    What code are you trying? Regarding the i, the stopping condition would be i < markers.length-1 and not i <= markers.length, maybe this is returning an error and "seems" that is not working...

  • Next Eduardo... it’s kind of hard to know where the problem is if we don’t have the marker generation code, post the javascript code we checked for you.

1 answer

4


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

You have to store the instance of google.maps.Marker in an array when you create them (see this example);

Taking the example of code who you followed, what 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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.