【问题标题】:Google Maps Javascript API - bounds.contains methodGoogle Maps Javascript API - bounds.contains 方法
【发布时间】:2016-02-21 22:18:52
【问题描述】:

我使用 Google Maps API (Javascript) 创建了一张地图,如下所示:

var currentLocation = {lat: 47.3663615, lng: 8.5388539};
var map = new google.maps.Map(document.getElementById('map'), {
    zoom: zoomLevel,
    center: currentLocation,
    mapTypeId: google.maps.MapTypeId.TERRAIN
});

然后我尝试使用 bound.contain 函数来检查各种 LatLng 位置是否包含在地图边界内:

var center = map.getCenter(); // works fine
var bounds = map.getBounds(); // works fine
var x = bounds.contains(center); // works fine, delivers true
var a = bounds.contains(currentLocation); throws an error (Uncaught TypeError: a.lat is not a function)

经过一些调试,currentLocation 对象似乎没有正确的格式来使用“包含”方法进行处理。请注意,我对 javascript 和 Google Maps api 还是很陌生。

提前感谢您的帮助。

【问题讨论】:

    标签: javascript google-maps-api-3


    【解决方案1】:

    LatLngBounds.contains 当前需要 LatLng 作为参数,LatLngBoundsLiteral 将不被接受。

    使用

    var currentLocation = new google.maps.LatLng(47.3663615,8.5388539);
    

    【讨论】:

    • 已按建议更正。现在工作得很好。非常感谢您的及时回答和提示。
    猜你喜欢
    • 1970-01-01
    • 2011-08-23
    • 2020-05-11
    • 1970-01-01
    • 2020-10-07
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    相关资源
    最近更新 更多