weixin_33698043 2012-11-12 18:55 采纳率: 0%
浏览 21

邮政编码谷歌地图返回

I've been trying to figure this out for so long that I'm sure it's staring me in the face.

I'm using jquery to auto complete an address being entered by a user with the following code:

    $(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,
             }
         }));
      })
  }

I need to extract the address components specfically i.e. postal code. Right now I'm using numbers but depending on the address inputted, the fields may or may not be correct.

I pretty much have the same question this guy did:

Google Maps Geocoder: Return postal_code

The answer to that question is appropriate but the issue I"m having his when I try to loop, or do anything other than extract the info the way it's already done here the script fails.

Based on the way the jquery autocomplete does it's google query, can I assign the "results" to a variable and then parse it that way for correct type names?

Everything I've tried so far has failed, usually resulting in the script not running.

I'm getting desperate here, hopefully my question is coherent! :P

  • 写回答

2条回答 默认 最新

  • weixin_33682719 2012-11-14 03:37
    关注

    The following code works for me to extract the postal code into 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
              }
            }));
          })
        }
      });
    });
    

    ​See this jsfiddle: http://jsfiddle.net/mccannf/uzvvq/3/

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog