【问题标题】:Angularjs, cannot pass data to geojsonLayer LeafletAngularjs,无法将数据传递给geojsonLayer Leaflet
【发布时间】:2015-02-10 20:43:19
【问题描述】:

所以我尝试为 Leaflet.js 创建指令,当我在指令中使用工厂时,一切正常

(function() {
'use strict';
 angular
    .module('directoryAppMap')
    .directive('leafletDirective', function (Directory) {
        return {
            restrict: 'EA',
            template:'<div></div>',
            link: function (scope,element, attrs) {
                var map = L.map(attrs.id, {
                    center: [40, -86],
                    zoom: 2
                });
                //create a CloudMade tile layer and add it to the map
                L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                    attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
                    maxZoom: 18
                }).addTo(map);
                Directory.get(function (data) {
                  L.geoJson(data).addTo(map);
                });
            }
        };
    });
})();

在我的控制器中,我做了一些事情并将其传递给查看

    $scope.geojson = {};
    $scope.FilteredGeojson = function () {
        var result;

        result = $filter('filter')($scope.data, $scope.search);
        result = $filter('offset')(result, $scope.currentPage *            $scope.pageSize);
        result = $filter('limitTo')(result, $scope.pageSize);
        $scope.geojson = result;
        return result;
    };

在表中一切正常我使用 ng-repeat 和 FilteredGeojson() 但是当我尝试将 $scope.geojson 传递给指令时,角度开始无限摘要循环并且地图没有任何 geojson

对上一个指令我使用添加

 scope: { 
 data: '='
 }

在我看来我通过了

 data="geojson"

不幸的是,我不能将传单指令用于角度,因为有很多标记非常慢

【问题讨论】:

    标签: angularjs leaflet geojson


    【解决方案1】:

    您在尝试时是否删除了工厂?这对我有用:

    指令:

    angular.module('app').directive('leaflet', [
      function () {
        return {
          restrict: 'EA',
          replace: true,
          scope: {
            data: "="
          },
          template: '<div></div>',
          link: function (scope, element, attributes) {
            var map = L.map(element[0], {
                center: [0, 0],
                zoom: 0
            });
            var tileLayer = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
                maxZoom: 18
            }).addTo(map);
            var geojsonLayer = L.geoJson(scope.data).addTo(map);
            map.fitBounds(geojsonLayer.getBounds());
          }
        };
      }
    ]);
    

    控制器:

    angular.module('app').controller('rootController', [
      '$scope',
      function ($scope) {
        $scope.geojson = {
          "type": "FeatureCollection",
          "features": [{
            "type": "Feature",
            "properties": {},
            "geometry": {
              "type": "Point",
              "coordinates": [45, 45]
            }
          },{
            "type": "Feature",
            "properties": {},
            "geometry": {
              "type": "Point",
              "coordinates": [-45, -45]
            }
          }]
        };
      }
    ]);
    

    模板:

    <leaflet data="geojson"></leaflet>
    

    这是 Plunker 上的工作示例:http://plnkr.co/edit/0cUudGJp2VwRtxFBMRqc?p=preview

    根据下面 cmets 中的请求,另一种实现方式是,实际上它是一种完全不同的传单指令方法。将所有逻辑保存在控制器中。回调方法:

    指令:

    angular.module('app').directive('leaflet', [
      function () {
        return {
          restrict: 'EA',
          replace: true,
          scope: {
            callback: "="
          },
          template: '<div></div>',
          link: function (scope, element, attributes) {
            scope.callback(L.map(element[0]));
          }
        };
      }
    ]);
    

    模板:

    <leaflet callback="callback"></leaflet>
    

    控制器:

    angular.module('app').controller('rootController', [
      '$scope',
      function ($scope) {
        $scope.geojson = {
          // See controller above
        };
        $scope.callback = function (map) {
          var tileLayer = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
            attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
            maxZoom: 18
          }).addTo(map);
          var layer = L.geoJson($scope.geojson).addTo(map);
          map.fitBounds(layer.getBounds());
        };
      }
    ]);
    

    这是这种方法的一个工作示例:http://plnkr.co/edit/0cUudGJp2VwRtxFBMRqc?p=preview

    【讨论】:

    • 我在它也适用于我之前尝试过,但我尝试从 FilteredGeojson 函数传递 scope.geojson,过滤后有数据,然后我得到不定式摘要循环并且地图为空?
    • 您能否尝试通过以下方式运行最终过滤的对象:geojsonlint.com(并且不要忘记在顶部导航栏中的 FeatureCollection 上进行设置)也许您的过滤器正在破坏您的数据。或者您有我们可以在某处查看的测试用例吗?
    • 实际上我认为过滤器不是这里的问题,我使用 FilteredGeojson() 考虑到 ng-repeat 并且一切正常,不幸的是我没有任何测试用例。或者也许还有其他方法可以将geojson从rest添加到leaflet.js中?在使用 angular-leaflet-directive 一切正常之前,它的性能非常糟糕,有很多标记。
    • 我为传单指令添加了另一种完全不同的方法。也许这对你有用,我猜它没有,但我只是想我会把它扔在那里,也许这是一种更方便的工作方式,可以让你更轻松地调试你的问题。哦,您可以使用 ng-repeat 遍历您的功能并不意味着您的 geojson 是有效的。当然,您应该真正验证您的数据集。
    • 如果您的数据集太大,您可以随时自行验证。保存并包含这个 JS 文件:raw.githubusercontent.com/mapbox/geojsonhint/master/… 然后像这样使用它var errors = geojsonhint.hint($scope.geojson); 然后检查返回的数组中是否有东西
    猜你喜欢
    • 1970-01-01
    • 2016-06-15
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2020-02-14
    相关资源
    最近更新 更多