javascript - 强制停止 map 拖动

标签 javascript google-maps google-maps-api-3

我正在与 Google Maps 合作现在,我希望能够禁止用户将 map 拖出指定的边界。

var strictBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(49.00, 14.07),
    new google.maps.LatLng(54.50, 24.09)
);

我该怎么办?我尝试使用 drag事件但没有运气..

google.maps.event.addListener(map, 'drag', function() {
    if (
         !strictBounds.contains(map.getBounds().getNorthEast())
      || !strictBounds.contains(map.getBounds().getSouthWest())
    ) {
        // map is out of bounds here
    }
});

如何停止用户对 strictBounds 的拖动?

最佳答案

您可能想改为监听 dragend 事件,如果 map 被拖动到允许的边界之外,请将其移回到允许的边界内。很高兴您使用 LatLngBounds对象来定义边界,因为您将能够使用 contains() 方法,如果给定的 lat/lng 参数在边界内,该方法将返回 true。

您可能还应该限制缩放级别,因为通过缩小,用户仍然能够“看到”边界之外的 map 。

因此,您可能需要尝试以下示例:

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>Google Maps JavaScript API v3 Example: Limit Panning</title> 
   <script type="text/javascript" 
           src="http://maps.google.com/maps/api/js?sensor=false"></script>
</head> 
<body> 
   <div id="map" style="width: 400px; height: 300px;"></div> 

   <script type="text/javascript"> 

   var minZoomLevel = 5;

   var map = new google.maps.Map(document.getElementById('map'), {
      zoom: minZoomLevel,
      center: new google.maps.LatLng(38.50, -90.50),
      mapTypeId: google.maps.MapTypeId.ROADMAP
   });

   // Bounds for North America
   var strictBounds = new google.maps.LatLngBounds(
     new google.maps.LatLng(28.70, -127.50), 
     new google.maps.LatLng(48.85, -55.90));

   // Listen for the dragend event
   google.maps.event.addListener(map, 'dragend', function() {
     if (strictBounds.contains(map.getCenter())) return;

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

   // Limit the zoom level
   google.maps.event.addListener(map, 'zoom_changed', function() {
     if (map.getZoom() < minZoomLevel) map.setZoom(minZoomLevel);
   });

   </script> 
</body> 
</html>

上述示例的屏幕截图。在这种情况下,用户将无法向南或远东拖动:

Google Maps JavaScript API v3 Example: Force stop map dragging

关于javascript - 强制停止 map 拖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3194323/

相关文章:

javascript - IE11 上纯 JavaScript 中的 Angular

ios - 位置返回零

html - 如何在我的谷歌地图旁边添加 CSS 表格?

javascript - 从图像中获取像素颜色总是 0 0 0 0?

javascript - vue.js:597 [Vue warn]: 属性或方法 "$t"未定义

javascript - 注入(inject)不纯函数与调用它有何不同?

javascript - 将谷歌地图自动完成限制为仅距给定地址位置 50 英里

css - 在 Google Maps API 中更改链接颜色或链接 CSS

javascript - 在 Google map (React) 中获取可拖动标记位置 (lat,lng)

javascript - 在 html 元素的 id 中插入点或句点的目的是什么