简体   繁体   English

谷歌地图Javascript API V3:标记具有相同的标题

[英]Google maps Javascript API V3 : Markers have same title

I'm having a hard time with Google Maps API V3. 我在使用Google Maps API V3时遇到了困难。 I am able to display several markers on my map (3 markers) but every markers have the same title (Maurice GENEVOIX). 我可以在我的地图上显示几个标记(3个标记)但每个标记具有相同的标题(Maurice GENEVOIX)。 Are you able to help me/tell me how ? 你能帮我/告诉我怎么样? I can't figure it. 我无法理解。 I have the code below : 我有以下代码:

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

This is a JavaScript closure issue, rather than an issue with the Google Maps JS API. 这是一个JavaScript闭包问题,而不是Google Maps JS API的问题。 Here's how you should define the callback, so you can get the correct value for i . 以下是您应该如何定义回调,以便为i获取正确的值。 (I took the liberty of taking your GeocodingResult function and inlining it.) (我冒昧地使用了GeocodingResult函数并将其内联。)

DEMO 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM