Skip to content

Instantly share code, notes, and snippets.

@zross
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zross/11184494 to your computer and use it in GitHub Desktop.
Save zross/11184494 to your computer and use it in GitHub Desktop.
Put topojson on LeafletJS map
<html>
<head>
<title>HTML5</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=650, user-scalable=yes">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<style>
html, body {
height: 100%;
margin: 0;
}
#mapcanvas{
height:100%;
}
</style>
<script>
var geoJsonObject;
$(document).ready(function(){
map = new L.Map('mapcanvas');
var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var osmAttrib='Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
var osm = new L.TileLayer(osmUrl, {attribution: osmAttrib});
// start the map in South-East England
map.setView(new L.LatLng(36.788283, -119.805891),6);
map.addLayer(osm);
//county.json is a topojson file
var myStyle = {
"color": "#003399",
"weight": 1,
"opacity": 0.65
};
$.getJSON("township.json", function(data){
geoJsonObject = topojson.feature(data, data.objects.township)
L.geoJson(geoJsonObject,{
style:myStyle
}).addTo(map);
});
});//end document ready
</script>
</head>
<body>
<div id="mapcanvas">
</div>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment