【问题标题】:How to return address types from google maps geocode?如何从谷歌地图地理编码返回地址类型?
【发布时间】:2012-01-20 13:26:40
【问题描述】:

我目前正在使用 google maps api 对地图上的位置进行地理编码并返回街道地址。

我目前用以下代码返回地址:

        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

帮助表示赞赏。

【问题讨论】:

    标签: javascript google-maps geocoding


    【解决方案1】:

    请问,您为什么要检查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 数组可以玩很多,玩得开心! :)

    若想获得更多乐趣,请查看http://gmaps-samples-v3.googlecode.com/svn/trunk/geocoder/v3-geocoder-tool.html 上的 Google Maps API v3 Geocoder Tool(源代码)

    【讨论】:

      【解决方案2】:

      long_name 是地理编码器返回的地址组件的全文描述或名称。

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

      【讨论】:

      • hmm... 提醒任何其他想法为“未定义”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 2014-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 2014-05-18
      相关资源
      最近更新 更多