2010-09-10 62 views

回答

8

该作品(前提是你要有正确的API密钥,受2500个请求/天的速度极限):

address_string = '1600 Pennsylvania Avenue NW Washington, DC 20500'; 
geocoder = new GClientGeocoder(); 
geocoder.getLatLng(
    address_string, 
    function(point) { 
     if (point !== null) { 
      alert(point); // or do whatever else with it 
     } else { 
      // error - not found, over limit, etc. 
     } 
    } 
); 

你似乎被调用的GClientGeocoder功能,在您需要创建一个new GClientGeocoder()并致电函数。

+0

谢谢,这工作。我仍在学习JavaScript。 – Queueball 2010-09-10 13:31:45

+0

@Queueball:不客气,我还在学习:) – Piskvor 2010-09-10 13:37:21

1

试试看喜欢:

geocoder = new GClientGeocoder(); 
point = geocoder.getLatLng(address) 
38

好像你正在使用谷歌地图的V2;如果您有兴趣使用V3(因为谷歌已经正式弃用第2版),你可以做这样的事情:

var mygc = new google.maps.Geocoder(); 
mygc.geocode({'address' : '1600 Pennsylvania Avenue NW Washington, DC 20500'}, function(results, status){ 
    console.log("latitude : " + results[0].geometry.location.lat()); 
    console.log("longitude : " + results[0].geometry.location.lng()); 
}); 

它类似于除了其他例子:

  • 你叫新google.maps.Geocoder()代替的GClientGeocoder()
  • 您可以通过自身
  • 结果传递一个对象有“地址”属性,而不是字符串是一个JSON对象,这意味着你必须ACCE ss lat/lng略有不同

Google API docs有关于v3的更多详细信息。干杯!

相关问题