2010-10-28 35 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

相关问题