Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps setMap is not a function

Tags:

google-maps

i am dealing with the Problem that my google Maps Page shows me an error in Firebug. The clearLocations() function is fired when i search within my dealer map. But this error is showing up: "markers[i].setMap is not a function"

Does anyone know how to solve that problem? I searched in several forums and groups, but i am using a google.maps.Marker array so i can't 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);

}

like image 710
j-fichtel Avatar asked Nov 08 '12 10:11

j-fichtel


1 Answers

So, if I understand correctly, the Markers array is an array of LatLongs? Or is it an array of type google.maps.Marker? If it's the former then there won't be any maps functions available to it as it's not a Google maps marker.

An option might be to make an array of the markers you place on the 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)
like image 62
Idiot211 Avatar answered Sep 19 '22 16:09

Idiot211