簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM