简体   繁体   English

Google Maps在LatLng.toUrlValue中返回经度> 180

[英]Google Maps returning longitude >180 in LatLng.toUrlValue

I am building a global map with overlay polygons. 我正在建立一个带有重叠多边形的全球地图。 It is inside a jQuery Accordion. 它在jQuery手风琴内部。 Whenever I switch accordion panes and then come back to the map, it jumps down to Antarctica. 每当我切换手风琴窗格然后返回地图时,它就会跳到南极洲。 To overcome this behavior, I have been trying to capture the current center LatLng, store it in a variable, then setCenter() when returning to the map's accordion panel. 为了克服此问题,我一直试图捕获当前的中心LatLng,将其存储在变量中,然后在返回地图的手风琴面板时设置setCenter()。

It didn't work, so I tried using the .toUrlValue() method to dump the current center coordinates to a visible element on the page. 它没有用,所以我尝试使用.toUrlValue()方法将当前的中心坐标转储到页面上的可见元素。 As I pan the map east or west, the longitude number just keeps incrementing. 当我平移地图的东或西时,经度数字一直在增加。 I expected it to go to 180 then reset to -180. 我希望它可以转到180,然后重置为-180。

地图经度问题

My code: 我的代码:

var currMapCenter = null;

jQuery(document).ready(function() {
  jQuery("#accordion").bind('accordionchange', function(event, ui) {
    if (ui.newContent.attr('id') == 'region-list')
    {
    triggerMapResize(); 
    }
});
});

function initialize_map() {

//do all the initialization here...

currMapCenter = map.getCenter(); //set initial center in variable.

google.maps.event.addListener(map, 'center_changed', function() {
  currMapCenter=map.getCenter();
  jQuery("#description").html(currMapCenter.toUrlValue());//to see what is happening...
});
}

function triggerMapResize()
{
google.maps.event.trigger(map, "resize");
map.setCenter(currMapCenter);
}

The documentation indicates getCenter() returns a LatLng, and setCenter() requires a LatLng. 文档指示getCenter()返回LatLng,而setCenter()需要LatLng。 Am I misunderstanding what happens when I try to set currMapCenter = map.getCenter()? 我尝试设置currMapCenter = map.getCenter()时会误解什么吗? Given that my map needs to wrap as it currently does, how do I capture a usable LatLng to use with setCenter()? 鉴于我的地图需要像当前一样包装,我该如何捕获可用的LatLng以与setCenter()一起使用?

getCenter() returns LatLng, but it could be more than 180 degrees. getCenter()返回LatLng,但可能超过180度。 It depends on how many times you rotate the earth around its axis. 这取决于您绕地球轴旋转多少次。 :) :)

I found simple fix for this problem 我找到了解决此问题的简单方法

var loc=map.getCenter();
loc=new google.maps.LatLng(loc.lat(), loc.lng(), false);

Now LatLng is recalculated to proper values and you can use it ... 现在,LatLng已重新计算为正确的值,您可以使用它了...

The that behavior is documented for the Map object getCenter method 该行为记录在Map对象的 getCenter方法中

getCenter() getCenter()
LatLng 纬度
Returns the position displayed at the center of the map. 返回显示在地图中心的位置。 Note that this LatLng object is not wrapped . 请注意,此LatLng对象未包装 See LatLng for more information. 有关更多信息,请参见LatLng。

Maybe you can use getBounds() (might not work if the map is zoomed out to show multiple copies) 也许可以使用getBounds()(如果将地图缩小以显示多个副本,则可能无法使用)

getBounds() getBounds()
LatLngBounds LatLngBounds
Returns the lat/lng bounds of the current viewport. 返回当前视口的纬度/经度边界。 If more than one copy of the world is visible, the bounds range in longitude from -180 to 180 degrees inclusive. 如果可以看到一个以上的世界副本,则边界的经度范围为-180至180度(含)。

It seems now that the title of this topic is inaccurate and irrelevant to the actual problem. 现在看来,该主题的标题不准确,与实际问题无关。 IE8 - no surprise - was giving me all sorts of problems and failing to draw the map at all, while standards-compliant browsers were fine except the loss of center when switching accordion panes. IE8-毫不奇怪-给我带来了各种各样的问题,根本没有绘制地图,而符合标准的浏览器很好,除了切换手风琴窗格时失去中心之外。

In my original code, I was listening for the 'center_changed' event to capture the current map center coordinates. 在我的原始代码中,我正在监听'center_changed'事件以捕获当前地图中心坐标。 I started wondering if jQuery's events were altering the position of the map as the accordion closed, so I switched the center capture event to 'mouseout'. 我开始想知道,随着手风琴关闭,jQuery的事件是否正在改变地图的位置,所以我将中心捕获事件切换为“ mouseout”。 This solved the problem, though I still don't know what caused it. 这解决了问题,尽管我仍然不知道是什么原因造成的。

side-note: I had been using window.onload = function () { initialize_map(); 旁注:我一直在使用window.onload = function(){initialize_map(); } with success in browsers other than IE, but IE choked and wouldn't load at all. }在IE以外的浏览器中取得了成功,但IE阻塞了,根本无法加载。

var map=null;
var currMapCenter = null;

jQuery(document).ready(function() {
jQuery("#accordion").bind('accordionchange', function(event, ui) {
if (ui.newContent.attr('id') == 'region-list')
{
if(map==null) {
//first load; call map initialization
initialize_map();
}
triggerMapResize(); 
}
});

function initialize_map() {

//do initilization here...

google.maps.event.addListener(map, 'mouseout', function() { 
currMapCenter=map.getCenter();
});
}

function triggerMapResize() {
google.maps.event.trigger(map, "resize");
map.setCenter(currMapCenter);
}

The above code works without a problem; 上面的代码可以正常工作; tested in Firefox 14, Chrome and IE8. 在Firefox 14,Chrome和IE8中进行了测试。

Thanks, this thread helped me. 谢谢,这个线程帮助了我。 This is the code I used to verify the noWarp setting and access the center that I required without the coordinates going over the +/- 180 degrees :- 这是我用来验证noWarp设置并访问我所需的中心的代码,坐标不超过+/- 180度:-

var gmap_center = new google.maps.LatLng(map.getCenter().lat(), map.getCenter().lng(), false);

console.log('GMap lat/lng with default noWrap:true = ' + map.getCenter().lat() + ' / ' + map.getCenter().lng());

console.log('GMap lat/lng with noWrap:false = ' + gmap_center.lat() + ' / ' + gmap_center.lng());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM