Google Maps is not a feature

I am dealing with a problem that my Google Maps page shows me an error in Firebug. The clearLocations () function is triggered by a search in my dealer map. But this error appears: " markers [i] .setMap is not a function "

Does anyone know how to solve this problem? I searched in several forums and groups, but I use google.maps.Marker array, so I can not find my problem.

Thanks in advance!

Code ( clearLocations () ):

function clearLocations() { infoWindow.close(); for (var i = 0; i < markers.length; i++) { markers[i].setMap(null); } markers.length = 0; dealers.innerHTML = ""; 

}

Code ( load () ):

 function load() { map = new google.maps.Map(document.getElementById("map_canvas"), { //center: new google.maps.LatLng(51.30174, 10.60824), zoom: 10, mapTypeId: 'roadmap', mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU} }); infoWindow = new google.maps.InfoWindow(); dealers = document.getElementById("dealers"); infoWindow = new google.maps.InfoWindow(); var bounds = new google.maps.LatLngBounds(); for (i = 0; i < markers.length; i++) { marker = new google.maps.Marker({ position: new google.maps.LatLng(markers[i][1], markers[i][2]), map: map }); var pos = new google.maps.LatLng(markers[i][1], markers[i][2]); bounds.extend(pos); google.maps.event.addListener(marker, 'click', (function(marker, i) { return function() { infowindow.setContent(markers[i][0]); infowindow.open(map, marker); } })(marker, i)); } map.fitBounds(bounds); 

}

+4
source share
2 answers

So, if I understand correctly, the marker array is an array of LatLongs? Or is it an array of type google.maps.Marker? If this is the first, then there will be no map features available, as this is not a Google maps marker.

The parameter may be to create an array of markers that you place on a map like this.

 var marker = new google.maps.Marker({ position: new google.maps.LatLng(markers[i][1], markers[i][2]), map: map }); mapMarkers[i] = marker; 

And then call

 mapMarkers[i].setMap(null) 
+11
source

you can try it - just

  console.log('=>'+markers.length); for (var i = 0; i < markers.length; i++) { console.log('===>'+markers[i]); markers[i] = null; } markers = []; console.log('==>'+markers.length); 
0
source

Source: https://habr.com/ru/post/1444761/


All Articles