Created
May 31, 2013 13:50
-
-
Save taclab/5685115 to your computer and use it in GitHub Desktop.
layer To Geometry - leaflet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var layerToGeometry = function(layer) { | |
| var json, type, latlng, latlngs = [], i; | |
| if (L.Marker && (layer instanceof L.Marker)) { | |
| type = 'Point'; | |
| latlng = LatLngToCoords(layer._latlng); | |
| return JSON.stringify({"type": type, "coordinates": latlng}); | |
| } else if (L.Polygon && (layer instanceof L.Polygon)) { | |
| type = 'Polygon'; | |
| latlngs = LatLngsToCoords(layer._latlngs, 1); | |
| return JSON.stringify({"type": type, "coordinates": [latlngs]}); | |
| } else if (L.Polyline && (layer instanceof L.Polyline)) { | |
| type = 'LineString'; | |
| latlngs = LatLngsToCoords(layer._latlngs); | |
| return JSON.stringify({"type": type, "coordinates": latlngs}); | |
| } | |
| } | |
| var LatLngToCoords = function (LatLng, reverse) { // (LatLng, Boolean) -> Array | |
| var lat = parseFloat(reverse ? LatLng.lng : LatLng.lat), | |
| lng = parseFloat(reverse ? LatLng.lat : LatLng.lng); | |
| return [lng,lat]; | |
| } | |
| var LatLngsToCoords = function (LatLngs, levelsDeep, reverse) { // (LatLngs, Number, Boolean) -> Array | |
| var coord, | |
| coords = [], | |
| i, len; | |
| for (i = 0, len = LatLngs.length; i < len; i++) { | |
| coord = levelsDeep ? | |
| LatLngToCoords(LatLngs[i], levelsDeep - 1, reverse) : | |
| LatLngToCoords(LatLngs[i], reverse); | |
| coords.push(coord); | |
| } | |
| return coords; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment