我使用Google Places API轻松输入目的地。理想情况下,我希望用户能够开始输入语义名称(例如斯坦福大学)并让Places API自动提供一个选项。我正在使用Geocoder返回的结果来获取结构化位置数据。但是,我注意到如果我输入"斯坦福大学"的查询,则返回的地理编码结果不包含street_number的键。有谁知道如何使用Places API将地点地理编码为包含street_number的地理编码结果?
答案 0 :(得分:0)
如果我查找Stanford University with the places service,则会返回place_id
ChIJneqLZyq7j4ARf2j8RBrwzSk
。带有getDetails
的{{1}}请求会返回一个地址组件数组,其中的条目类型为place_id
。
代码段
street_number

var map;
var infowiandow;
var service;
var data_id;
var count = 1;
var request;
var placeIDs = [];
function initialize() {
map = new google.maps.Map(document.getElementById('map-canvas'), {
center: new google.maps.LatLng(19.12455, 72.929158),
zoom: 11
});
infowindow = new google.maps.InfoWindow();
var bounds = new google.maps.LatLngBounds();
var errorCnt = 0;
var successCnt = 0;
for (var i = 0; i < data.length; i++) {
placeIDs[i] = data[i]['place_id'];
infoWindow = new google.maps.InfoWindow();
service = new google.maps.places.PlacesService(map);
service.getDetails({
placeId: placeIDs[i]
}, function (place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
successCnt += 1;
var marker = new google.maps.Marker({
map: map,
position: place.geometry.location,
title: place.name
});
for (var i = 0; i < place.address_components.length; i++) {
for (var j = 0; j < place.address_components[i].types.length; j++) {
if (place.address_components[i].types[j] == "street_number") {
document.getElementById('info').innerHTML += "street_number:" + place.address_components[i].long_name + "<br>";
}
}
}
console.log(place);
bounds.extend(marker.getPosition());
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent(place.name + "<br>" + place.types + "<br><a href='" + place.url + "'>url</a>");
infowindow.open(map, this);
});
document.getElementById('success').innerHTML += "placename=" + place.name + " successCnt=" + successCnt + "<br>";
map.fitBounds(bounds);
} else {
document.getElementById('info').innerHTML += "[" + errorCnt + "] status=" + status + " successCnt=" + successCnt + "<br>";
errorCnt += 1;
}
});
}
/* }); */
}
var data = [{
place_id: "ChIJneqLZyq7j4ARf2j8RBrwzSk"
}];
google.maps.event.addDomListener(window, 'load', initialize);
&#13;
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
&#13;