我正在構建一個地圖,我必須有編號標記以及標記與地圖上顯示的標記匹配的下方列表。我正在加載地址數組,並從同一個數組中填充我的列表。標記位置在地圖上正確顯示,列表正確,但標記上的數字全部相同(它們最終成爲地圖上的標記數+ 1)。因此在這個例子中,由於有3個地址,每個標記顯示「4」。谷歌地圖編號標記全都顯示相同數字
我的代碼:
function initialize()
{
var map;
var elevator;
var myOptions = {
zoom: 9,
center: new google.maps.LatLng(34.0708,-118.2762),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map($('#googleMap')[0], myOptions);
var addresses = [
{NAME:"an address name string",ADDRESS:"an address number string"},
{NAME:"an address name string",ADDRESS:"an address number string"},
{NAME:"an address name string",ADDRESS:"an address number string"}
]
for (var x = 0; x < addresses.length; x++) {
//The list, which works correctly
$("#listID").append("<li><img src='http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/marker" + (x + 1) + ".png' alt='marker'><strong>" + addresses[x].NAME + "</strong><br>" + addresses[x].ADDRESS + "<br></li>");
//Based off another SO answer, to populate the map based on addresses without having to look up lat/lng
$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+addresses[x].ADDRESS +'&sensor=false', null, function (data) {
var p = data.results[0].geometry.location;
var latlng = new google.maps.LatLng(p.lat, p.lng);
var image = 'http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/marker' + (x + 1) + '.png';
new google.maps.Marker({
position: latlng,
icon: image,
map: map
});
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
謝謝!我懷疑這個解決方案可能很簡單。看起來我需要更多地閱讀Javascript。有意義的是這是一個關閉問題。 –