【问题标题】:Why is place.address_components always undefined? (Maps API v.3)为什么 place.address_components 总是未定义? (地图 API v.3)
【发布时间】:2014-01-17 20:09:36
【问题描述】:

我的网站上有一个简单的搜索功能,我正在尝试访问地点结果对象的 address_components。但它总是不确定的。

基本上我要检索的是用于在信息窗口中显示每个标记的邮政编码。我以前对每个地方都使用了 formatted_address,但它不包含邮政编码。我假设 long_name 或 short_name 可能包含邮政编码,但我不知道如何检索它。

这是我到目前为止的代码:

function initialize() {

    var markers = [];
    var map = new google.maps.Map(document.getElementById('admin-map-canvas'), {
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    var vges = new google.maps.LatLng(x, y);

    var defaultBounds = new google.maps.Circle({center: vges, radius: 2000}).getBounds();
    map.fitBounds(defaultBounds);

  // Create the search box and link it to the UI element.
  var input = /** @type {HTMLInputElement} */(
    document.getElementById('pac-input'));
  map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

  var searchBox = new google.maps.places.SearchBox(
    /** @type {HTMLInputElement} */(input));

  // Listen for the event fired when the user selects an item from the
  // pick list. Retrieve the matching places for that item.
  google.maps.event.addListener(searchBox, 'places_changed', function() {
    var places = searchBox.getPlaces();

    for (var i = 0, marker; marker = markers[i]; i++) {
        marker.setMap(null);
    }

    var infowindow = new google.maps.InfoWindow({
        content:"",
        maxWidth: 300
      });

    // For each place, get the icon, place name, and location.
    markers = [];
    var bounds = new google.maps.LatLngBounds();
    for (var i = 0, place; place = places[i]; i++) {
        var image = {
            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(25, 25)
        };

      // Create a marker for each place.
      var marker = new google.maps.Marker({
        map: map,
        title: place.name,
        position: place.geometry.location
      });


      if(typeof place.address_components=='undefined'){
        address= "foo";
      }

      else{
        address= place.address_components[0];
      }

      markers.push(marker);

      bindInfoWindow(marker,map,infowindow,place.name,address);

      bounds.extend(place.geometry.location);
  }


  function bindInfoWindow(marker,map,InfoWindow,strDescription,Address){

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(strDescription + ", " + Address);
        infowindow.open(map,marker);
    });

  }

  map.fitBounds(bounds);
});

  // Bias the SearchBox results towards places that are within the bounds of the
  // current map's viewport.
  google.maps.event.addListener(map, 'bounds_changed', function() {
    var bounds = map.getBounds();
    searchBox.setBounds(bounds);
  });
}



$(document).ready(function(){
    google.maps.event.addDomListener(window, 'load', initialize);
});

【问题讨论】:

    标签: javascript google-maps google-maps-api-3


    【解决方案1】:

    已编辑:address_components 仅在地方详细信息请求的结果中可用,而不是在地方搜索请求的结果中可用。因此,您需要通过 Place Details 请求传递每个地点的参考 ID 以获取 postal_code。


    @Tuco 在这里有一个很好的答案:Retrieving Postal Code with Google Maps Javascript API V3 Reverse Geocode

    代码是:

    var address = place.address_components;
    var code = address[address.length -1].long_name;
    

    这是可行的,因为邮政编码始终是 address_components 数组中的最后一项。

    编辑:您提到 address_components 始终未定义;您的代码在 typeof 检查中使用address_component(没有s),因此它始终是未定义的。

    【讨论】:

    【解决方案2】:

    place.postal_code 不存在。

    bindInfoWindow(marker,map,infowindow,place.name,place.postal_code);
    

    也许您正在寻找 place.formatted_address?

    【讨论】:

    • 我的错误。更新的问题。我试过 formatted_address 但它不包含邮政编码。
    猜你喜欢
    • 2017-05-12
    • 2017-07-04
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多