import {
Map,
MapLocateControl,
MapTileLayer,
} from "@/components/ui/map"
import type { LatLngExpression } from "leaflet"
export function MapWithLocateControl() {
const TORONTO_COORDINATES = [43.6532, -79.3832] satisfies LatLngExpression
return (
<Map center={TORONTO_COORDINATES}>
<MapTileLayer />
<MapLocateControl />
</Map>
)
}
Optional Callbacks
Pass optional callbacks to get the location and handle any errors.
"use client"
import {
Map,
MapLocateControl,
MapPopup,
MapTileLayer,
} from "@/components/ui/map"
import type { LatLngExpression } from "leaflet"
import { useState } from "react"
import { toast } from "sonner"
export function MapWithLocateControlCallbacks() {
const TORONTO_COORDINATES = [43.6532, -79.3832] satisfies LatLngExpression
const [myCoordinates, setMyCoordinates] = useState<LatLngExpression | null>(
null
)
return (
<Map center={TORONTO_COORDINATES}>
<MapTileLayer />
<MapLocateControl
onLocationFound={(location) =>
setMyCoordinates(location.latlng)
}
onLocationError={(error) => toast.error(error.message)}
watch
/>
{myCoordinates && (
<MapPopup
position={myCoordinates}
offset={[0, -5]}
className="w-56">
{myCoordinates.toString()}
</MapPopup>
)}
</Map>
)
}
On This Page
Optional Callbacks