与Leafletjs的angularjs

前端之家收集整理的这篇文章主要介绍了与Leafletjs的angularjs前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
以下直接代码来自 http://jsfiddle.net/M6RPn/26/
我想得到一个有很多lat和long的json Feed ..我可以轻松地在Angular中获得带有$resource或$http的json但是如何将它提供给这个指令来映射地图上的东西?
  1. module.directive('sap',function() {
  2. return {
  3. restrict: 'E',replace: true,template: '<div></div>',link: function(scope,element,attrs) {
  4. var map = L.map(attrs.id,{
  5. center: [40,-86],zoom: 10
  6. });
  7. //create a CloudMade tile layer and add it to the map
  8. L.tileLayer('http://{s}.tile.cloudmade.com/57cbb6ca8cac418dbb1a402586df4528/997/256/{z}/{x}/{y}.png',{
  9. maxZoom: 18
  10. }).addTo(map);
  11.  
  12. //add markers dynamically
  13. var points = [{lat: 40,lng: -86},{lat: 40.1,lng: -86.2}];
  14. for (var p in points) {
  15. L.marker([points[p].lat,points[p].lng]).addTo(map);
  16. }
  17. }
  18. };
  19. });
我不太了解Leaflet或你想要做什么,但我想你想要从你的控制器传递一些坐标到你的指令?

实际上有很多方法可以做到这一点……其中最好的方法是利用范围.

这是将数据从控制器传递到指令的一种方法

  1. module.directive('sap',lng: -86.2}];
  2. updatePoints(points);
  3.  
  4. function updatePoints(pts) {
  5. for (var p in pts) {
  6. L.marker([pts[p].lat,pts[p].lng]).addTo(map);
  7. }
  8. }
  9.  
  10. //add a watch on the scope to update your points.
  11. // whatever scope property that is passed into
  12. // the poinsource="" attribute will now update the points
  13. scope.$watch(attr.pointsource,function(value) {
  14. updatePoints(value);
  15. });
  16. }
  17. };
  18. });

这是标记.在这里你要添加链接功能正在寻找设置$watch的pointsource属性.

  1. <div ng-app="leafletMap">
  2. <div ng-controller="MapCtrl">
  3. <sap id="map" pointsource="pointsFromController"></sap>
  4. </div>
  5. </div>

然后在你的控制器中你有一个你可以更新的属性.

  1. function MapCtrl($scope,$http) {
  2. //here's the property you can just update.
  3. $scope.pointsFromController = [{lat: 40,lng: -86.2}];
  4.  
  5. //here's some contrived controller method to demo updating the property.
  6. $scope.getPointsFromSomewhere = function() {
  7. $http.get('/Get/Points/From/Somewhere').success(function(somepoints) {
  8. $scope.pointsFromController = somepoints;
  9. });
  10. }
  11. }

猜你在找的Angularjs相关文章