Skip to content

Instantly share code, notes, and snippets.

@bollwyvl
Forked from emeeks/d3map.css
Created July 14, 2014 14:02
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 bollwyvl/d2a599d2a6e13250df93 to your computer and use it in GitHub Desktop.
Save bollwyvl/d2a599d2a6e13250df93 to your computer and use it in GitHub Desktop.

Using d3.carto.layer with d3.carto.map.

Each d3.carto.layer is defined and then added to the map using map.addCartoLayer. You can access the added layers using map.layers(). Each layer fires a "load" event once successfully added to the map.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>d3.carto.map - d3.carto.layer</title>
<meta charset="utf-8" />
<link type="text/css" rel="stylesheet" href="https://raw.githubusercontent.com/emeeks/d3-carto-map/master/d3map.css" />
<link type="text/css" rel="stylesheet" href="https://raw.githubusercontent.com/emeeks/d3-carto-map/master/examples/example.css" />
</head>
<style>
html,body {
height: 100%;
width: 100%;
margin: 0;
}
#map {
height: 100%;
width: 100%;
position: absolute;
}
</style>
<script>
function makeSomeMaps() {
map = d3.carto.map();
d3.select("#map").call(map);
tileLayer = d3.carto.layer();
tileLayer
.type("tile")
.path("examples.map-zgrqqx0w")
.label("Base")
.on("load", recenter);
geojsonLayer = d3.carto.layer();
geojsonLayer
.type("geojson")
.path("http://bl.ocks.org/emeeks/raw/c970c9ee3e242e90004b/world.geojson")
.label("GeoBorders")
.cssClass("countryborders")
.renderMode("canvas")
.on("load", createFeatureLayer);
topojsonLayer = d3.carto.layer();
topojsonLayer
.type("topojson")
.path("http://bl.ocks.org/emeeks/raw/c970c9ee3e242e90004b/sample_routes.topojson")
.label("TopoRoutes")
.cssClass("roads")
.renderMode("canvas")
.on("load", function() {console.log("load topojson")});
csvLayer = d3.carto.layer();
csvLayer
.type("csv")
.path("http://bl.ocks.org/emeeks/raw/c970c9ee3e242e90004b/sample_points.csv")
.label("CSV Points")
.cssClass("pinkcircle")
.renderMode("svg")
.markerSize(3)
.x("x")
.y("y")
.on("load", changeMarkers);
xyLayer = d3.carto.layer();
xyLayer
.type("xyarray")
.features([{x: 5, y: 40},{x: 5, y: 50}])
.label("XY Array")
.cssClass("greencircle")
.renderMode("svg")
.markerSize(30)
.x("x")
.y("y")
.on("load", function(){console.log("load xyarray")});
map.addCartoLayer(tileLayer);
map.addCartoLayer(topojsonLayer);
map.addCartoLayer(geojsonLayer);
map.addCartoLayer(csvLayer);
map.addCartoLayer(xyLayer);
function recenter() {
map.centerOn([-0.1275,51.507],"latlong",5000);
}
function changeMarkers() {
d3.selectAll("g.pinkcircle").select("circle").remove();
d3.selectAll("g.pinkcircle").datum({e: 0}).append("rect").attr("class", "bluesquare").attr("height", 5).attr("width",5).attr("x",-2.5).attr("y",-2.5)
// To get the graphical elements to scale you need to do this
map.refresh();
}
function createFeatureLayer() {
var featuresArray = [];
var mapLayers = map.layers();
mapLayers.forEach(function (layer) {
if (layer.label() == "GeoBorders") {
featuresArray = layer.features();
}
})
shortNameCountries = featuresArray.filter(function(d) {return d.properties.name.length < 7})
featureLayer = d3.carto.layer();
featureLayer
.type("featurearray")
.features(shortNameCountries)
.label("Feature Array")
.cssClass("halffilledcountries")
.renderMode("svg")
.on("load", function(){console.log("load featurearray")});
map.addCartoLayer(featureLayer);
}
}
</script>
<body onload="makeSomeMaps()">
<div id="map"></div>
<footer>
<script src="https://rawgit.com/emeeks/d3-carto-map/master/resources/d3.v3.min.js" type="text/javascript"></script>
<script src="https://rawgit.com/emeeks/d3-carto-map/master/resources/topojson.js" type="text/javascript">
</script>
<script src="https://rawgit.com/emeeks/d3-carto-map/master/resources/d3.geo.projection.min.js" type="text/javascript">
</script>
<script src="https://rawgit.com/emeeks/d3-carto-map/master/resources/tile.js" type="text/javascript">
</script>
<script src="https://rawgit.com/emeeks/d3-carto-map/master/resources/d3.quadtiles.js" type="text/javascript">
</script>
<script src="https://rawgit.com/emeeks/d3-carto-map/master/resources/d3.geo.raster.js" type="text/javascript">
</script>
<script src="https://rawgit.com/emeeks/d3-carto-map/master/d3.carto.map.js" type="text/javascript">
</script>
</footer>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment