├── LICENSE.txt ├── README.md ├── example ├── index.html ├── layerconfig.js └── sample.geojson ├── package.json ├── sample.json └── src └── leaflet-layerconfig.js /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, NORKART AS 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 18 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Leaflet-LayerConfig 2 | =================== 3 | 4 | Library that communicates with a web service/reads a stored JavaScript object and adds layers to a leaflet map. 5 | 6 | 7 | 8 | ##Usage 9 | L.LayerConfig takes two arguments, the first one is either a URL or a object containing the information about the layers to add. A sample.json file is provided that shows some of the options available. 10 | The second argument is the map container or LayerGroup/FeatureGroup on which we add layers to. 11 | 12 | ``` 13 | var layerConfig = L.layerConfig(object or url to json file, map); 14 | ``` 15 | 16 | ##JSON notation 17 | The object containing the information is pretty basic, here is a sample file adding only a marker and setting the map view to a coordinate and a zoomlevel. You can also set bounds if you want to fit the map view to always show some objects. 18 | ``` 19 | { 20 | "center": [63.43048, 10.39508], 21 | "zoom": 14, 22 | "layers": [ 23 | { 24 | "type": "marker", 25 | "latLng": [63.43048, 10.39508], 26 | "popupContent": "Olavs statuen" 27 | } 28 | ] 29 | } 30 | ``` 31 | 32 | To pass options to the layer simply add a options key to the layer. The options passed to the layer is in the same format as the Leaflet options found in the reference. 33 | Some option keys support functions if you want to pass a function to Leaflet through a web service you need to put it in a single line string. A minifier can help with that. This is because JSON does not support functions or multiline strings. 34 | 35 | ###Other supported layer types: 36 | 37 | ####GeoJSON 38 | This can either take an URL or a GeoJSON object 39 | 40 | ``` 41 | { 42 | "type": "geojson", 43 | "url": "http://example.com/geojson-example.geojson", 44 | "options": { 45 | "style": "function () { return { color: \"red\" }; };" 46 | } 47 | 48 | } 49 | ``` 50 | 51 | ``` 52 | { 53 | "type": "geojson", 54 | "geojson": { 55 | "type": "FeatureCollection", 56 | "features": [ 57 | { 58 | "type": "Feature", 59 | "geometry": { 60 | "type": "Point", 61 | "coordinates": [102.0, 0.6] 62 | }, 63 | "properties": { 64 | "prop0": "value0" 65 | } 66 | } 67 | ] 68 | } 69 | 70 | } 71 | ``` 72 | ####TileLayer 73 | ``` 74 | { 75 | "type": "tilelayer", 76 | "url": "http://b.tiles.mapbox.com/v3/torbjornav.hhni5j5f/{z}/{x}/{y}.png", 77 | "options": { 78 | "attribution": "Mapbox" 79 | } 80 | } 81 | ``` 82 | ####WMS 83 | ``` 84 | { 85 | "type": "wms", 86 | "url": "URL-TO-WMS", 87 | "options": { 88 | //WMS OPTIONS GO HERE 89 | } 90 | } 91 | ``` 92 | ####PolyLine 93 | ``` 94 | { 95 | "type": "line", 96 | "path": [[63.43182, 10.39195],[63.42526, 10.3937]], 97 | "popupContent": "Prinsens gate" 98 | } 99 | ``` 100 | ####Polygon 101 | ``` 102 | { 103 | "type": "polygon", 104 | "path": [[[63.42639, 10.38975],[63.42564, 10.39006], 105 | [63.42570, 10.39194],[63.42643, 10.39183], 106 | [63.42639, 10.38975]]] 107 | } 108 | ``` 109 | ####Rectangle 110 | ``` 111 | { 112 | "type": "rectangle", 113 | "path": [[63.42647, 10.39694],[63.42554, 10.39508]], 114 | "options": { 115 | 116 | "color": "red" 117 | 118 | } 119 | } 120 | ``` 121 | ####Circle 122 | ``` 123 | { 124 | "type": "circle", 125 | "latLng": [63.43055, 10.39273], 126 | "radius": 10 127 | } 128 | ``` 129 | ####CircleMarker 130 | ``` 131 | { 132 | "type": "circlemarker", 133 | "latLng": [63.43034, 10.38647] 134 | } 135 | ``` 136 | 137 | ####FeatureGroup 138 | FeatureGroup has a "layer" key containing the layers to be added to it. This is in the same format as the top-level layer key. 139 | ``` 140 | { 141 | "type": "featuregroup", 142 | "layers": [ 143 | { 144 | "type": "marker", 145 | "latLng": [63.43027, 10.40094] 146 | }, 147 | { 148 | "type": "circle", 149 | "latLng": [63.43055, 10.39273], 150 | "radius": 10 151 | }, 152 | { 153 | "type": "circlemarker", 154 | "latLng": [63.43034, 10.38647] 155 | } 156 | 157 | ], 158 | "popupContent": "Kongens gate" 159 | } 160 | ``` 161 | ####LayerGroup 162 | LayerGroup has a "layer" key containing the layers to be added to it. This is in the same format as the top-level layer key. 163 | ``` 164 | { 165 | "type": "layergroup", 166 | "layers": [ 167 | { 168 | "type": "marker", 169 | "latLng": [63.43027, 10.40094] 170 | }, 171 | { 172 | "type": "circle", 173 | "latLng": [63.43055, 10.39273], 174 | "radius": 10 175 | }, 176 | { 177 | "type": "circlemarker", 178 | "latLng": [63.43034, 10.38647] 179 | } 180 | 181 | ] 182 | } 183 | ``` 184 | ###Events 185 | Leaflet-LayerConfig fires various events. 186 | 187 | | Name | Description | 188 | | ---- | ----------- | 189 | | startLayerLoading |Fired when the JSON object is read and we're ready to start loading layers | 190 | | stopLayerLoading | Fired when all layers has been loaded | 191 | | LayerLoaded | Fired when a layer has been added. Passes the Leaflet layer, type of layer and the name of the layer with the event object | 192 | | GroupLoadingStart | Fired when we start loading layers to a group. Passes the group to the layer | 193 | | GroupLoadingEnd | Fired when we have loaded all sub-layers to a group | 194 | 195 | -------------------------------------------------------------------------------- /example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Leaflet-layerconfig example 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 23 | 24 | 25 |
26 | 27 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /example/layerconfig.js: -------------------------------------------------------------------------------- 1 | var json = { 2 | "center": [63.43048, 10.39508], 3 | "zoom": 14, 4 | "layers": [{ 5 | "name": "marker", 6 | "type": "marker", 7 | "latLng": [63.43048, 10.39508], 8 | "popupContent": "Olavs statuen" 9 | }, { 10 | "type": "featuregroup", 11 | "name": "featuregroup", 12 | "layers": [{ 13 | "name": "submarker", 14 | "type": "marker", 15 | "latLng": [63.43027, 10.40094], 16 | }, { 17 | "name": "subcircle", 18 | "type": "circle", 19 | "latLng": [63.43055, 10.39273], 20 | "radius": 10 21 | }, { 22 | "name": "subcirclemarker", 23 | "type": "circlemarker", 24 | "latLng": [63.43034, 10.38647] 25 | } 26 | 27 | ], 28 | "popupContent": "Kongens gate" 29 | }, { 30 | "name": "Pink", 31 | "type": "tilelayer", 32 | "url": "http://{s}.tiles.mapbox.com/v3/torbjornav.hhni5j5f/{z}/{x}/{y}.png", 33 | "options": { 34 | "attribution": "Mapbox" 35 | }, 36 | "baseLayer": true 37 | }, { 38 | "name": "KuGIS", 39 | "type": "tilelayer", 40 | "url": "http://{s}.tiles.mapbox.com/v3/torbjornav.map-d9hhchjc/{z}/{x}/{y}.png", 41 | "options": { 42 | "attribution": "Mapbox" 43 | }, 44 | "baseLayer": true 45 | }, { 46 | "name": "Purple", 47 | "type": "tilelayer", 48 | "url": "http://{s}.tiles.mapbox.com/v3/torbjornav.hcp57k2g/{z}/{x}/{y}.png", 49 | "options": { 50 | "attribution": "Mapbox" 51 | }, 52 | "baseLayer": true 53 | }, { 54 | "name": "line", 55 | "type": "line", 56 | "path": [ 57 | [63.43182, 10.39195], 58 | [63.42526, 10.3937] 59 | ], 60 | "popupContent": "Prinsens gate" 61 | }, { 62 | "name": "polygon", 63 | "type": "polygon", 64 | "path": [ 65 | [ 66 | [63.42639, 10.38975], 67 | [63.42564, 10.39006], 68 | [63.42570, 10.39194], 69 | [63.42643, 10.39183], 70 | [63.42639, 10.38975] 71 | ] 72 | ] 73 | }, { 74 | "name": "rectangle", 75 | "type": "rectangle", 76 | "path": [ 77 | [63.42647, 10.39694], 78 | [63.42554, 10.39508] 79 | ], 80 | "options": { 81 | 82 | "color": "red" 83 | 84 | } 85 | }, { 86 | "name": "geojson testlayer", 87 | "type": "geojson", 88 | "geojson": { 89 | "type": "FeatureCollection", 90 | "features": [{ 91 | "type": "Feature", 92 | "geometry": { 93 | "type": "Point", 94 | "coordinates": [102.0, 0.6] 95 | }, 96 | "properties": { 97 | "prop0": "value0" 98 | } 99 | }, { 100 | "type": "Feature", 101 | "geometry": { 102 | "type": "LineString", 103 | "coordinates": [ 104 | [102.0, 0.0], 105 | [103.0, 1.0], 106 | [104.0, 0.0], 107 | [105.0, 1.0] 108 | ] 109 | }, 110 | "properties": { 111 | "prop1": 0.0, 112 | "prop0": "value0" 113 | } 114 | }, { 115 | "type": "Feature", 116 | "geometry": { 117 | "type": "Polygon", 118 | "coordinates": [ 119 | [ 120 | [100.0, 0.0], 121 | [101.0, 0.0], 122 | [101.0, 1.0], 123 | [100.0, 1.0], 124 | [100.0, 0.0] 125 | ] 126 | ] 127 | }, 128 | "properties": { 129 | "prop1": { 130 | "this": "that" 131 | }, 132 | "prop0": "value0" 133 | } 134 | }] 135 | } 136 | 137 | },{ 138 | "name": "geojson testfile", 139 | "type": "geojson", 140 | "url": "https://gist.githubusercontent.com/anonymous/6ef989b1fc43c6ac6ddf/raw/92f9f07ccbafe57091bab39dc305856bded80984/map.geojson", 141 | "options": { 142 | "style": "function () { return { color: \"red\" }; };" 143 | } 144 | } 145 | 146 | 147 | 148 | 149 | ] 150 | 151 | 152 | 153 | }; -------------------------------------------------------------------------------- /example/sample.geojson: -------------------------------------------------------------------------------- 1 | { 2 | "type": "FeatureCollection", 3 | "features": [ 4 | { 5 | "type": "Feature", 6 | "properties": {}, 7 | "geometry": { 8 | "type": "LineString", 9 | "coordinates": [ 10 | [ 11 | 10.391349792480469, 12 | 63.43393125840144 13 | ], 14 | [ 15 | 10.367317199707031, 16 | 63.434391886868774 17 | ], 18 | [ 19 | 10.36285400390625, 20 | 63.41811185480355 21 | ], 22 | [ 23 | 10.393409729003906, 24 | 63.41488546774787 25 | ], 26 | [ 27 | 10.39306640625, 28 | 63.42210592624859 29 | ], 30 | [ 31 | 10.3765869140625, 32 | 63.4225667448372 33 | ] 34 | ] 35 | } 36 | }, 37 | { 38 | "type": "Feature", 39 | "properties": {}, 40 | "geometry": { 41 | "type": "LineString", 42 | "coordinates": [ 43 | [ 44 | 10.399932861328125, 45 | 63.4225667448372 46 | ], 47 | [ 48 | 10.40130615234375, 49 | 63.41580732966916 50 | ], 51 | [ 52 | 10.417098999023438, 53 | 63.41580732966916 54 | ], 55 | [ 56 | 10.401649475097656, 57 | 63.41672916195147 58 | ], 59 | [ 60 | 10.400962829589844, 61 | 63.419340859135154 62 | ], 63 | [ 64 | 10.407829284667969, 65 | 63.419494480972034 66 | ], 67 | [ 68 | 10.400619506835938, 69 | 63.420416194705815 70 | ], 71 | [ 72 | 10.401649475097656, 73 | 63.42287395311381 74 | ], 75 | [ 76 | 10.410919189453125, 77 | 63.42302755601735 78 | ] 79 | ] 80 | } 81 | }, 82 | { 83 | "type": "Feature", 84 | "properties": {}, 85 | "geometry": { 86 | "type": "Polygon", 87 | "coordinates": [ 88 | [ 89 | [ 90 | 10.416069030761719, 91 | 63.4155000456553 92 | ], 93 | [ 94 | 10.416069030761719, 95 | 63.42302755601735 96 | ], 97 | [ 98 | 10.435295104980469, 99 | 63.42302755601735 100 | ], 101 | [ 102 | 10.435295104980469, 103 | 63.4155000456553 104 | ], 105 | [ 106 | 10.416069030761719, 107 | 63.4155000456553 108 | ] 109 | ] 110 | ] 111 | } 112 | }, 113 | { 114 | "type": "Feature", 115 | "properties": {}, 116 | "geometry": { 117 | "type": "LineString", 118 | "coordinates": [ 119 | [ 120 | 10.45074462890625, 121 | 63.43531312158517 122 | ], 123 | [ 124 | 10.454521179199219, 125 | 63.416575525295976 126 | ], 127 | [ 128 | 10.440101623535156, 129 | 63.41611461038977 130 | ], 131 | [ 132 | 10.439758300781248, 133 | 63.41918723647505 134 | ] 135 | ] 136 | } 137 | }, 138 | { 139 | "type": "Feature", 140 | "properties": {}, 141 | "geometry": { 142 | "type": "LineString", 143 | "coordinates": [ 144 | [ 145 | 10.48919677734375, 146 | 63.43454542804542 147 | ], 148 | [ 149 | 10.458984375, 150 | 63.434238344869236 151 | ], 152 | [ 153 | 10.466880798339844, 154 | 63.427174523274594 155 | ], 156 | [ 157 | 10.489883422851562, 158 | 63.42533150079501 159 | ], 160 | [ 161 | 10.472373962402344, 162 | 63.417497332879755 163 | ], 164 | [ 165 | 10.460357666015625, 166 | 63.417497332879755 167 | ] 168 | ] 169 | } 170 | } 171 | ] 172 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "leaflet-layerconfig", 3 | "version": "0.0.1", 4 | "author": "Norkart AS", 5 | "description": "A plugin for Leaflet that provides an easy way of configuring the setup of a map from a JSON configuration.", 6 | "license": "BSD-2-Clause", 7 | "readmeFilename": "README.md", 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/Norkart/Leaflet-LayerConfig.git" 11 | }, 12 | "homepage": "https://github.com/Norkart/Leaflet-LayerConfig", 13 | "bugs": { 14 | "url": "https://github.com/Norkart/Leaflet-LayerConfig/issues" 15 | }, 16 | "dependencies": { 17 | "leaflet": ">=0.5.0" 18 | }, 19 | "keywords": [ 20 | "maps", 21 | "leaflet", 22 | "client", 23 | "layerconfig" 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /sample.json: -------------------------------------------------------------------------------- 1 | { 2 | 3 | "layers": [ 4 | { 5 | "type": "marker", 6 | "latLng": [63.43048, 10.39508], 7 | "popupContent": "Olavs statuen" 8 | }, 9 | { 10 | "type": "featuregroup", 11 | "layers": [ 12 | { 13 | "type": "marker", 14 | "latLng": [63.43027, 10.40094] 15 | }, 16 | { 17 | "type": "circle", 18 | "latLng": [63.43055, 10.39273], 19 | "radius": 10 20 | }, 21 | { 22 | "type": "circlemarker", 23 | "latLng": [63.43034, 10.38647] 24 | } 25 | 26 | ], 27 | "popupContent": "Kongens gate" 28 | }, 29 | { 30 | "type": "tilelayer", 31 | "url": "URL-TO-TILELAYER", 32 | "options": { 33 | "attribution": "Public domain" 34 | } 35 | }, 36 | { 37 | "type": "line", 38 | "path": [[63.43182, 10.39195],[63.42526, 10.3937]], 39 | "popupContent": "Prinsens gate" 40 | }, 41 | { 42 | "type": "polygon", 43 | "path": [[[63.42639, 10.38975],[63.42564, 10.39006],[63.42570, 10.39194],[63.42643, 10.39183],[63.42639, 10.38975]]] 44 | }, 45 | { 46 | "type": "rectangle", 47 | "path": [[63.42647, 10.39694],[63.42554, 10.39508]], 48 | "options": { 49 | 50 | "color": "red" 51 | 52 | } 53 | }, 54 | { 55 | "type": "geojson", 56 | "url": "URL-TO-GEOJSON-SERVICE" 57 | }, 58 | { 59 | "type": "geojson", 60 | "geojson": { 61 | "type": "FeatureCollection", 62 | "features": [ 63 | { 64 | "type": "Feature", 65 | "geometry": { 66 | "type": "Point", 67 | "coordinates": [102.0, 0.6] 68 | }, 69 | "properties": { 70 | "prop0": "value0" 71 | } 72 | }, 73 | { 74 | "type": "Feature", 75 | "geometry": { 76 | "type": "LineString", 77 | "coordinates": [ 78 | [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0] 79 | ] 80 | }, 81 | "properties": { 82 | "prop1": 0.0, 83 | "prop0": "value0" 84 | } 85 | }, 86 | { 87 | "type": "Feature", 88 | "geometry": { 89 | "type": "Polygon", 90 | "coordinates": [ 91 | [ 92 | [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], 93 | [100.0, 0.0] 94 | ] 95 | ] 96 | }, 97 | "properties": { 98 | "prop1": { 99 | "this": "that" 100 | }, 101 | "prop0": "value0" 102 | } 103 | } 104 | ] 105 | } 106 | 107 | } 108 | 109 | 110 | 111 | 112 | ] 113 | 114 | 115 | 116 | } -------------------------------------------------------------------------------- /src/leaflet-layerconfig.js: -------------------------------------------------------------------------------- 1 | /* 2 | leaflet-layerconfig 3 | Library that communicates with a web service/reads a stored JavaScript object and adds layers to a leaflet map. 4 | Copyright 2014 Norkart AS 5 | */ 6 | 7 | /*global L: false, ActiveXObject: false, window: false, XMLHttpRequest: false*/ 8 | 9 | 10 | (function () { 11 | 'use strict'; 12 | 13 | function ajaxRequest(url, callback, onerror) { 14 | var httpRequest; 15 | 16 | var alertContents = function () { 17 | if (httpRequest.readyState === 4) { 18 | if (httpRequest.status === 200) { 19 | callback(httpRequest.responseText); 20 | } else if (onerror) { 21 | onerror(httpRequest); 22 | } 23 | } 24 | }; 25 | 26 | var makeRequest = function (url) { 27 | if (window.XMLHttpRequest) { // Mozilla, Safari, ... 28 | httpRequest = new XMLHttpRequest(); 29 | } else if (window.ActiveXObject) { // IE 30 | try { 31 | httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 32 | } catch (e) { 33 | try { 34 | httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 35 | } catch (ignore) { 36 | 37 | } 38 | } 39 | } 40 | 41 | if (!httpRequest) { 42 | throw new Error('Giving up :( Cannot create an XMLHTTP instance'); 43 | } 44 | httpRequest.onreadystatechange = alertContents; 45 | httpRequest.open('GET', url); 46 | httpRequest.send(); 47 | }; 48 | makeRequest(url); 49 | } 50 | 51 | function existsAndIsString(value) { 52 | return !!(value && typeof value === "string"); 53 | } 54 | 55 | L.LayerConfig = L.Evented.extend({ 56 | 57 | _map: null, 58 | 59 | _json: null, 60 | 61 | initialize: function (map) { 62 | this._map = map; 63 | }, 64 | 65 | parse: function (json) { 66 | this._json = json; 67 | 68 | if (typeof json === "string") { // We take a string to mean we got an url; 69 | var self = this; 70 | var success = function (data) { 71 | var parsedJson = JSON.parse(data); 72 | if (self._map.setView && parsedJson.center && parsedJson.zoom) { 73 | self._map.setView(parsedJson.center, parsedJson.zoom); 74 | } 75 | self.addLayers(parsedJson); 76 | }; 77 | ajaxRequest(json, success); 78 | } else { 79 | if (this._map.setView && json.center && json.zoom) { 80 | this._map.setView(json.center, json.zoom); 81 | } 82 | if (this._map.fitBounds && json.bounds) { 83 | this._map.fitBounds(json.bounds); 84 | } 85 | this.addLayers(json); 86 | } 87 | }, 88 | 89 | _evalJsonOptions: function (options) { 90 | /*jslint evil: true */ 91 | if (options) { 92 | if (existsAndIsString(options.style)) { 93 | eval("options.style = " + options.style); 94 | } 95 | if (existsAndIsString(options.onEachFeature)) { 96 | eval("options.onEachFeature = " + options.onEachFeature); 97 | } 98 | if (existsAndIsString(options.filter)) { 99 | eval("options.filter = " + options.filter); 100 | } 101 | if (existsAndIsString(options.pointToLayer)) { 102 | eval("options.pointToLayer = " + options.pointToLayer); 103 | } 104 | if (existsAndIsString(options.coordsToLatLng)) { 105 | eval("options.coordsToLatLng = " + options.coordsToLatLng); 106 | } 107 | } 108 | /*jslint evil: false */ 109 | return options; 110 | }, 111 | 112 | _addLayer: function (layer, json, addTo) { 113 | if (json.baseLayer && this._addedBaseLayer) { 114 | 115 | } else if (!json.baseLayer) { 116 | addTo.addLayer(layer); 117 | } else { 118 | this._addedBaseLayer = true; 119 | addTo.addLayer(layer); 120 | if (layer.bringToFront) { 121 | layer.bringToFront(); 122 | } 123 | } 124 | this.fire("LayerLoaded", { 125 | type: json.type, 126 | name: json.name, 127 | layer: layer, 128 | baseLayer: json.baseLayer 129 | }); 130 | }, 131 | 132 | addLayers: function (json, addTo) { 133 | if (!addTo) { 134 | addTo = this._map; 135 | } 136 | this.fire("startLayerLoading"); 137 | var i; 138 | var layer; 139 | for (i = 0; i < json.layers.length; i++) { 140 | layer = json.layers[i]; 141 | switch (layer.type) { 142 | case 'marker': 143 | this.addMarker(layer, addTo); 144 | break; 145 | case 'geojson': 146 | this.addGeoJson(layer, addTo); 147 | break; 148 | case 'layergroup': 149 | this.addLayerGroup(layer, addTo); 150 | break; 151 | case 'featuregroup': 152 | this.addFeatureGroup(layer, addTo); 153 | break; 154 | case 'tilelayer': 155 | this.addTileLayer(layer, addTo); 156 | break; 157 | case 'wms': 158 | this.addWMS(layer, addTo); 159 | break; 160 | case 'circlemarker': 161 | this.addCircleMarker(layer, addTo); 162 | break; 163 | case 'circle': 164 | this.addCircle(layer, addTo); 165 | break; 166 | case 'rectangle': 167 | this.addRectangle(layer, addTo); 168 | break; 169 | case 'polygon': 170 | this.addPolygon(layer, addTo); 171 | break; 172 | case 'line': 173 | this.addLine(layer, addTo); 174 | break; 175 | case 'layer': 176 | this.addLayer(layer, addTo); 177 | break; 178 | } 179 | } 180 | this.fire("stopLayerLoading"); 181 | return this; 182 | }, 183 | 184 | addMarker: function (json, addTo) { 185 | json.options = this._evalJsonOptions(json.options); 186 | var l = L.marker(json.latLng, json.options); 187 | if (json.popupContent) { 188 | l.bindPopup(json.popupContent); 189 | } 190 | this._addLayer(l, json, addTo); 191 | return this; 192 | }, 193 | 194 | addGeoJson: function (json, addTo) { 195 | json.options = this._evalJsonOptions(json.options); 196 | var parent = this; 197 | var addGeoJSON = function (geojson, options) { 198 | var l = L.geoJson(geojson, options); 199 | addTo.addLayer(l); 200 | parent._addLayer(l, json, addTo); 201 | }; 202 | function onsuccess(data) { 203 | addGeoJSON(JSON.parse(data), json.options, parent._map); 204 | } 205 | function onerror(data) { 206 | console.debug(data); 207 | } 208 | if (json.url) { 209 | ajaxRequest(json.url, onsuccess, onerror); 210 | } else if (json.geojson) { 211 | addGeoJSON(json.geojson, json.options, this._map); 212 | } else { 213 | throw new Error("URL or GeoJSON object expected"); 214 | } 215 | return this; 216 | }, 217 | 218 | addLayerGroup: function (json, addTo) { 219 | json.options = this._evalJsonOptions(json.options); 220 | var l = L.layerGroup(); 221 | this.fire("GroupLoadingStart", {layer: l}); 222 | this.addLayers(json, l); 223 | this.fire("GroupLoadingEnd", {layer: l}); 224 | this._addLayer(l, json, addTo); 225 | return this; 226 | }, 227 | 228 | addFeatureGroup: function (json, addTo) { 229 | json.options = this._evalJsonOptions(json.options); 230 | var l = L.featureGroup(); 231 | this.fire("GroupLoadingStart", {layer: l}); 232 | this.addLayers(json, l); 233 | this.fire("GroupLoadingEnd", {layer: l}); 234 | if (json.popupContent) { 235 | l.bindPopup(json.popupContent); 236 | } 237 | this._addLayer(l, json, addTo); 238 | return this; 239 | }, 240 | 241 | addTileLayer: function (json, addTo) { 242 | json.options = this._evalJsonOptions(json.options); 243 | var l = L.tileLayer(json.url, json.options); 244 | this._addLayer(l, json, addTo); 245 | return this; 246 | }, 247 | 248 | addWMS: function (json, addTo) { 249 | json.options = this._evalJsonOptions(json.options); 250 | var l = L.tileLayer.wms(json.url, json.options); 251 | this._addLayer(l, json, addTo); 252 | return this; 253 | }, 254 | 255 | addLayerAndPopup: function (l, json, addTo) { 256 | if (json.popupContent) { 257 | l.bindPopup(json.popupContent); 258 | } 259 | this._addLayer(l, json, addTo); 260 | }, 261 | 262 | addCircleMarker: function (json, addTo) { 263 | json.options = this._evalJsonOptions(json.options); 264 | var l = L.circleMarker(json.latLng, json.options); 265 | this.addLayerAndPopup(l, json, addTo); 266 | return this; 267 | }, 268 | 269 | addCircle: function (json, addTo) { 270 | json.options = this._evalJsonOptions(json.options); 271 | var l = L.circle(json.latLng, json.radius, json.options); 272 | this.addLayerAndPopup(l, json, addTo); 273 | return this; 274 | }, 275 | 276 | addRectangle: function (json, addTo) { 277 | json.options = this._evalJsonOptions(json.options); 278 | var l = L.rectangle(json.path, json.options); 279 | this.addLayerAndPopup(l, json, addTo); 280 | return this; 281 | }, 282 | 283 | addPolygon: function (json, addTo) { 284 | json.options = this._evalJsonOptions(json.options); 285 | var l = L.polygon(json.path, json.options); 286 | this.addLayerAndPopup(l, json, addTo); 287 | return this; 288 | }, 289 | 290 | addLine: function (json, addTo) { 291 | json.options = this._evalJsonOptions(json.options); 292 | var l = L.polyline(json.path, json.options); 293 | this.addLayerAndPopup(l, json, addTo); 294 | return this; 295 | }, 296 | 297 | addLayer: function (json, addTo) { 298 | json.options = this._evalJsonOptions(json.options); 299 | var l = json.layer; 300 | this.addLayerAndPopup(l, json, addTo); 301 | return this; 302 | } 303 | }); 304 | 305 | L.layerConfig = function (json, map) { 306 | return new L.LayerConfig(json, map); 307 | }; 308 | 309 | }()); 310 | --------------------------------------------------------------------------------