![](/img/trans.png)
[英]Google maps LatLng and LatLngBounds returning different results
[英]Google Maps returning longitude >180 in LatLng.toUrlValue
我正在建立一个带有重叠多边形的全球地图。 它在jQuery手风琴内部。 每当我切换手风琴窗格然后返回地图时,它就会跳到南极洲。 为了克服此问题,我一直试图捕获当前的中心LatLng,将其存储在变量中,然后在返回地图的手风琴面板时设置setCenter()。
它没有用,所以我尝试使用.toUrlValue()方法将当前的中心坐标转储到页面上的可见元素。 当我平移地图的东或西时,经度数字一直在增加。 我希望它可以转到180,然后重置为-180。
我的代码:
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);
}
文档指示getCenter()返回LatLng,而setCenter()需要LatLng。 我尝试设置currMapCenter = map.getCenter()时会误解什么吗? 鉴于我的地图需要像当前一样包装,我该如何捕获可用的LatLng以与setCenter()一起使用?
getCenter()返回LatLng,但可能超过180度。 这取决于您绕地球轴旋转多少次。 :)
我找到了解决此问题的简单方法
var loc=map.getCenter();
loc=new google.maps.LatLng(loc.lat(), loc.lng(), false);
现在,LatLng已重新计算为正确的值,您可以使用它了...
该行为记录在Map对象的 getCenter方法中
getCenter()
纬度
返回显示在地图中心的位置。 请注意,此LatLng对象未包装 。 有关更多信息,请参见LatLng。
也许可以使用getBounds()(如果将地图缩小以显示多个副本,则可能无法使用)
getBounds()
LatLngBounds
返回当前视口的纬度/经度边界。 如果可以看到一个以上的世界副本,则边界的经度范围为-180至180度(含)。
现在看来,该主题的标题不准确,与实际问题无关。 IE8-毫不奇怪-给我带来了各种各样的问题,根本没有绘制地图,而符合标准的浏览器很好,除了切换手风琴窗格时失去中心之外。
在我的原始代码中,我正在监听'center_changed'事件以捕获当前地图中心坐标。 我开始想知道,随着手风琴关闭,jQuery的事件是否正在改变地图的位置,所以我将中心捕获事件切换为“ mouseout”。 这解决了问题,尽管我仍然不知道是什么原因造成的。
旁注:我一直在使用window.onload = function(){initialize_map(); }在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);
}
上面的代码可以正常工作; 在Firefox 14,Chrome和IE8中进行了测试。
谢谢,这个线程帮助了我。 这是我用来验证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.