2023-01-05 02:09:44 +00:00
|
|
|
import React, { useRef, useEffect, useState } from "react";
|
|
|
|
// @ts-ignore
|
|
|
|
// eslint-disable-next-line
|
|
|
|
// import mapboxgl from "!mapbox-gl";
|
|
|
|
import mapboxgl from "mapbox-gl";
|
|
|
|
import "./App.css";
|
2023-01-05 06:06:46 +00:00
|
|
|
import { mapOnLoad } from "./layers";
|
2023-01-05 02:09:44 +00:00
|
|
|
|
|
|
|
const MAPBOX_TOKEN =
|
|
|
|
"pk.eyJ1IjoiamFrZWMiLCJhIjoiY2tkaHplNGhjMDAyMDJybW4ybmRqbTBmMyJ9.AR_fnEuka8-cFb4Snp3upw";
|
|
|
|
|
|
|
|
mapboxgl.accessToken = MAPBOX_TOKEN;
|
|
|
|
|
|
|
|
export function Map() {
|
|
|
|
const mapContainer = React.useRef<HTMLDivElement>(null);
|
|
|
|
const mapRef = React.useRef<mapboxgl.Map | null>(null);
|
2023-01-05 06:06:46 +00:00
|
|
|
|
|
|
|
const [lng, setLng] = useState(151.21084276742022);
|
|
|
|
const [lat, setLat] = useState(-33.8720286260115);
|
|
|
|
const [zoom, setZoom] = useState(17);
|
2023-01-05 02:09:44 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// This is called on every pan
|
|
|
|
if (mapContainer.current === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mapRef.current !== null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mapRef.current = new mapboxgl.Map({
|
|
|
|
container: mapContainer.current,
|
2023-01-05 06:06:46 +00:00
|
|
|
style: "mapbox://styles/mapbox/streets-v12",
|
|
|
|
// style: "mapbox://styles/mapbox/dark-v10",
|
2023-01-05 02:09:44 +00:00
|
|
|
center: [lng, lat],
|
|
|
|
zoom: zoom,
|
|
|
|
});
|
|
|
|
|
2023-01-05 06:06:46 +00:00
|
|
|
const map = mapRef.current;
|
|
|
|
map.on("load", mapOnLoad(map));
|
2023-01-05 02:09:44 +00:00
|
|
|
|
|
|
|
map.addControl(new mapboxgl.NavigationControl());
|
|
|
|
map.addControl(new mapboxgl.FullscreenControl());
|
|
|
|
map.addControl(
|
|
|
|
new mapboxgl.GeolocateControl({
|
|
|
|
positionOptions: {
|
|
|
|
enableHighAccuracy: true,
|
|
|
|
},
|
|
|
|
trackUserLocation: true,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
map.on("move", () => {
|
|
|
|
if (!map) {
|
|
|
|
return; // wait for map to initialize
|
|
|
|
}
|
2023-01-05 06:06:46 +00:00
|
|
|
const { lng, lat } = map.getCenter();
|
|
|
|
const zoom = map.getZoom();
|
|
|
|
console.log(lng, lat, zoom);
|
2023-01-05 02:09:44 +00:00
|
|
|
|
|
|
|
setLng(map.getCenter().lng);
|
|
|
|
setLat(map.getCenter().lat);
|
|
|
|
setZoom(map.getZoom());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className="sidebar">
|
|
|
|
<label>
|
2023-01-05 06:06:46 +00:00
|
|
|
A work in progrss side project by{" "}
|
2023-01-05 02:09:44 +00:00
|
|
|
<a
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
href="https://jakecoppinger.com/"
|
|
|
|
>
|
|
|
|
Jake Coppinger
|
|
|
|
</a>{" "}
|
2023-01-05 06:06:46 +00:00
|
|
|
| Open source on{" "}
|
2023-01-05 02:09:44 +00:00
|
|
|
<a
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
href="https://github.com/jakecoppinger/safe-cycling-map"
|
|
|
|
>
|
|
|
|
Github
|
|
|
|
</a>
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<div ref={mapContainer} className="map-container" />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|