This is a D3 (v4) based animated orthographic globe which alows you to plot markers from a given set of geo coordinates. This solution also allows you to set spinning speed and tilt shift parameter in a config object.
Last active
November 12, 2024 14:42
-
-
Save atanumallick/8d18989cd538c72ae1ead1c3b18d7b54 to your computer and use it in GitHub Desktop.
D3 Rotating Globe with Location Marker
This file contains 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> | |
<body> | |
<svg></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://d3js.org/topojson.v1.min.js"></script> | |
<script> | |
const width = 960; | |
const height = 500; | |
const config = { | |
speed: 0.005, | |
verticalTilt: -30, | |
horizontalTilt: 0 | |
} | |
let locations = []; | |
const svg = d3.select('svg') | |
.attr('width', width).attr('height', height); | |
const markerGroup = svg.append('g'); | |
const projection = d3.geoOrthographic(); | |
const initialScale = projection.scale(); | |
const path = d3.geoPath().projection(projection); | |
const center = [width/2, height/2]; | |
drawGlobe(); | |
drawGraticule(); | |
enableRotation(); | |
function drawGlobe() { | |
d3.queue() | |
.defer(d3.json, 'https://gist.githubusercontent.com/mbostock/4090846/raw/d534aba169207548a8a3d670c9c2cc719ff05c47/world-110m.json') | |
.defer(d3.json, 'locations.json') | |
.await((error, worldData, locationData) => { | |
svg.selectAll(".segment") | |
.data(topojson.feature(worldData, worldData.objects.countries).features) | |
.enter().append("path") | |
.attr("class", "segment") | |
.attr("d", path) | |
.style("stroke", "#888") | |
.style("stroke-width", "1px") | |
.style("fill", (d, i) => '#e5e5e5') | |
.style("opacity", ".6"); | |
locations = locationData; | |
drawMarkers(); | |
}); | |
} | |
function drawGraticule() { | |
const graticule = d3.geoGraticule() | |
.step([10, 10]); | |
svg.append("path") | |
.datum(graticule) | |
.attr("class", "graticule") | |
.attr("d", path) | |
.style("fill", "#fff") | |
.style("stroke", "#ccc"); | |
} | |
function enableRotation() { | |
d3.timer(function (elapsed) { | |
projection.rotate([config.speed * elapsed - 120, config.verticalTilt, config.horizontalTilt]); | |
svg.selectAll("path").attr("d", path); | |
drawMarkers(); | |
}); | |
} | |
function drawMarkers() { | |
const markers = markerGroup.selectAll('circle') | |
.data(locations); | |
markers | |
.enter() | |
.append('circle') | |
.merge(markers) | |
.attr('cx', d => projection([d.longitude, d.latitude])[0]) | |
.attr('cy', d => projection([d.longitude, d.latitude])[1]) | |
.attr('fill', d => { | |
const coordinate = [d.longitude, d.latitude]; | |
gdistance = d3.geoDistance(coordinate, projection.invert(center)); | |
return gdistance > 1.57 ? 'none' : 'steelblue'; | |
}) | |
.attr('r', 7); | |
markerGroup.each(function () { | |
this.parentNode.appendChild(this); | |
}); | |
} | |
</script> | |
</body> | |
</html> |
This file contains 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
MIT License | |
Copyright (c) 2018-2022 Atanu Mallick | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
This file contains 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
[ | |
{"latitude": 22, "longitude": 88}, | |
{"latitude": 12.61315, "longitude": 38.37723}, | |
{"latitude": -30, "longitude": -58}, | |
{"latitude": -14.270972, "longitude": -170.132217}, | |
{"latitude": 28.033886, "longitude": 1.659626}, | |
{"latitude": 40.463667, "longitude": -3.74922}, | |
{"latitude": 35.907757, "longitude": 127.766922}, | |
{"latitude": 23.634501, "longitude": -102.552784} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment