"use client"; import { useEffect, useRef, useState } from "react"; import mapboxgl from "mapbox-gl"; import "mapbox-gl/dist/mapbox-gl.css"; import { Button } from "@react-email/components"; import { IconMapPin } from "@tabler/icons-react"; // You should store this in an environment variable const MAPBOX_ACCESS_TOKEN = process.env.SIGAP_MAPBOX_ACCESS_TOKEN || "pk.eyJ1IjoiZGl5b2FuZ2dhcmEiLCJhIjoiY203ZG5rcjhzMDA4djJqcXpzMXpoZzh6cSJ9.ZMiOrYWSYsabmZp3lnI5xw"; mapboxgl.accessToken = MAPBOX_ACCESS_TOKEN; export const MapboxMap = () => { const mapContainer = useRef(null); const map = useRef(null); const [lng, setLng] = useState(113.7025); const [lat, setLat] = useState(-8.1725); const [zoom, setZoom] = useState(10); useEffect(() => { if (map.current) return; // initialize map only once if (!mapContainer.current) return; // wait for map container to be ready // Set the bounds to Kabupaten Jember const bounds: mapboxgl.LngLatBoundsLike = [ [113.0, -8.5], // Southwest coordinates [114.0, -7.5], // Northeast coordinates ]; map.current = new mapboxgl.Map({ container: mapContainer.current, style: "mapbox://styles/mapbox/streets-v12", center: [lng, lat], zoom: zoom, }); // Add zoom and rotation controls to the map (top-right corner) map.current.addControl(new mapboxgl.NavigationControl(), "top-right"); // Add fullscreen control to the map (top-left corner) map.current.addControl(new mapboxgl.FullscreenControl(), "top-left"); // Add control to focus on Jember (top-left corner) // const focusButton = document.createElement("button"); // focusButton.className = "mapboxgl-ctrl-icon"; // focusButton.title = "Focus on Jember"; // focusButton.innerHTML = ` // // // // `; // focusButton.onclick = () => { // focusButton; // }; // map.current.addControl( // { // onAdd: () => focusButton, // onRemove: () => focusButton.remove(), // }, // "top-left" // ); map.current.on("move", () => { if (!map.current) return; setLng(Number(map.current.getCenter().lng.toFixed(4))); setLat(Number(map.current.getCenter().lat.toFixed(4))); setZoom(Number(map.current.getZoom().toFixed(2))); }); return () => { if (map.current) { map.current.remove(); } }; }, []); const focusOnJember = () => { if (map.current) { const bounds: mapboxgl.LngLatBoundsLike = [ [113.0, -8.5], // Southwest coordinates [114.0, -7.5], // Northeast coordinates ]; map.current.fitBounds(bounds, { padding: 20 }); } }; return (
Longitude: {lng} | Latitude: {lat} | Zoom: {zoom}
{/* */}
); };