Skip to content Skip to sidebar Skip to footer

Markers Not Showing On Google Maps

I'm trying to show markers on my map. but it's not showing them. this is the code I use: var map; var markers = new Array(); function initialize() { var mapOptions = {

Solution 1:

map is a local variable, only visible inside initialize.

Use this instead of map when you set the map-property of the marker:

function addMarker(event) {
    markers.push(event.latLng);

    marker = new google.maps.Marker({
        position: event.latLng,
        map: this
    });
}

Explanation:

the callback will be executed in the scope of the object that triggers the event. Here it is the Maps-instance(because the click-listener has been applied to the map ), this will refer to this object inside the callback.

Solution 2:

markers.push(event.latLng);

Here you're pushing the latlng, not the marker.

markers is an array variable with global scope, whereas marker is a local variable. In markers array you have to insert each inidividual marker and then process it.

function addMarker(event) {
    var marker = new google.maps.Marker({
        position: event.latLng,
        map: map
    });
    markers.push(marker);
}

Post a Comment for "Markers Not Showing On Google Maps"