angularjs with Leafletjs

前端 未结 4 1642
独厮守ぢ
独厮守ぢ 2020-12-25 09:55

Following directie code is from http://jsfiddle.net/M6RPn/26/ I want to get a json feed that has many lat and long.. I can get a json with $resource or $http in Angular easi

4条回答
  •  半阙折子戏
    2020-12-25 10:26

    Assuming that in your controller you got

    $scope.points = // here goes your retrieved data from json
    

    and your directive template is:

    
    

    then inside your directive definition you can use the "=" simbol to setup a bi-directional binding between your directive scope and your parent scope

    module.directive('sap', function() {
    return {
        restrict: 'E',
        replace: true,
        scope:{
          points:"=points"
        },
        link: function(scope, element, attrs) {
            var map = L.map(attrs.id, {
                center: [40, -86],
                zoom: 10
            });
            L.tileLayer('http://{s}.tile.cloudmade.com/57cbb6ca8cac418dbb1a402586df4528/997/256/{z}/{x}/{y}.png', {
                maxZoom: 18
            }).addTo(map);
    
            for (var p in points) {
                L.marker([p.lat, p.lng]).addTo(map);
            }
        }
    };
    });
    

    Also instead of adding the markers right into the map, it's recomended to add your markers to a L.featureGroup first, and then add that L.featureGroup to the map because it has a clearLayers() metho, which will save you some headaches when updating your markers.

    grupo = L.featureGroup();
    grupo.addTo(map);
    
    for (var p in points) {
        L.marker([p.lat, p.lng]).addTo(grupo);
    }
    
    
    // remove all markers
    grupo.clearLayers();
    

    I hope this helps, cheers

提交回复
热议问题