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

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

问题描述

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

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

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

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())

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

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

推荐答案

啊,太棒了.google.maps.LatLngBounds 构造函数需要 SouthWest 和 NorthEast LatLng 参数.不知怎的,我弄错了坐标,而是从西北和东南通过了!

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

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

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天全站免登陆