繁体   English   中英

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

[英]Google Maps v3 - Why is LatLngBounds.contains returning false

我有以下代码,我希望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参数。 我不知何故搞砸了我的坐标,而是通过了NorthWest和SouthEast!

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返回有用的答案,则最好传入建议的SouthWest和NorthEast!

我想这更容易尝试。 它对我有用而不必担心NE或SW

var bounds = new google.maps.LatLngBounds();
bounds.extend(54.69726685890506,-2.7379201682812226);
bounds.extend(55.38942944437183, -1.2456105979687226);
var center = bounds.getCenter();  // still returns (55.04334815163844, -1.9917653831249726)
var x = bounds.contains(center);  // now returns true

我知道这篇文章很老,但我来这里寻找答案,所以想到从我学到的东西中更新。

这是它对我有用的方式:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM