【问题标题】:Postal code google map return邮政编码谷歌地图返回
【发布时间】:2012-11-12 18:55:27
【问题描述】:

我一直在试图弄清楚这一点,以至于我确信它正盯着我的脸。

我正在使用 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

该问题的答案是合适的,但是当我尝试循环或执行 任何事情 时,我遇到的问题不是以脚本在此处已经完成的方式提取信息。

基于 jquery 自动完成它是谷歌查询的方式,我可以将“结果”分配给一个变量,然后以这种方式解析它以获得正确的类型名称吗?

到目前为止我尝试过的一切都失败了,通常会导致脚本无法运行。

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

【问题讨论】:

    标签: jquery ajax jquery-ui google-maps


    【解决方案1】:

    以下代码可用于将邮政编码提取到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/

    【讨论】:

    • 感谢 mccannf 的回答!实际上,我自己设法解决了这个问题,我很高兴看到我们都在同一页上。我的解决方案有点不同,请查看上方!
    【解决方案2】:

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

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

    More efficient way to extract address components

    我使用了 user: 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

    大家干杯。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 1970-01-01
      相关资源
      最近更新 更多