Skip to content

Instantly share code, notes, and snippets.

@Andrew-Reid
Created September 11, 2017 01:43
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 Andrew-Reid/770a7ad0f49e2c0f6384f3144a4ef240 to your computer and use it in GitHub Desktop.
Save Andrew-Reid/770a7ad0f49e2c0f6384f3144a4ef240 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no'/>
<title>MapboxGL & D3.js - point</title>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.12.0/mapbox-gl.css' rel='stylesheet'/>
<style>
html, body, #wrapper {
width: 100%;
height: 100%;
padding: 0px;
margin: 0px;
}
#map {
position: relative;
width: 100%;
height: 100%;
margin: auto auto;
}
svg {
position: absolute;
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="map"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.12.0/mapbox-gl.js'></script>
<script>
d3.json("json.json", function (err, data) {
mapDraw(data);
})
function mapDraw(data) {
mapboxgl.accessToken = 'pk.eyJ1Ijoic2hpbWl6dSIsImEiOiI0cl85c2pNIn0.RefZMaOzNn-IistVe-Zcnw'
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/streets-v8',
center: [151, -33.85],
zoom: 8,
});
map.addControl(new mapboxgl.Navigation());
var container = map.getCanvasContainer()
var svg = d3.select(container).append("svg")
var circle = svg.selectAll("circle")
.data([data.features[0]])
.enter()
.append("circle");
setTimeout(function() {
function project(d) {
return map.project(new mapboxgl.LngLat(+d[0], +d[1]));
console.log(map.project(new mapboxgl.LngLat(+d[0], +d[1])))
}
var i = 0;
function drop() {
circle
.data([data.features[i%4]])
.attr({
"stroke": "black",
"stroke-width": 2,
"opacity": .7,
"fill": "red",
"r": 5
})
.attr("cx", function(d) {
return project(d.geometry.coordinates).x;
})
.attr("cy", function(d, i) {
return project(d.geometry.coordinates).y;
})
.transition()
.duration(2000)
.attr("r", function(d) {
console.log(d.properties.count)
return d.properties.count / 10
})
i++;
}
function update() {
circle
.attr({
cx: function(d) {
return project(d.geometry.coordinates).x;
},
cy: function(d) {
return project(d.geometry.coordinates).y;
},
})
}
map.on("viewreset", update)
map.on("move", update)
drop();
},3000)
}
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
},
"features": [
{
"type": "Feature",
"properties": {
"mode": "train",
"date": "20160730",
"tap": "on",
"time": "09:00",
"loc": "Town Hall Station",
"count": "321",
"lat": -33.8732664,
"lon": 151.2062183,
"coordinates": "151.2062183,-33.8732664"
},
"geometry": {
"type": "Point",
"coordinates": [151.206218299999989, -33.8732664]
}
},
{
"type": "Feature",
"properties": {
"mode": "train",
"date": "20160730",
"tap": "on",
"time": "10:00",
"loc": "Town Hall Station",
"count": "32",
"lat": -33.8732664,
"lon": 151.2062183,
"coordinates": "151.2062183,-33.8732664"
},
"geometry": {
"type": "Point",
"coordinates": [151.206218299999989, -33.8732664]
}
},
{
"type": "Feature",
"properties": {
"mode": "train",
"date": "20160730",
"tap": "on",
"time": "11:00",
"loc": "Town Hall Station",
"count": "350",
"lat": -33.8732664,
"lon": 151.2062183,
"coordinates": "151.2062183,-33.8732664"
},
"geometry": {
"type": "Point",
"coordinates": [151.206218299999989, -33.8732664]
}
},
{
"type": "Feature",
"properties": {
"mode": "train",
"date": "20160730",
"tap": "on",
"time": "12:00",
"loc": "Town Hall Station",
"count": "400",
"lat": -33.8732664,
"lon": 151.2062183,
"coordinates": "151.2062183,-33.8732664"
},
"geometry": {
"type": "Point",
"coordinates": [151.206218299999989, -33.8732664]
}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment