Last active
December 7, 2022 00:23
-
-
Save rclark/5779673 to your computer and use it in GitHub Desktop.
TopoJSON-aware Leaflet layer
This file contains 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
/* | |
You'll need something like this in your HTML: | |
<script src="http://d3js.org/topojson.v1.min.js"></script> | |
*/ | |
L.TopoJSON = L.GeoJSON.extend({ | |
addData: function(jsonData) { | |
if (jsonData.type === "Topology") { | |
for (key in jsonData.objects) { | |
geojson = topojson.feature(jsonData, jsonData.objects[key]); | |
L.GeoJSON.prototype.addData.call(this, geojson); | |
} | |
} | |
else { | |
L.GeoJSON.prototype.addData.call(this, jsonData); | |
} | |
} | |
}); | |
/* | |
The MIT License (MIT) | |
Copyright (c) 2013 Ryan Clark | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
Wow -- you were on this before I even knew if it worked. Well it does! And now there's more license than code -- hurray!
Any ideas how to make this work in combination with leaflet-ajax?
Thanks for this! You could add
return this;
to allow the same method chaining used in L.GeoJSON for full compatibility.
Additional info with similar approach: http://blog.webkid.io/maps-with-leaflet-and-topojson/
Forked and edited to be used the same as L.geoJson()
https://gist.github.com/brendanvinson/0e3c3c86d96863f1c33f55454705bca7
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! I just tested something similar myself a moment ago. I like the simplicity of your implementation. Is there any particular license you're releasing this under?