Advertisement
jamesmyers

Untitled

Mar 28th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Service to manage geoJSON layering with Leaflet.js' angular directive, which only allows 1 set of geoJSON data.
  3.  *
  4.  * Assuming you have a leaflet directive with its 'geojson' attribute set to `geojson`, usage is as follows:
  5.  *     var layers = new GeoJSONLayers();
  6.  *
  7.  *     layers.addLayer('myLayer', geoJSON, function(feature) { return { fillColor: '#00F' }; });
  8.  *     $scope.geojson = layers.get();
  9.  *
  10.  *     layers.removeLayer('myLayer');
  11.  *     $scope.geojson = layers.get();
  12.  */
  13. angular.module('dcMap').factory('GeoJSONLayers', function($http, kmlHandler, $ionicModal) {
  14.     function findById(source, id) {
  15.       for (var i = 0; i < source.length; i++) {
  16.         if (source[i]._id === id) {
  17.           return source[i];
  18.         }
  19.       }
  20.       throw "Couldn't find object with id: " + id;
  21.     }
  22.         var handler = function()
  23.         {
  24.             this.clear();
  25.         };
  26.  
  27.         handler.prototype.clear = function()
  28.         {
  29.             this.json = {
  30.                 type : "FeatureCollection",
  31.                 features : []
  32.             };
  33.             this.layerStyles = {};
  34.         };
  35.  
  36.         handler.prototype.addLayer = function(layerID, geoJSON, styleCallback, pointToLayerCallback, onEachFeatureCallback)
  37.         {
  38.             this.layerStyles[layerID] = styleCallback;
  39.  
  40.             this.pointToLayer = pointToLayerCallback;
  41.  
  42.             this.onEachFeature = onEachFeatureCallback;
  43.  
  44.             // tag features with their assigned layer
  45.             geoJSON.features.forEach(function(feature, i) {
  46.                 feature.properties.__LAYERID__ = layerID;
  47.             });
  48.  
  49.             // merge into current objects
  50.             Array.prototype.push.apply(this.json.features, geoJSON.features);
  51.         };
  52.  
  53.         handler.prototype.removeLayer = function(layerID)
  54.         {
  55.             var feats = this.json.features,
  56.                 i = 0, latlng = this.json.latlng;
  57.  
  58.             delete this.layerStyles[layerID];
  59.  
  60.             // remove relevant geoJSON objects as well
  61.             for (; i < feats.length; ++i) {
  62.                 feature = feats[i];
  63.                 if (feature.properties.__LAYERID__ == layerID) {
  64.                     feats.splice(i, 1);
  65.                     --i;
  66.                 }
  67.             }
  68.         };
  69.         var x2js = new X2JS();
  70.  
  71.         handler.prototype.__handleStyle = function(feature)
  72.         {
  73.             if (feature.properties['__LAYERID__'] === undefined) {
  74.                 return {};
  75.             }
  76.             console.log(this.pointToLayer(feature))
  77.  
  78.             return this.layerStyles[feature.properties.__LAYERID__](feature);
  79.         }
  80.         handler.prototype.__handlePointToLayer = function(feature, pointToLayer)
  81.         {
  82.           console.log( this.pointToLayer(feature));
  83.             return this.pointToLayer(feature);
  84.         }
  85.         handler.prototype.__handleOnEachFeature = function(feature, pointToLayer)
  86.         {
  87.             return this.onEachFeature(feature);
  88.         }
  89.         handler.prototype.get = function($scope)
  90.         {
  91.             var self = this;
  92.               return {
  93.                 data: this.json,
  94.                 style: function(feature) {
  95.                     return self.__handleStyle.call(self, feature);
  96.                 },
  97.                 /*hack below, works, want to be able to pass this in through prototype */
  98.                    pointToLayer: function(feature, latlng) {
  99.                       pointID = ((feature.properties.styleUrl).substring(1));
  100.                       var url = (findById(kmlHandler.iconStyles, pointID).IconStyle.Icon.href).substr(1);
  101.                       var smallIcon = new L.Icon({
  102.                         iconUrl: url,
  103.                         iconAnchor:  [12, 41],
  104.                         popupAnchor: [1, -34],
  105.                         shadowSize:  [41, 41]
  106.                       });
  107.                       return L.marker(latlng, {
  108.                         icon: smallIcon
  109.                       });
  110.                     },
  111.                 /*
  112.                 pointToLayer: function(feature) {
  113.                   console.log(self.__handlePointToLayer.call(self, feature));
  114.                     return self.__handlePointToLayer.call(self, feature);
  115.                 },*/
  116.                 onEachFeature: function (feature, layer) {
  117.                   //same deal here:
  118.                   //return self.__handleStyle.call(self, feature);
  119.                   layer.bindPopup('<div><h1>' + feature.properties.name + '</h1></div><div>' + feature.properties.description + '</div>');
  120.                 }
  121.               }
  122.         };
  123.  
  124.         return handler;
  125.     }
  126. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement