Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
TopoJSON-aware Leaflet layer
/*
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.
*/

Caged commented Jun 14, 2013

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?

Owner

rclark commented Jun 14, 2013

Wow -- you were on this before I even knew if it worked. Well it does! And now there's more license than code -- hurray!

kynan commented Sep 14, 2014

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.

bbss commented Sep 2, 2015

Additional info with similar approach: http://blog.webkid.io/maps-with-leaflet-and-topojson/

brendanvinson commented May 18, 2016 edited

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