Skip to content

Commit

Permalink
feat(map): zoom to location
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jun 21, 2023
1 parent 0ad13a3 commit d055096
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions src/map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useDeviceLocation } from '#context/DeviceLocation.js'
import { useParameters } from '#context/Parameters.js'
import maplibregl from 'maplibre-gl'
import { createContext } from 'preact'
import { useContext, useEffect, useRef } from 'preact/hooks'
import { useContext, useEffect, useRef, useState } from 'preact/hooks'
import { styled } from 'styled-components'
import { useCognitoCredentials } from '../context/CognitoCredentials.js'
import { geoJSONPolygonFromCircle } from './geoJSONPolygonFromCircle.js'
Expand Down Expand Up @@ -307,35 +307,46 @@ export const Map = () => {
const { credentials } = useCognitoCredentials()
const containerRef = useRef<HTMLDivElement>(null)
const { location } = useDeviceLocation()
console.log(`[Map]`, location)
const [map, setMap] = useState<maplibregl.Map>()

useEffect(() => {
if (containerRef.current === null) return
let map: maplibregl.Map | undefined = undefined
onParameters(({ region, mapName }) => {
map = new maplibregl.Map({
container: 'map',
style: mapStyle({
region,
mapName,
setMap(
new maplibregl.Map({
container: 'map',
style: mapStyle({
region,
mapName,
}),
center: [10.437581513483195, 63.42148461054351],
zoom: 12,
transformRequest: transformRequest(credentials, region),
refreshExpiredTiles: false,
trackResize: true,
keyboard: false,
renderWorldCopies: false,
// Static map, no mouse interaction at all
interactive: false,
}),
center: [10.437581513483195, 63.42148461054351],
zoom: 12,
transformRequest: transformRequest(credentials, region),
refreshExpiredTiles: false,
trackResize: true,
keyboard: false,
renderWorldCopies: false,
// Static map, no mouse interaction at all
interactive: false,
})
)
})

return () => {
map?.remove()
}
}, [containerRef.current])

useEffect(() => {
if (location === undefined) return
if (map === undefined) return
console.log(`[Map]`, location)
map.flyTo({
center: location,
zoom: 12,
})
}, [location, map])

return (
<MapSection>
<MapContainer id="map" ref={containerRef} />
Expand All @@ -346,8 +357,9 @@ export const Map = () => {
)}
{location !== undefined && (
<NoLocation>
<MapPin strokeWidth={1} style={{ zoom: 4 }} /> {location.lat},
{location.lng}
<MapPin strokeWidth={1} style={{ zoom: 4 }} />
<br />
{location.lat},{location.lng}
</NoLocation>
)}
</MapSection>
Expand Down

0 comments on commit d055096

Please sign in to comment.