我的网站上有一个简单的搜索功能,我正在尝试访问放置结果对象的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);
});
最佳答案
编辑:address_components仅在“地点详细信息”请求的结果中可用,而在“地点搜索”请求中不可用。因此,您需要通过“地点详细信息”请求传递每个地点的参考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
),因此始终是未定义的。关于javascript - 为什么place.address_components总是未定义? (Maps API第3版),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20848033/