用于平移的Google Maps Strict Bounds

时间:2012-10-11 12:54:02

标签: google-maps

我正在使用以下示例:

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));
   });

2 个答案:

答案 0 :(得分:1)

我发现这是一个更清洁的解决方案。它通过基于中心点的列表/平移来组合功能,但是使用检测某个边界集是否在另一个边界内的测试。似乎对我来说很完美。

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);

    });

//更新:+缺少分号

答案 1 :(得分:-1)

你可能不得不使用map.getBounds而不是map.getCenter,看看它的边缘是否超出了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)
        ));
    });