javascript - 如何直接从 Google map 而不是地点获取对象

标签 javascript google-maps google-maps-api-3

我想获取 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();

JSFiddle demo

关于javascript - 如何直接从 Google map 而不是地点获取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31183257/

相关文章:

php - 在谷歌地图中按纬度/经度删除标记

javascript - HTML 表格单元格对齐

javascript - 如何在 jQuery 中查找元素值

javascript - 如何在 C# 中将 ExpandoObject 转换为字典?

ios - 适用于 iOS 的 Google Maps SDK 不断增加内存使用量

android - 如何为 android google map 创建自动化测试?

javascript - fusion-tables,需要帮助从表中获取最新数据并将其插入 map 代码中

javascript - Chrome 和 Firefox 相当于 IE 的 window.opener.functionName()

google-maps - 使用地理编码 api 有多少免费请求?

android - 如何避免运行时加载 googlemap api Javascript-phonegap?