![](/img/trans.png)
[英]Google Maps API v3 - Markers All Share The Same InfoWindow
[英]Google maps Javascript API V3 : Markers have same title
我在使用Google Maps API V3時遇到了困難。 我可以在我的地圖上顯示幾個標記(3個標記)但每個標記具有相同的標題(Maurice GENEVOIX)。 你能幫我/告訴我怎么樣? 我無法理解。 我有以下代碼:
function initialize() {
var myMarker=null;
var i=0;
var GeocoderOptions;
var myGeocoder;
var nom;
var temp;
var info = [
['57 Avenue Joseph Kessel 78180 Montigny-le-Bretonneux','Paul VERLAINE'],
['24 Rue du champ d avoine 78180 Mintigny-le-Bretonneux','Pauline VERLAINE'],
['21 Rue du Poirier Saint Martin 78180 Mintigny-le-Bretonneux','Maurice GENEVOIX']
];
var mapOptions = {
center: new google.maps.LatLng(48.772, 2.028),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
function GeocodingResult( results , status , i)
{
if(status == google.maps.GeocoderStatus.OK){
myMarker = new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title: nom,
icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
});
} else {
alert("L'adresse n'a pas pu etre geocodee avec succes.");
}
}
for(i=0;i<info.length;i++){
GeocoderOptions={
'address' : info[i][0],
'region':'FR'
};
nom=info[i][1];
myGeocoder = new google.maps.Geocoder();
myGeocoder.geocode( GeocoderOptions, GeocodingResult );
}
}
google.maps.event.addDomListener(window, 'load', initialize);
這是一個JavaScript閉包問題,而不是Google Maps JS API的問題。 以下是您應該如何定義回調,以便為i
獲取正確的值。 (我冒昧地使用了GeocodingResult
函數並將其內聯。)
myGeocoder.geocode( GeocoderOptions, function(i){
return function(results, status){
if(status == google.maps.GeocoderStatus.OK){
markers.push( new google.maps.Marker({
position: results[0].geometry.location,
map: map,
title: info[i][1],
icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png'
}));
} else {
alert("L'adresse n'a pas pu etre geocodee avec succes.");
}
}
}(i)); // this sends the current i into the closure of the callback.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.