2012-01-20 65 views
2

我目前使用谷歌地图API来在地图上的地理位置地址,并返回该街道的地址。如何从Google地图地理编码返回地址类型?

目前,我用下面的代码返回地址:

 function codeLatLng(markerPos) { 
      geocoder.geocode({'latLng': markerPos}, function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
       if (results[1]) { 
       //Set markerAddress variable 
       var markerAddress = results[0].formatted_address; 
alert(markerAddress); 
... 

但如果我不想使用地址元件类型返回格式的地址,但更详细的版本,我怎么能返回某个地址值如:http://code.google.com/apis/maps/documentation/geocoding/#Types

帮助赞赏。

回答

3

请问,你为什么为results[1]检查,然后使用results[0](或者是它只是一个错字我应该忽略)?

只要statusOK,就会有至少一个结果。否则,status将是ZERO_RESULTS

无论如何,你可以使用这样的事情:

function codeLatLng(markerPos) { 
    geocoder.geocode({'latLng': markerPos}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     var markerAddress = results[0].address_components[0].long_name 
      + ' (type: ' + results[0].address_components[0].types[0] + ')'; 
     alert(markerAddress); 

你可以玩很多与整个address_components阵列,玩得开心! :)

更好玩,看看谷歌地图API V3地理编码工具(的源代码)在http://gmaps-samples-v3.googlecode.com/svn/trunk/geocoder/v3-geocoder-tool.html

1

long_name是由Geocoder返回的地址组件的全文描述或名称。

function codeLatLng(markerPos) { 
       geocoder.geocode({'latLng': markerPos}, function(results, status) { 
        if (status == google.maps.GeocoderStatus.OK) { 
        if (results[1]) { 
        //Set markerAddress variable 
        var markerAddress = results[0].long_name; 
    alert(markerAddress); 
... 
+0

嗯......这提醒为“未定义”任何其他的想法 – hairynuggets

相关问题