javascript - 邮政编码未出现在自动完成框 Google map 上?

标签 javascript google-maps-api-3 autocomplete street-address postal-code

我正在尝试在自动完成框上显示邮政编码信息。

例如:如果用户查看地址:25 Cordwallis Road,Madienhead,SL6 7DQ... 它还可以检索邮政编码。

function initialize() { 
 directionsDisplay = new google.maps.DirectionsRenderer();
  var heathrow = new google.maps.LatLng(51.4706001282,-0.461941003799);
  var mapOptions = {
    center: heathrow,
    zoom:14,
    mapTypeId: google.maps.MapTypeId.ROADMAP

  };

   map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
   directionsDisplay.setMap(map);

   var inputFrom =(document.getElementById('searchFrom'));
    var options = {      
       componentRestrictions: {country: "US"}
     };
  var autocompleteFrom = new google.maps.places.Autocomplete(inputFrom, options);

  var infowindow = new google.maps.InfoWindow();
  var marker = new google.maps.Marker({
    map: map
  });

  google.maps.event.addListener(autocompleteFrom, 'place_changed', function() {
   infowindow.close();
    marker.setVisible(false);
    inputFrom.className = '';
    var place = autocompleteFrom.getPlace();
    if (!place.geometry) {
      // Inform the user that the place was not found and return.
      inputFrom.className = 'notfound';
      return;
    }

    // If the place has a geometry, then present it on a map.
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(17);  // Why 17? Because it looks good.
    }
    marker.setIcon(/** @type {google.maps.Icon} */({
      url: place.icon,
      size: new google.maps.Size(71, 71),
      origin: new google.maps.Point(0, 0),
      anchor: new google.maps.Point(17, 34),
      scaledSize: new google.maps.Size(35, 35)
    }));
    marker.setPosition(place.geometry.location);
    marker.setVisible(true);

    var address = '';

    if (place.address_components) {
      address = [
        (place.address_components[0] && place.address_components[0].short_name || ''),
        (place.address_components[1] && place.address_components[1].short_name || ''),
        (place.address_components[2] && place.address_components[2].short_name || '')
      ].join(' ');
    }

    infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
    infowindow.open(map, marker);
    calcRoute();

  });

我如何使用它来获取邮政编码信息

var zip = address[address.length - 1].long_name = results[0].address_components;

谁能给点建议

最佳答案

您无法 100% 解决问题,因为并非您通过地址搜索获得的所有结果都包含邮政编码。

部分解决方案是修复代码的以下部分。

 (place.address_components[0] && place.address_components[0].short_name || ''),
 (place.address_components[1] && place.address_components[1].short_name || ''),
 (place.address_components[2] && place.address_components[2].short_name || '')

正如我认为您假设邮政编码位于 address_components 的 3 个之一,但它并不总是这样。

您可以拥有 3 个以上的 address_components。 我有一些邮政编码位于 6(place) address_components 的地址。

为了查找您需要知道属性名称的邮政编码或任何其他组件,它位于

place.address_components[i].types

对于 posalCode 他的名字是“postal_code” 这可以使用类似的东西来完成。

for(var p = 0; p < oPlace.address_components.length ; p++){
    if(oPlace.address_components[p].types.indexOf("postal_code") > -1){
        return oPlace.address_components[p].long_name
    }
}

关于javascript - 邮政编码未出现在自动完成框 Google map 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19902982/

相关文章:

google-maps-api-3 - 谷歌已开始以粉色突出显示搜索区域。 Google Maps API 3 中是否提供此功能?

javascript - 使用 jQuery-UI 自动完成选择选项时设置多个输入的值

vim - 在 Vim 中完成空格分隔的单词

javascript - h :commandButton 中实际操作后的 OnClick 操作

javascript - 防止通过引用分配

javascript - 如何在 Node.js/Express 中进行重定向(301)?

php - 处理标签

javascript - Javascript for 循环初始化 block 中的词法声明

javascript - 导航 map 时向 Google map 添加标记

javascript - 如何在谷歌街景中保存 "zoom"图像