【问题标题】:Using maxBounds to stop panning in Leaflet with angular-leaflet-directive使用 maxBounds 停止在带有 angular-leaflet-directive 的 Leaflet 中平移
【发布时间】:2016-10-24 19:34:07
【问题描述】:

现在我的 AngularJS 应用程序正在运行,我正在尝试限制 LeafletJS 地图平移,如 this Mapbox document 中所述。问题是我正在使用 angular-leaflet-directive 并且我现有的代码被编写为使用我的 AngularJS 模板中的传单指令创建地图。所以现有的地图是这样定义的:

<leaflet id="mymap" markers="markers" center="center" width="100%" height="380px"></leaflet>

在我从未明确调用过 new L.map('leaflet', {...})) 的这种情况下,我如何 getBounds() 和 setMaxBounds()?

【问题讨论】:

  • 你的 sn-p 不能正常工作
  • 是的,我同意。如果控制器中没有支持代码,则传单指令将不起作用。我只是试图说明我如何在生产中使用传单指令。

标签: angularjs leaflet angular-leaflet-directive


【解决方案1】:

boundsmaxBounds 属性可以通过 angular-leaflet-directive 分别使用 maxBoundsbounds 指令设置,例如:

<leaflet markers="markers" maxbounds="boundsInfo" bounds="boundsInfo"></leaflet>

应按以下格式指定边界:

$scope.boundsInfo = {'northEast': {'lat': 40.774, 'lng': -74.125},'southWest': {'lat': 40.712, 'lng': -74.227}};

示例

angular.module("demoapp", ["leaflet-directive"])
    .controller("CustomizedMarkersController", ['$scope','leafletData', function ($scope, leafletData) {

       
        $scope.boundsInfo = {'northEast': {'lat': 40.774, 'lng': -74.125},'southWest': {'lat': 40.712, 'lng': -74.227}};
        $scope.markers = [];

        leafletData.getMap().then(function (map) {
         
            console.log(map.getBounds());
        });

    }]);
.angular-leaflet-map {
    width: 640px;
    height: 480px;
}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="https://code.angularjs.org/1.2.2/angular.js"></script>
<script type="text/javascript" src="https://tombatossals.github.io/angular-leaflet-directive/dist/angular-leaflet-directive.js"></script>
<script src="https://unpkg.com/leaflet@1.0.1/dist/leaflet.js"></script>

<div ng-app="demoapp" ng-controller="CustomizedMarkersController">
    <leaflet markers="markers" maxbounds="boundsInfo" bounds="boundsInfo" maxZoom="19" minZoom="10"></leaflet>
</div>

【讨论】:

  • 对不起,我没有尽快回复您的回答。我真的很感激帮助。在我的应用程序中,我正在绘制地图,中心有一个标记,代表主要兴趣点。如何计算给定中心点的东北和西南边界?