gpt4 book ai didi

javascript - 为什么 place.address_components 总是未定义? ( map API v.3)

转载 作者:行者123 更新时间:2023-11-30 12:51:22 27 4
gpt4 key购买 nike

我的网站上有一个简单的搜索功能,我正在尝试访问地点结果对象的 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 总是未定义? ( map API v.3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20848033/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com