邮政编码谷歌地图返回

时间:2012-11-12 18:55:28

标签: jquery ajax jquery-ui google-maps

我一直在努力解决这个问题,以至于我确信它正在盯着我。

我正在使用jquery使用以下代码自动完成用户输入的地址:

    $(function() {
      $("#address").autocomplete({
      //This bit uses the geocoder to fetch address values
      source : function(request, response) {
      geocoder.geocode({'address' : request.term },

function(results, status) {
   response($.map(results, function(item) {
   return {
      label : item.formatted_address,
      value : item.formatted_address,
      latitude : item.geometry.location.lat(),
      longitude : item.geometry.location.lng(),
      streetNo : item.address_components[0].long_name,
      streetName : item.address_components[1].long_name,
      town : item.address_components[2].long_name,
      province : item.address_components[4].long_name,
             }
         }));
      })
  }

我需要明确地提取地址组件,即邮政编码。现在我正在使用数字,但根据输入的地址,字段可能正确也可能不正确。

我几乎有同样的问题:

Google Maps Geocoder: Return postal_code

这个问题的答案是恰当的,但问题是当我尝试循环时,我会拥有他的问题,或者任何事情,而不是按照已经完成的方式提取信息,脚本失败。< / p>

基于jquery自动完成功能的google查询方式,我可以将“结果”分配给变量,然后以正确的类型名称解析它吗?

到目前为止我尝试过的所有内容都失败了,通常导致脚本无法运行。

我在这里绝望,希望我的问题是连贯的! :P

2 个答案:

答案 0 :(得分:2)

以下代码可用于将邮政编码提取到postal_code

$(function() {
  var geocoder = new google.maps.Geocoder();

  $("#address").autocomplete({
    //This bit uses the geocoder to fetch address values
    source : function(request, response) {
      geocoder.geocode({'address' : request.term },

      function(results, status) {
        response($.map(results, function(item) {
          var postalCode = "";
          if (item.address_components) {
            for (var i in item.address_components) {
              if (typeof(item.address_components[i]) === "object" && item.address_components[i].types[0] == "postal_code") {
                postalCode = item.address_components[i].long_name;
              }
            }
          }

          return {
            label : item.formatted_address,
            value : item.formatted_address,
            latitude : item.geometry.location.lat(),
            longitude : item.geometry.location.lng(),
            streetNo : item.address_components[0].long_name,
            streetName : item.address_components[1].long_name,
            town : item.address_components[2].long_name,
            province : item.address_components[4].long_name,
            postal_code : postalCode
          }
        }));
      })
    }
  });
});

请参阅此jsfiddle:http://jsfiddle.net/mccannf/uzvvq/3/

答案 1 :(得分:0)

好的伙计,所以我设法弄清楚了。在这里发布答案以防万一其他人都被困在这件事上。

它的编码方式允许程序员通过使用stackoverflow上另一个答案中编写的函数来提取完全他们正在寻找的类型:

More efficient way to extract address components

我使用过用户:Johann的答案,但我会将我的代码包含在内,以便为像我这样可能很难“缩小差距”的其他“业余爱好”程序员提供更大的整体“范围”。

$(function() {
    $("#address").autocomplete({
        //This bit uses the geocoder to fetch address values
        source : function(request, response) {  

            geocoder.geocode({
                'address' : request.term
            },function(results, status) {
                //Here we take the response and manipulate the map
                response($.map(results, function(item) {
                    //Function here to be called by the variables we want, essentially it loops through the array
                    //of address_components that come back from the geolocate, and loops until it finds the 'type'
                    //Entered in the "return" section below
                    function extractFromAdress(components, type){
                        for (var i=0; i<components.length; i++)
                            for (var j=0; j<components[i].types.length; j++)
                                if (components[i].types[j]==type) return components[i].long_name;
                            return "";
                            }
                    //Returns the variables, left of the ':' to the script
                    //Code right of the ':' is whatever we want to assign to the variable
                    return {
                        label : item.formatted_address,
                        value : item.formatted_address,
                        //Lat and Long from the geo-locate
                        latitude : item.geometry.location.lat(),
                        longitude : item.geometry.location.lng(),
                        //Typical street address values a user would input, and what a typical program would need.                          
                        streetNo : extractFromAdress(item.address_components, "street_number"),
                        streetName : extractFromAdress(item.address_components, "route"),
                        town : extractFromAdress(item.address_components, "administrative_area_level_3"),
                        province : extractFromAdress(item.address_components, "administrative_area_level_1"),
                        postal : extractFromAdress(item.address_components, "postal_code"),
                    }

                }));
            })
        }

有关地址中可用类型的参考,请查看以下链接:

https://developers.google.com/maps/documentation/geocoding/index#JSON

为每个人欢呼。

相关问题