How to remove Markers one by one(javascript)

Asked

Viewed 303 times

1

I have the following code to add my markers:

 function setParking(map)
{
   var pointA = { lat: -16.113700, lng: -45.825545 };
   var pointB = { lat: -15.284216, lng: -44.658747 };
   var poinC = { lat: -16.139567, lng: -43.236152 };
   
   setMap(map, pointA);
   setMap(map, pointB);
   setMap(map, poinC);
}

function setParkingMap(map, point)
        {
                var contentString = '<div id="content">'+
                    '<div id="siteNotice">'+
                    '</div>'+
                    '<h1 id="firstHeading" class="firstHeading">Loja Cascais</h1>' +
                    '<div id="bodyContent">'+
                    '<p><b>Morada:</b> Teste' +
                    '<p><b>Horário:</b> Teste 2 ' +
                    '</div>' +
                    '<button onclick="">Obter indicações</button>'
                '</div>';

                var infowindow = new google.maps.InfoWindow({
                    content: contentString
                });

                var marker = new google.maps.Marker({
                    position: point,
                    map: map,
                    title: 'Teste'
                });
                marker.addListener('click', function() {
                    infowindow.open(map, marker);
                });
        }

function initMap() {
var mapOptions = {
            zoom: 15, center: new google.maps.LatLng(38.696029, -9.424029)
        }

        map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

setParking(map)
}

  jQuery(document).ready(function () {
        initMap();
    });

What I intend to do now is when I click on the checkbox to call a function that erases me a Marker or more, I tried the following:

poinC.setMap(null), but it can’t be.

Does anyone know how I can do this?

  • Can provide the event and function where this should occur sff

  • <input id="chkOranTarif" type="checkbox" name="chkOranTarif" class="psform__checkbox" onchange="removeMap()"/> function removeMap(){ poinC.setMap(null)}. I don’t think it adds much but the idea is this

  • Debug: If you put an Alert inside the function it works? It’s not usually a problem but put ';' next to setMap(null);

  • Maybe it’s an error in the syntax at points A and B you have pointA/pointB and in the C has poinC

  • On the console the error you give me is the following: poinC.setMap is not a Function

  • Do you understand English well? If so, take a look here: http://stackoverflow.com/questions/13286913/google-maps-setmap-is-not-a-function and here http://stackoverflow.com/questions/14099556/why-setmapnullis-not-working-google-maps-apiv3

  • The problem is that I don’t want to have an array of points, I want to have everything individualized. as the selected checkbox may appear a Marker or not.

Show 2 more comments

2 answers

1


I figured out what was wrong, I was deleting stitches and not markers, I created markers= [], whenever I add a Marker do markers.push(marker); and in the delete function:

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

1

If you can make a suggestion to avoid the cycle:

<input id="chkOranTarif" type="checkbox" name="chkOranTarif" class="psform__checkbox" onchange="removeMap(this)" data-marker="<MARKER A REMOVER>"/>

EX:

<input id="chkOranTarif" type="checkbox" name="chkOranTarif" class="psform__checkbox" onchange="removeMap(this);" data-marker="0"/>

...

function removeMap(checkbox) {
   var markerToDel = checkbox.data('marker'); // aqui vai ser o num 0
   markers[markerToDel].setMap(null); // apagar o primeiro, markers[0].setMap(null);
}
  • And so erase them all?

  • It only deletes what you want, if for each checkbox you have an associated Marketer... If it is to delete all then the cycle is what I recommend

  • Thanks for the tip, but in this case I have several checkboxes and the combination of them is that make show some markers and others not so I will have to use the same. Thanks for the tip

Browser other questions tagged

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