2013-11-14 136 views
0

我在Google Maps API V3中遇到困難。我能夠在我的地圖上顯示多個標記(3個標記),但每個標記都具有相同的標題(Maurice GENEVOIX)。你能幫助我/告訴我如何?我無法想象它。我的代碼如下:谷歌地圖JavaScript API V3:標記具有相同的標題

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

回答

2

這是JavaScript關閉問題,而不是Google Maps JS API的問題。以下是您應該如何定義回調的方法,以便您可以獲得i的正確值。 (我拿了把你GeocodingResult功能和內嵌它的自由。)

DEMO

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. 
+0

謝謝您的回答,你的演示,這有很大幫助!我或多或少的瞭解問題的來源,但無法正確解決問題。它現在工作正常! – NicowB

+0

那麼,你能將這個答案標記爲'正確答案'嗎? :) –