2010-10-28 60 views
1

我嘗試通過這種方式(例如)添加/定製與GMAP V2標記:Google Map API v2 - GeoCoder - 如何自定義標記?

for (var i in table) 
{ 
    var myvar = table[i]['text'] ; 
    var myaddress = table[i]['address'] ; 

    geocoder.getLatLng(
      myaddress , 
      function(point) { 
       alert(myvar) ; // here myvar is wrong 
       // ... adding customer markers ...    
      } 
    }); 
} 

在此示例中,我得到了很好的點爲表中的每個條目,但MYVAR是錯誤的,每次調用:myvar停留等於表的最後一項...

geocoder.getLatLng是異步函數,是由於那個原因?


編輯: 感謝您的答覆。但我只是有這個問題,當我使用循環例如:

var address = 'somewhere'; 
for (i = 0 ; i < 3 ; i++) 
{ 
    geocoder.getLatLng(
      address, 
      function(point) { 
       if (point) { 
        alert(i); 
       } 
    }); 
} 

點總是等於3!

回答

1

這是我做的。

 if ($('#map_canvas').length != 0) { 

      var marker= new GIcon(title); 
      marker.image = '/images/icon.png'; 
      marker.iconSize = new GSize(139,64); 
      marker.iconAnchor = new GPoint(0, 64); 
      marker.name = title ; 


       markerOptions = { icon:marker }; 
      map = new GMap2(document.getElementById("map_canvas")); 
      geocoder = new GClientGeocoder(); 
      geocoder.getLatLng(
       address, 
       function(point) { 
       if (!point) { 
        alert(address + " not found"); 
       } else { 
        map.setCenter(point, 14); 
        map.addOverlay(new GMarker(point, markerOptions)); 
        } 
       } 
      );  
     } 
    }); 

如果這沒有幫助,你可以看看: http://code.google.com/intl/da/apis/maps/documentation/javascript/overlays.html#Icons

1

Autosolved:

geocoder = new GClientGeocoder(); 
map = new GMap2(document.getElementById("map")); 
map.setCenter(new GLatLng(50, 3, 13)); 

geocoder = new GClientGeocoder(); 
function showAddress(address) 
{ 
    var adresses = ["10 place de la joliette, 13002 MARSEILLE", 
    "15 place de la joliette, 13002 MARSEILLE", 
    "20 place de la joliette, 13002 MARSEILLE"]; 

    for (var i = 0; i < adresses.length; i++) 
    { 
     alert(adresses[i]); 
     var address = adresses[i]; 
     addMarkerAtGeocode(address); 
    } 
} 
function addMarkerAtGeocode(address) 
{ 
    geocoder.getLatLng(address, function(point) { 
      if (!point) { 
       alert(address + " not found"); 
      } else { 
       alert('2:' + address); 
       var marker = createMarker(point, address); 
       map.addOverlay(marker); 
      } 
    }); 
} 

function createMarker(point, address) 
{ 
    var marker = new GMarker(point); 
    return marker; 
} 

showAddress() ; 
+0

不錯,你可以解決你的問題,並與我們分享。請將您的答案標記爲答案。 – 2012-07-09 09:06:08