1
- interface GeocoderConfig {
2
- query : string ;
3
- }
1
+ import type {
2
+ CarmenGeojsonFeature ,
3
+ MaplibreGeocoderApi ,
4
+ MaplibreGeocoderApiConfig ,
5
+ MaplibreGeocoderFeatureResults ,
6
+ } from "@maplibre/maplibre-gl-geocoder" ;
4
7
5
- const nominatimGeocoder = {
6
- forwardGeocode : async ( config : GeocoderConfig ) => {
7
- const features = [ ] ;
8
+ const nominatimGeocoder : MaplibreGeocoderApi = {
9
+ forwardGeocode : async (
10
+ config : MaplibreGeocoderApiConfig ,
11
+ ) : Promise < MaplibreGeocoderFeatureResults > => {
12
+ const features : CarmenGeojsonFeature [ ] = [ ] ;
8
13
try {
9
14
const request = `https://nominatim.openstreetmap.org/search?q=${ config . query } &format=geojson&polygon_geojson=1&addressdetails=1` ;
10
15
const response = await fetch ( request ) ;
11
16
const geojson = await response . json ( ) ;
12
17
for ( let i = 0 ; i < geojson . features . length ; i += 1 ) {
13
18
const feature = geojson . features [ i ] ;
14
- const center = [
19
+ const center : number [ ] = [
15
20
feature . bbox [ 0 ] + ( feature . bbox [ 2 ] - feature . bbox [ 0 ] ) / 2 ,
16
21
feature . bbox [ 1 ] + ( feature . bbox [ 3 ] - feature . bbox [ 1 ] ) / 2 ,
17
22
] ;
18
- const point = {
23
+ const point : CarmenGeojsonFeature = {
24
+ id : feature . id ,
19
25
type : "Feature" ,
20
26
geometry : {
21
27
type : "Point" ,
@@ -25,7 +31,6 @@ const nominatimGeocoder = {
25
31
properties : feature . properties ,
26
32
text : feature . properties . display_name ,
27
33
place_type : [ "place" ] ,
28
- center,
29
34
} ;
30
35
features . push ( point ) ;
31
36
}
@@ -34,9 +39,20 @@ const nominatimGeocoder = {
34
39
}
35
40
36
41
return {
42
+ type : "FeatureCollection" ,
37
43
features,
38
44
} ;
39
45
} ,
46
+ reverseGeocode (
47
+ _config : MaplibreGeocoderApiConfig ,
48
+ ) : Promise < MaplibreGeocoderFeatureResults > {
49
+ // Empty implementation as it's required in TypeScript
50
+ const emptyResult : MaplibreGeocoderFeatureResults = {
51
+ type : "FeatureCollection" ,
52
+ features : [ ] ,
53
+ } ;
54
+ return Promise . resolve ( emptyResult ) ;
55
+ } ,
40
56
} ;
41
57
42
58
export default nominatimGeocoder ;
0 commit comments