如何直接从谷歌地图而不是地点获取对象

How can I get objects directly from Google Maps, not Places

本文关键字:获取 取对象 何直接 谷歌地图      更新时间:2023-09-26

我想获取地图对象(机场大门(的坐标。

我不能用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();

JSFiddle演示