Mar*_*ger 22 javascript google-maps google-maps-api-3
我有以下代码,我希望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
Run Code Online (Sandbox Code Playgroud)
在同一页面上,map是对Map对象的引用,以下代码按预期返回true:
map.getBounds().contains(map.getBounds().getCenter())
Run Code Online (Sandbox Code Playgroud)
为什么我的电话bounds.contains会回复假?
Mar*_*ger 47
啊,太棒了.该google.maps.LatLngBounds构造预计西南和东北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
Run Code Online (Sandbox Code Playgroud)
获得的经验:getCenter不关心你是否LatLngBounds用NorthWest和SouthEast 创建了,但是如果你想contains返回一个有用的答案,你最好传入建议的SouthWest和NorthEast!
axs*_*axs 13
我想这更容易尝试.它对我有用而不必担心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
Run Code Online (Sandbox Code Playgroud)
我知道这篇文章很老,但我来这里寻找答案,所以想到从我学到的东西中更新.