2012-10-11 18 views
1

我使用這個例子:谷歌地圖嚴厲的限制,進行平

Google Maps v3 - limit viewable area and zoom level

問題是,我決定在我的自定義地圖的SW和NE分,但它似乎只禁止任何基於搖攝地圖的中心,這允許在嚴格界限之外進行平移。

這裏是我的代碼:

var strictBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(sw_lat, sw_lon), 
    new google.maps.LatLng(ne_lat, ne_lon) 
    ); 

    google.maps.event.addListener(map, 'drag', function() 
    { 
    if (strictBounds.contains(map.getCenter())) return; 

    // We're out of bounds - Move the map back within the bounds 

    var c = map.getCenter(), 
     x = c.lng(), 
     y = c.lat(), 
     maxX = strictBounds.getNorthEast().lng(), 
     maxY = strictBounds.getNorthEast().lat(), 
     minX = strictBounds.getSouthWest().lng(), 
     minY = strictBounds.getSouthWest().lat(); 

    if (x < minX) x = minX; 
    if (x > maxX) x = maxX; 
    if (y < minY) y = minY; 
    if (y > maxY) y = maxY; 

    map.setCenter(new google.maps.LatLng(y, x)); 
    }); 

回答

-1

你可能不得不使用map.getBounds代替map.getCenter,看看那年秋天outwith strictBounds的界限的邊緣。

這是沒有經過充分測試,但這樣的事情是如何我解決它:

var strictBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(sw_lat, sw_lon), 
    new google.maps.LatLng(ne_lat, ne_lon) 
    ); 

    google.maps.event.addListener(map, 'drag', function() 
    { 
     var mapBounds = map.getBounds(), 
     map_SW_lat = mapBounds.getSouthWest().lat(), 
     map_SW_lng = mapBounds.getSouthWest().lng(), 
     map_NE_lat = mapBounds.getNorthEast().lat(), 
     map_NE_lng = mapBounds.getNorthEast().lng(), 
     maxX = strictBounds.getNorthEast().lng(), 
     maxY = strictBounds.getNorthEast().lat(), 
     minX = strictBounds.getSouthWest().lng(), 
     minY = strictBounds.getSouthWest().lat(); 

     if (strictBounds.contains(mapBounds.getNorthEast()) && strictBounds.contains(mapBounds.getSouthWest())) 
     { 
      return; 
     } 

     // We're out of bounds - Move the map back within the bounds 
     if (map_SW_lng < minX) map_SW_lng = minX; 
     if (map_SW_lng > maxX) map_SW_lng = maxX; 
     if (map_NE_lng < minX) map_NE_lng = minX; 
     if (map_NE_lng > maxX) map_NE_lng = maxX; 

     if (map_SW_lat < minY) map_SW_lat = minY; 
     if (map_SW_lat > maxY) map_SW_lat = maxY; 
     if (map_NE_lat < minY) map_NE_lat = minY; 
     if (map_NE_lat > maxY) map_NE_lat = maxY; 

     map.panToBounds(new google.maps.LatLngBounds(
     new google.maps.LatLng(map_SW_lat, map_SW_lng), 
     new google.maps.LatLng(map_NE_lat, map_NE_lng) 
     )); 
    }); 
+0

你有這方面的一個例子嗎? –

+0

查看我的更新回答 – duncan

+0

這似乎並不奏效。光標被固定在地圖上,我無法停止平移。 –

1

我發現這是一個更清潔的解決方案。它通過基於中心點的listneing/panning來組合功能,但使用檢測某個邊界是否在另一個範圍內的測試。似乎完全適合我。

google.maps.event.addListener(map, 'center_changed', function(){ 

     var mapBounds = map.getBounds(); 

     if (imageBounds.contains(mapBounds.getNorthEast()) && imageBounds.contains(mapBounds.getSouthWest())) 
     { 
      lastValidCenter = map.getCenter(); 
      return; 
     } 

     map.panTo(lastValidCenter); 

    }); 

//更新:+缺少分號

+0

這裏是什麼'imageBounds'? –

+0

哦,抱歉,這並不明確。 imageBounds是谷歌LatLngBounds類的一個實例。在這種情況下,您希望將平移限制在https://developers.google.com/maps/documentation/javascript/reference#LatLngBounds – Robin