我想获取 map 对象(机场登机口)的坐标。
我无法使用 Places API 做到这一点,因为门不是 Places 对象。
如果您打开maps.google.com并输入SFO Gate 27,它不会找到它,但如果您输入SFO,按Enter键,然后输入Gate 27,您就会找到正确的位置。
那么,我的问题是如何通过 JS SDK 或 HTTP 请求找到这些地方?
最佳答案
与地理编码器配合使用。
var geocoder;
var map;
function initialize() {
map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 5,
center: new google.maps.LatLng(10, 10)
});
geocoder = new google.maps.Geocoder();
// Bind click event listener for search button
document.getElementById("search").addEventListener('click', codeAddress, false);
// Bind key-up event listener for address field
document.getElementById("address").addEventListener('keyup', function (event) {
// Check the event key code
if (event.keyCode == 13) {
// Key code 13 == Enter key was pressed (and released)
codeAddress();
}
});
}
function codeAddress() {
// Get address and geocode
var address = document.getElementById("address").value;
geocoder.geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
// Center map on result bounds
map.fitBounds(results[0].geometry.bounds);
// Place marker on map
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
initialize();
关于javascript - 如何直接从 Google map 而不是地点获取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31183257/