-
-
Save farleyknight/bc3fb99d133350497cfb12ea5da489b8 to your computer and use it in GitHub Desktop.
Google Maps + D3
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
license: gpl-3.0 |
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> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> | |
<style> | |
html, body, #map { | |
width: 100%; | |
height: 100%; | |
margin: 0; | |
padding: 0; | |
} | |
.stations, .stations svg { | |
position: absolute; | |
} | |
.stations svg { | |
width: 60px; | |
height: 20px; | |
padding-right: 100px; | |
font: 10px sans-serif; | |
} | |
.stations circle { | |
fill: brown; | |
stroke: black; | |
stroke-width: 1.5px; | |
} | |
</style> | |
<div id="map"></div> | |
<script src="//maps.google.com/maps/api/js?sensor=true"></script> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
// Create the Google Map… | |
var map = new google.maps.Map(d3.select("#map").node(), { | |
zoom: 8, | |
center: new google.maps.LatLng(37.76487, -122.41948), | |
mapTypeId: google.maps.MapTypeId.TERRAIN | |
}); | |
// Load the station data. When the data comes back, create an overlay. | |
d3.json("stations.json", function(error, data) { | |
if (error) throw error; | |
var overlay = new google.maps.OverlayView(); | |
// Add the container when the overlay is added to the map. | |
overlay.onAdd = function() { | |
var layer = d3.select(this.getPanes().overlayLayer).append("div") | |
.attr("class", "stations"); | |
// Draw each marker as a separate SVG element. | |
// We could use a single SVG, but what size would it have? | |
overlay.draw = function() { | |
var projection = this.getProjection(), | |
padding = 10; | |
var marker = layer.selectAll("svg") | |
.data(d3.entries(data)) | |
.each(transform) // update existing markers | |
.enter().append("svg") | |
.each(transform) | |
.attr("class", "marker"); | |
// Add a circle. | |
marker.append("circle") | |
.attr("r", 4.5) | |
.attr("cx", padding) | |
.attr("cy", padding); | |
// Add a label. | |
marker.append("text") | |
.attr("x", padding + 7) | |
.attr("y", padding) | |
.attr("dy", ".31em") | |
.text(function(d) { return d.key; }); | |
function transform(d) { | |
d = new google.maps.LatLng(d.value[1], d.value[0]); | |
d = projection.fromLatLngToDivPixel(d); | |
return d3.select(this) | |
.style("left", (d.x - padding) + "px") | |
.style("top", (d.y - padding) + "px"); | |
} | |
}; | |
}; | |
// Bind our overlay to the map… | |
overlay.setMap(map); | |
}); | |
</script> |
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
{ | |
"Boston": [ | |
-71.05, | |
42.36 | |
], | |
"New York": [ | |
-74.00, | |
40.71 | |
], | |
"Houston": [ | |
-95.36, | |
29.76 | |
], | |
"Chicago": [ | |
-87.62, | |
41.87 | |
], | |
"San Francisco": [ | |
-122.41, | |
37.77 | |
], | |
"Seattle": [ | |
-122.33, | |
47.60 | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment