Last active
August 29, 2015 14:21
-
-
Save mapsense-examples/f1099c642a7a1ed9e71e to your computer and use it in GitHub Desktop.
Toggle Layers
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content='initial-scale=1,maximum-scale=1,user-scalable=no' /> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<script src="http://d3js.org/topojson.v1.min.js" charset="utf-8"></script> | |
<script src="https://developer.mapsense.co/mapsense.js" charset="utf-8"></script> | |
<link type="text/css" href="https://developer.mapsense.co/mapsense.css" rel="stylesheet"/> | |
<style> | |
html, body, #myMap{ | |
height: 100%; | |
width: 100%; | |
margin: 0; padding: 0; | |
font-family: sans-serif; | |
} | |
#myMap { | |
position: absolute; | |
top: 0; right: 0; bottom: 0; left: 0; | |
} | |
.top-left { | |
position: absolute; | |
top: 5px; | |
left: 5px; | |
background-color: rgba(255,255,255,0.8); | |
padding: 3px; | |
} | |
circle { | |
fill: rgba(68, 167, 228, 0.3); /*blue*/ | |
stroke: rgba(68, 167, 228, 1); /*blue*/ | |
} | |
</style> | |
</head> | |
<body> | |
<div id="myMap"></div> | |
<div class="top-left"> | |
<label><input type="checkbox" id="show_layer" class="" /> Layer</label> | |
<br> | |
<label><input type="checkbox" id="show_imagery" class="" /> Imagery</label> | |
</div> | |
<script> | |
var map = mapsense.map("#myMap") //tell it where to go | |
.add( mapsense.basemap().apiKey("key-2d5eacd8b924489c8ed5e8418bd883bc") ); | |
var url = "https://{S}-api.mapsense.co/universes/mapsense.farmers_markets/{Z}/{X}/{Y}.topojson?api-key=key-2d5eacd8b924489c8ed5e8418bd883bc"; | |
var layer = mapsense.topoJson() //init a topojson layer | |
.url(mapsense.url(url).hosts(['a', 'b', 'c', 'd'])) //tell it where to look | |
.selection(function(d){ //class each feature with its layer and sublayer, for css | |
d.attr("r", 7) | |
.attr("class", "mapFeatures"); | |
}) | |
.clip(false) | |
.scale("fixed") | |
imagery_url = "http://{S}.mqcdn.com/tiles/1.0.0/sat/{Z}/{X}/{Y}.jpg"; // Credit http://developer.mapquest.com/web/products/open/map | |
imagery_layer = mapsense.image() | |
.url(mapsense.url(imagery_url) | |
.hosts(["otile1", "otile2", "otile3", "otile4"])); | |
map.add(imagery_layer.visible(false).id("imagery_layer")); | |
map.add(layer.visible(false).id("layer")); | |
document.addEventListener("DOMContentLoaded",function(){ | |
// When dom ready, wire up the buttons | |
d3.selectAll('#show_imagery, #show_layer').on("click",function() { | |
if ( document.getElementById('show_imagery').checked ) { | |
imagery_layer.visible(true); | |
} else { | |
imagery_layer.visible(false); | |
} | |
if ( document.getElementById('show_layer').checked ) { | |
console.log(document.getElementById('show_layer').checked); | |
layer.visible(true); | |
} else { | |
layer.visible(false); | |
} | |
}); | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment