angularjs with Leafletjs

前端 未结 4 1630
独厮守ぢ
独厮守ぢ 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:35

    I don't know a lot about Leaflet or what you're trying to do, but I'd assume you want to pass some coordinates in from your controller to your directive?

    There are actually a lot of ways to do that... the best of which involve leveraging scope.

    Here's one way to pass data from your controller to your directive:

    module.directive('sap', function() {
        return {
            restrict: 'E',
            replace: true,
            template: '
    ', link: function(scope, element, attrs) { var map = L.map(attrs.id, { center: [40, -86], zoom: 10 }); //create a CloudMade tile layer and add it to the map L.tileLayer('http://{s}.tile.cloudmade.com/57cbb6ca8cac418dbb1a402586df4528/997/256/{z}/{x}/{y}.png', { maxZoom: 18 }).addTo(map); //add markers dynamically var points = [{lat: 40, lng: -86},{lat: 40.1, lng: -86.2}]; updatePoints(points); function updatePoints(pts) { for (var p in pts) { L.marker([pts[p].lat, pts[p].lng]).addTo(map); } } //add a watch on the scope to update your points. // whatever scope property that is passed into // the poinsource="" attribute will now update the points scope.$watch(attr.pointsource, function(value) { updatePoints(value); }); } }; });

    Here's the markup. In here you're adding that pointsource attribute the link function is looking for to set up the $watch.

    Then in your controller you have a property you can just update.

    function MapCtrl($scope, $http) {
       //here's the property you can just update.
       $scope.pointsFromController = [{lat: 40, lng: -86},{lat: 40.1, lng: -86.2}];
    
       //here's some contrived controller method to demo updating the property.
       $scope.getPointsFromSomewhere = function() {
         $http.get('/Get/Points/From/Somewhere').success(function(somepoints) {
             $scope.pointsFromController = somepoints;
         });
       }
    }
    

提交回复
热议问题