无法设置经度从-180到0的LatLngBounds

时间:2014-11-24 14:44:05

标签: javascript google-maps-api-3

当尝试设置一个新的LatLngBounds时,对于世界西南部,西南经度自动设置在世界的另一边,导致LatLngBounds不确定(SouthWest比NorthEast更东)

a = new google.maps.LatLng(-90, -180, true);
a.lng();
=> -180
b = new google.maps.LatLng(0, 0, true);
c = new google.maps.LatLngBounds(a, b);
c.getSouthWest().lng();
=> 180

问题似乎不在LatLng中,而在LatLngBounds中更多。它们是否是其他方式或其他方式,因此它可以代表世界的这个角落?

使用更多参数进行测试,只有西南经度始终设置为-180:http://jsfiddle.net/vr6ztq9z/1/

1 个答案:

答案 0 :(得分:1)

<强>纬度:

在墨卡托投影上,北纬最大值不是90,而是85.05113左右。在JavaScript中,您可以这样做:

Math.atan(Math.sinh(Math.PI)) * 180 / Math.PI;

通过这种方式,您可以找到投影的真实的北边和南边

<强>经度:

经度-180到180之间的区别是什么?没有。

您仍然可以识别投影的所有四分之三

var maxLat = Math.atan(Math.sinh(Math.PI)) * 180 / Math.PI;

var center = new google.maps.LatLng(0, 0);
var sw = new google.maps.LatLng(-maxLat, 180);
var ne = new google.maps.LatLng(maxLat, -180);

// Southwest part of the world
new google.maps.LatLngBounds(sw, center);

// Southeast part of the world
new google.maps.LatLngBounds(center, sw);

等等。

JSFiddle demo