Skip to content

Commit 23d8efd

Browse files
Nice names for clicking on the map
1 parent dc844af commit 23d8efd

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/components/MapPin.svelte

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<script lang="ts">
2-
import { toLonLat } from 'ol/proj'
2+
import { toLonLat } from 'ol/proj'
33
import { getOlContext } from '../ol/Map.svelte'
4-
import fragment from '../stores/fragment'
4+
import fragment from '../stores/fragment'
5+
import { findPlace } from '../search/geocode'
6+
import round from '../utils/round'
57
68
const { map } = getOlContext()
79
8-
map.on('click', e => {
10+
map.on('click', async (e) => {
911
const [lng, lat] = toLonLat(e.coordinate)
12+
const closestPoint = await findPlace(lat, lng)
1013
$fragment.label = {
11-
lat,
12-
lng,
13-
text: `Lat/Lng: ${lat},${lng}`
14+
lat: closestPoint?.lat ?? lat,
15+
lng: closestPoint?.lon ?? lng,
16+
text: closestPoint?.name ?? `Lat/Lon: ${round(lat, 5)},${round(lng, 5)}`,
1417
}
1518
})
1619
</script>

src/search/geocode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const searchNzPlaces = async (query: string): Promise<GeocodeResult[]> => {
4444

4545
export const findPlace = async (lat: number, lon: number) => {
4646
const search = await getSearch()
47-
return search.closest_place(lat, lon, 100)
47+
return search.closest_place(lat, lon, 0.1)
4848
}
4949

5050
export default async (query: string, sources=[searchNzPlaces]): Promise<GeocodeResult[]> => {

0 commit comments

Comments
 (0)