Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add live map #227

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ description: >- # this means to ignore newlines until "baseurl:"
baseurl: "/" # the subpath of your site, e.g. /blog
url: "https://remotehack.space" # the base hostname & protocol for your site, e.g. http://example.com
discord_invite_url: https://discord.gg/wNq8uVvQT3


live_map_api: https://remotehack-livemap-api.jakew.workers.dev

# Build settings
plugins:
- jekyll-redirect-from
- jekyll-sitemap

timezone: Europe/London

# To ensure that events in the future generate their html pages
Expand Down
85 changes: 85 additions & 0 deletions _includes/livemap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<p id="map-text">
Loading...
</p>
<div id="map-container"></div>
<div id="map-tooltip" class="tooltip"></div>
<p>Join the Discord to add yourself to the map</p>

<script type="module">
import * as d3 from "https://cdn.jsdelivr.net/npm/d3@v7/+esm";
import escape from "https://cdn.jsdelivr.net/npm/escape-html/+esm";

const container = document.getElementById("map-container");
const text = document.getElementById("map-text");
const tooltip = d3.select("#map-tooltip");

const width = 640;
const height = 400;

// create a new svg for d3.js to draw on
const svg = d3.create("svg").attr("width", width).attr("height", height);
container.append(svg.node());

// grab the data
Promise.all([
d3.json("{{ site.live_map_api }}/locations.geojson"),
d3.json("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/world.geojson")
]).then(([pointData, worldData]) => {
// update map text
text.innerText = `${pointData.features.length} hackers online!`;

// get the bounds of locations (min & max coordinates)
const bounds = d3.geoBounds(pointData);
const [[x0, y0], [x1, y1]] = bounds;

const dx = x1 - x0;
const dy = y1 - y0;
const x = ((x0 + x1) / 2) || 0;
const y = ((y0 + y1) / 2) || 0;

// calculate scale and translation based on the bounds
const scale = Math.max(10, Math.min(dx / width, dy / height)) * 100;
const translate = [width / 2 - scale * x, height / 2 - scale * y];
const padding = 100;

// create a new projection, centered at the middle of the locations
let projection = d3.geoNaturalEarth1()
.center([x, y])
.translate([width / 2, height / 2]);

// if there's more than 1 location, fit the map to show all locations nicely
if (pointData.features.length > 1) {
projection = projection.fitExtent([[0 + padding, 0 + padding], [width - padding, height - padding]], pointData);
}

// clamp the map scaling to 2000 so countries can be seen still
projection = projection.scale(Math.min(projection.scale(), 2000));

const pathGenerator = d3.geoPath(projection).pointRadius(4);

// draw countries
svg.append("path")
.datum(worldData)
.attr("d", pathGenerator)
.attr("fill", "#e6e7e8")
.attr("stroke", "white");

// draw points for each hacker location
svg.selectAll("circle")
.data(pointData.features)
.enter()
.append("circle")
.attr("cx", (d) => projection(d.geometry.coordinates)[0])
.attr("cy", (d) => projection(d.geometry.coordinates)[1])
.attr("r", 4)
.attr("fill", "blue")
.on("mouseover", (event, d) => {
tooltip.style("display", "block")
.html(`<b>${escape(d.properties.name || "Anonymous")}</b><br>${escape(d.properties.locationName)}`)
.style("left", (event.pageX + 10) + "px")
.style("top", (event.pageY - 10) + "px");
}).on("mouseout", () => {
tooltip.style("display", "none");
});
});
</script>
8 changes: 8 additions & 0 deletions _sass/_style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,11 @@ ol {
}
}
}

.tooltip {
position: absolute;
background: var(--background-light);
color: var(--foreground);
padding: 5px;
display: none;
}
7 changes: 7 additions & 0 deletions pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
An online event where you can meet friends and work on something new
</p>

<section class="map">
<div>
<h2>Live Map</h2>
{% include livemap.html %}
</div>
</section>

<section class="📅">
<div>
<h2>Next event</h2>
Expand Down