-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d90e83
commit 7fe0563
Showing
8 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { MapCoordinates, MapResponse } from 'src/types/map' | ||
|
||
|
||
export const getMapCoordinates = async (address: string): Promise<MapCoordinates> => { | ||
const response = await fetch( | ||
`https://nominatim.openstreetmap.org/search?q=${encodeURIComponent(address)}&format=json` | ||
) | ||
const data: MapResponse[] = await response.json() | ||
console.log('data', data) | ||
|
||
return [parseFloat(data[0].lon), parseFloat(data[0].lat)] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use client' | ||
|
||
import { Map, View } from 'ol' | ||
import { Tile as TileLayer } from 'ol/layer' | ||
import 'ol/ol.css' | ||
import { fromLonLat } from 'ol/proj' | ||
import { OSM } from 'ol/source' | ||
import type { FC } from 'react' | ||
import { useEffect, useRef } from 'react' | ||
|
||
import { MAP_ZOOM_LEVEL } from 'src/helpers/config/map' | ||
import type { MapCoordinates } from 'src/types/map' | ||
|
||
|
||
type GeoMapProps = { | ||
address: string | ||
coordinates: MapCoordinates | ||
} | ||
|
||
export const GeoMap: FC<GeoMapProps> = ({ coordinates }) => { | ||
const mapContainerRef = useRef<HTMLDivElement>(null) | ||
const mapRef = useRef<Map | null>(null) | ||
|
||
useEffect(() => { | ||
if (coordinates !== null) { | ||
const centerCoordinates = fromLonLat(coordinates) | ||
|
||
if (mapContainerRef.current && !mapRef.current) { | ||
mapRef.current = new Map({ | ||
layers: [new TileLayer({ source: new OSM() })], | ||
view: new View({ | ||
center: centerCoordinates, | ||
zoom: MAP_ZOOM_LEVEL, | ||
}), | ||
target: mapContainerRef.current, | ||
}) | ||
} else if (mapRef.current) { | ||
mapRef.current.getView().setCenter(centerCoordinates) | ||
} | ||
} | ||
}, [coordinates]) | ||
|
||
useEffect(() => { | ||
return () => { | ||
if (mapRef.current) { | ||
console.log('unmount') | ||
mapRef.current.setTarget(undefined) | ||
mapRef.current = null | ||
} | ||
} | ||
}, []) | ||
|
||
if (coordinates === null) { | ||
return null | ||
} | ||
|
||
return ( | ||
<div | ||
ref={mapContainerRef} | ||
style={{ width: '100%', height: '400px', position: 'relative' }} | ||
/> | ||
) | ||
} |
Oops, something went wrong.