Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Service to manage geoJSON layering with Leaflet.js' angular directive, which only allows 1 set of geoJSON data.
- *
- * Assuming you have a leaflet directive with its 'geojson' attribute set to `geojson`, usage is as follows:
- * var layers = new GeoJSONLayers();
- *
- * layers.addLayer('myLayer', geoJSON, function(feature) { return { fillColor: '#00F' }; });
- * $scope.geojson = layers.get();
- *
- * layers.removeLayer('myLayer');
- * $scope.geojson = layers.get();
- */
- angular.module('dcMap').factory('GeoJSONLayers', function($http, kmlHandler, $ionicModal) {
- function findById(source, id) {
- for (var i = 0; i < source.length; i++) {
- if (source[i]._id === id) {
- return source[i];
- }
- }
- throw "Couldn't find object with id: " + id;
- }
- var handler = function()
- {
- this.clear();
- };
- handler.prototype.clear = function()
- {
- this.json = {
- type : "FeatureCollection",
- features : []
- };
- this.layerStyles = {};
- };
- handler.prototype.addLayer = function(layerID, geoJSON, styleCallback, pointToLayerCallback, onEachFeatureCallback)
- {
- this.layerStyles[layerID] = styleCallback;
- this.pointToLayer = pointToLayerCallback;
- this.onEachFeature = onEachFeatureCallback;
- // tag features with their assigned layer
- geoJSON.features.forEach(function(feature, i) {
- feature.properties.__LAYERID__ = layerID;
- });
- // merge into current objects
- Array.prototype.push.apply(this.json.features, geoJSON.features);
- };
- handler.prototype.removeLayer = function(layerID)
- {
- var feats = this.json.features,
- i = 0, latlng = this.json.latlng;
- delete this.layerStyles[layerID];
- // remove relevant geoJSON objects as well
- for (; i < feats.length; ++i) {
- feature = feats[i];
- if (feature.properties.__LAYERID__ == layerID) {
- feats.splice(i, 1);
- --i;
- }
- }
- };
- var x2js = new X2JS();
- handler.prototype.__handleStyle = function(feature)
- {
- if (feature.properties['__LAYERID__'] === undefined) {
- return {};
- }
- console.log(this.pointToLayer(feature))
- return this.layerStyles[feature.properties.__LAYERID__](feature);
- }
- handler.prototype.__handlePointToLayer = function(feature, pointToLayer)
- {
- console.log( this.pointToLayer(feature));
- return this.pointToLayer(feature);
- }
- handler.prototype.__handleOnEachFeature = function(feature, pointToLayer)
- {
- return this.onEachFeature(feature);
- }
- handler.prototype.get = function($scope)
- {
- var self = this;
- return {
- data: this.json,
- style: function(feature) {
- return self.__handleStyle.call(self, feature);
- },
- /*hack below, works, want to be able to pass this in through prototype */
- pointToLayer: function(feature, latlng) {
- pointID = ((feature.properties.styleUrl).substring(1));
- var url = (findById(kmlHandler.iconStyles, pointID).IconStyle.Icon.href).substr(1);
- var smallIcon = new L.Icon({
- iconUrl: url,
- iconAnchor: [12, 41],
- popupAnchor: [1, -34],
- shadowSize: [41, 41]
- });
- return L.marker(latlng, {
- icon: smallIcon
- });
- },
- /*
- pointToLayer: function(feature) {
- console.log(self.__handlePointToLayer.call(self, feature));
- return self.__handlePointToLayer.call(self, feature);
- },*/
- onEachFeature: function (feature, layer) {
- //same deal here:
- //return self.__handleStyle.call(self, feature);
- layer.bindPopup('<div><h1>' + feature.properties.name + '</h1></div><div>' + feature.properties.description + '</div>');
- }
- }
- };
- return handler;
- }
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement