1
加載內容我需要幫助獲取信息窗口從位置顯示值[I] [0]谷歌地圖信息窗口不是從VAR
位置[I] [1]的工作蠻好的地址值,但我希望能夠從var「locations」中爲infowindow中的每個標記設置自定義標題。
我可以定義一個變種設定的內容和信息窗口這樣工作,但其同樣爲每一個標記,而不是從VAR拉「位置」
任何幫助表示讚賞!
function initialize() {
geocoder = new google.maps.Geocoder();
var myOptions = {
zoom: 10,
center: new google.maps.LatLng(0, 0),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(
document.getElementById("map_canvas"),
myOptions);
setMarkers(map, locations);
}
var locations = [
['Bondi Beach', '798 9th Ave, New York, NY', 4],
['Coogee Beach', '42 E 29th St, New York, NY', 5],
['Cronulla Beach', '56 W 25th St, New York, NY', 3],
];
function setMarkers(map, locations) {
var infowindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
var marker, i;
for (var i=0; i<=locations.length; i++) {
geocoder.geocode({'address': locations[i][1]}, function(results, status) {
marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.open(map, marker);
infowindow.setContent(locations[i][0]);
}
})(marker, i));
bounds.extend(marker.getPosition());
map.fitBounds(bounds);
})
}
}
如果我有更多的狀態ID投票你。我知道這個循環存在問題,但我從來沒有將自己的部分分類出來。謝謝。 – user1440778
我欣賞內聯評論,這不僅解決了我幫助我學習的問題。 – user1440778
我忘了添加,geocoder是異步的(就像Ajax中的A),意味着它等待響應。它會打電話給Google服務器,所以結果不會立即生效。當響應到達時,執行回調(功能(結果,狀態))。這解釋了爲什麼地理編碼內部的代碼不能一次執行。 –