Google Maps v3 - 为什么LatLngBounds.contains返回false [英] Google Maps v3 - Why is LatLngBounds.contains returning false

查看:121
本文介绍了Google Maps v3 - 为什么LatLngBounds.contains返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  var界限=新google.maps.LatLngBounds(
新google.maps.LatLng(55.38942944437183,-2.7379201682812226)
新google.maps.LatLng(54.69726685890506,-1.2456105979687226)
)的

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

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

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

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

为什么我要调用 bounds.contains 假?

解决方案

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(); //仍然返回(55.04334815163844,-1.9917653831249726)

var x = bounds.contains(center); //现在返回true

获得的经验: getCenter 不关心你是否用NorthWest和SouthEast创建了 LatLngBounds ,但是如果你想要包含来返回一个有用的答案,你最好通过建议的西南和东北!


I have the following code in which I would expect the contains method to return true, but it returns 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

On the same page, where map is a reference to the Map object, the following code returns true as expected:

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

Why might my call to bounds.contains be returning false?

解决方案

Ah, brilliant. The google.maps.LatLngBounds constructor expects SouthWest and NorthEast LatLng parameters. I have somehow bungled up my coordinates and passed in NorthWest and SouthEast instead!

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

Lesson learned: getCenter doesn't care if you created the LatLngBounds with NorthWest and SouthEast instead, but if you want contains to return a useful answer you better pass in the suggested SouthWest and NorthEast!

这篇关于Google Maps v3 - 为什么LatLngBounds.contains返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆