javascript - Markers not showing on google maps - Stack Overflow

I'm trying to show markers on my map. but it's not showing them.this is the code I use:var ma

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 = {
        zoom: 12,
        center: new google.maps.LatLng(27.9881, 86.9253),
    };
    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    google.maps.event.addListener(map, 'click', addMarker);
}

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

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

    console.log(markers);
}

google.maps.event.addDomListener(window, 'load', initialize);

the console is logging on every click an new coordinate, so the function is called, and coordinates are passed. And I can't really see any problems in the marker code.

So can anyone see the problem?

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 = {
        zoom: 12,
        center: new google.maps.LatLng(27.9881, 86.9253),
    };
    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    google.maps.event.addListener(map, 'click', addMarker);
}

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

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

    console.log(markers);
}

google.maps.event.addDomListener(window, 'load', initialize);

the console is logging on every click an new coordinate, so the function is called, and coordinates are passed. And I can't really see any problems in the marker code.

So can anyone see the problem?

Share Improve this question asked Dec 19, 2013 at 10:52 KiwiKiwi 2,7737 gold badges49 silver badges86 bronze badges 1
  • Visiting today and i just see that may be removing var from initialize() function will solve it :) – Mayur Patel Commented Jan 30, 2016 at 10:49
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744668162a4586876.html

相关推荐

  • javascript - Markers not showing on google maps - Stack Overflow

    I'm trying to show markers on my map. but it's not showing them.this is the code I use:var ma

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信