Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

javascript - unable to set a LatLngBounds with longitude from -180 to 0

When trying to set a new LatLngBounds, for the southwest quarter of the world, the SouthWest longitude is automatically set at the other side of the world, leading to an unconsistent LatLngBounds (SouthWest is more east than 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

The problem seems not to be in the LatLng, but more in the LatLngBounds. Are they other params or other ways to do it so it can represent this quarter of the world ?

Tests with more parameters, only southwest longitude is always set to -180 : http://jsfiddle.net/vr6ztq9z/1/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Latitude:

On a Mercator projection, the maximum north latitude is not 90, but something around 85.05113. In JavaScript you can do:

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

This way you can find the real north and south edges of the projection.

Longitude:

What's the difference between a longitude of -180 and 180? None.

You can still identify all 4 quarters of the projection:

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

And so on.

JSFiddle demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...