Skip to content

Instantly share code, notes, and snippets.

@kinjouj
Created June 26, 2026 20:16
Show Gist options
  • Select an option

  • Save kinjouj/cf0b14cd0c67aa4eb92fc8fe30fc732a to your computer and use it in GitHub Desktop.

Select an option

Save kinjouj/cf0b14cd0c67aa4eb92fc8fe30fc732a to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/openlocationcode/1.0.4/openlocationcode.min.js"></script>
<style>
#map {
height: 100vh;
width: 100%;
margin: 0;
}
body {
margin: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
const map = L.map("map");
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png").addTo(map);
function drawPlusCodeGrid(lat, lng) {
map.eachLayer(layer => {
if (layer instanceof L.Marker || layer instanceof L.Rectangle) {
map.removeLayer(layer);
}
});
map.setView([lat, lng], 16);
const userExactCode = OpenLocationCode.encode(lat, lng, 10);
const userAreaCode8 = OpenLocationCode.encode(lat, lng, 8);
const marker = L.marker([lat, lng]).addTo(map);
marker.bindTooltip(userExactCode, { permanent: true, direction: "top", offset: [0, -10] });
const centerInfo = OpenLocationCode.decode(userAreaCode8);
const centerLat = (centerInfo.latitudeLo + centerInfo.latitudeHi) / 2;
const centerLng = (centerInfo.longitudeLo + centerInfo.longitudeHi) / 2;
const step = centerInfo.latitudeHi - centerInfo.latitudeLo;
for (let dLat = 1; dLat >= -1; dLat--) {
for (let dLng = -1; dLng <= 1; dLng++) {
const nLat = centerLat + (dLat * step);
const nLng = centerLng + (dLng * step);
const neighborCode8 = OpenLocationCode.encode(nLat, nLng, 8);
const half = step / 2;
const nBounds = { southWest: [nLat - half, nLng - half], northEast: [nLat + half, nLng + half] };
L.rectangle([nBounds.southWest, nBounds.northEast], { weight: 2, fillColor: "#0066cc", fillOpacity: 0.1 })
.addTo(map)
.bindTooltip(neighborCode8, { permanent: true, direction: "center" });
}
}
}
map.on('locationfound', (e) => drawPlusCodeGrid(e.latlng.lat, e.latlng.lng));
map.locate({ setView: true, maxZoom: 16, enableHighAccuracy: true, timeout: 5000 });
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment