gpt4 book ai didi

javascript - Google map v3 - 为什么 LatLngBounds.contains 返回 false

转载 作者:行者123 更新时间:2023-12-03 00:08:32 26 4
gpt4 key购买 nike

我有以下代码,我希望 contains 方法返回 true,但它返回 false:

var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(55.38942944437183, -2.7379201682812226),
new google.maps.LatLng(54.69726685890506, -1.2456105979687226)
);

var center = bounds.getCenter(); // (55.04334815163844, -1.9917653831249726)

var x = bounds.contains(center); // returns false

在同一页面上,其中map是对Map对象的引用,以下代码按预期返回true:

map.getBounds().contains(map.getBounds().getCenter())

为什么我对 bounds.contains 的调用可能返回 false?

最佳答案

啊,太棒了。 google.maps.LatLngBounds 构造函数需要 SouthWest 和 NorthEast LatLng 参数。我不知何故搞砸了我的坐标并通过了西北和东南!

var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(54.69726685890506,-2.7379201682812226),
new google.maps.LatLng(55.38942944437183, -1.2456105979687226)
);

var center = bounds.getCenter(); // still returns (55.04334815163844, -1.9917653831249726)

var x = bounds.contains(center); // now returns true

经验教训:getCenter 并不关心您是否使用 NorthWest 和 SouthEast 创建了 LatLngBounds,但如果您希望 contains 返回一个有用的答案,你最好通过建议的西南和东北!

关于javascript - Google map v3 - 为什么 LatLngBounds.contains 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5405539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com