0

我想获取我添加到地图中的所有标记的坐标,但它只获取最后添加的标记。如何使每个添加的标记都显示在数组中?

google.maps.event.addListener(map, 'click', function (evt) {
    placeMarker(evt.latLng);
    coordinates = Array(evt.latLng + ';');
});

下面是它现在的打印方式:["(38.28993659801203, -89.6484375);"]. 我希望它打印出来["38.28993659801203,-89.6484375;39.9434364619742,-91.64794921875;"]

演示:http: //jsfiddle.net/edgren/CZ34s/

4

1 回答 1

2

coordinates在点击侦听器之外定义push新的坐标到数组上:

var coordinates = [];

google.maps.event.addListener(map, 'click', function (evt) {
    placeMarker(evt.latLng);
    coordinates.push(evt.latLng.toString());
});

或者,如果您想要一个长字符串,请创建coordinates一个字符串并将新值连接到其上:

var coordinates = "";

google.maps.event.addListener(map, 'click', function (evt) {
    placeMarker(evt.latLng);
    coordinates += evt.latLng.toString() + ";";
});
于 2013-04-08T14:11:22.100 回答