开发者

map.getCenter.lng() extends beyond -180 when dragging continuously. How can I stop that?

heres my code:

google.maps.event.addListener(map, 'idle', function(e) {
    console.log(map.getCenter().lng();      
});

when dragging the map horizontally repeatedly it will 开发者_StackOverflow社区produce a pattern such as: 0, -60, -120, -180, -240, -300

But i was hoping for: 0, -60, -120, -180, 120, 60

Is this a bug in gmaps or not?

Thanks!


just updating this to add an answer from here:

http://code.google.com/p/gmaps-api-issues/issues/detail?id=3247

as in the link above , you can create a new LatLng object which will correctly wrap the longitude.

var center = map.getCenter();

var wrapped = new google.maps.LatLng(center.lat(), center.lng());

I found that using my own math dividing my 180, I would still hit some contrived situations where a pixel or two drag around the prime meridian would cause incorrect measurements to be calculated - maybe my maths wasn't bang on, but in any case - the above seems to me to be a better fix.


Some would say a bug, while others would say it was a feature. To be fair it would seem like the "correct" behaviour, but it just doesn't give you any usable results.

I believe the most common way of getting past this "feature" is to make sure your value is never below -180 og above 180. An easy way to accomplish this is to always make the call:

map.getCenter().lng() % 180;

This will always give you the modulus 180 which is the remainder when you have subtracted by 180 until the value is in the correct range.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜