Skip to content

Commit a887041

Browse files
committed
nominatimGeocoder: add empty reverseGeocode and types
1 parent c9683fa commit a887041

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/components/map.tsx

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// @ts-ignore
2-
import MaplibreGeocoder from "@maplibre/maplibre-gl-geocoder";
2+
import MaplibreGeocoder, {
3+
type MaplibreGeocoderOptions,
4+
} from "@maplibre/maplibre-gl-geocoder";
35
import "@maplibre/maplibre-gl-geocoder/dist/maplibre-gl-geocoder.css";
46
import maplibregl, {
57
type MapGeoJSONFeature,
@@ -196,7 +198,8 @@ const MapView: FC<MapViewProps> = ({ openChangesetId, setOpenChangesetId }) => {
196198
const newMaplibreGeocoder = new MaplibreGeocoder(nominatimGeocoder, {
197199
maplibregl,
198200
placeholder: t("sidebar.find_location"),
199-
});
201+
reverseGeocode: false,
202+
} as MaplibreGeocoderOptions);
200203
newMaplibreGeocoder.setLanguage(language);
201204
map.addControl(newMaplibreGeocoder);
202205
maplibreGeocoderRef.current = newMaplibreGeocoder;

src/components/nominatimGeocoder.ts

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
interface GeocoderConfig {
2-
query: string;
3-
}
1+
import type {
2+
CarmenGeojsonFeature,
3+
MaplibreGeocoderApi,
4+
MaplibreGeocoderApiConfig,
5+
MaplibreGeocoderFeatureResults,
6+
} from "@maplibre/maplibre-gl-geocoder";
47

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[] = [];
813
try {
914
const request = `https://nominatim.openstreetmap.org/search?q=${config.query}&format=geojson&polygon_geojson=1&addressdetails=1`;
1015
const response = await fetch(request);
1116
const geojson = await response.json();
1217
for (let i = 0; i < geojson.features.length; i += 1) {
1318
const feature = geojson.features[i];
14-
const center = [
19+
const center: number[] = [
1520
feature.bbox[0] + (feature.bbox[2] - feature.bbox[0]) / 2,
1621
feature.bbox[1] + (feature.bbox[3] - feature.bbox[1]) / 2,
1722
];
18-
const point = {
23+
const point: CarmenGeojsonFeature = {
24+
id: feature.id,
1925
type: "Feature",
2026
geometry: {
2127
type: "Point",
@@ -25,7 +31,6 @@ const nominatimGeocoder = {
2531
properties: feature.properties,
2632
text: feature.properties.display_name,
2733
place_type: ["place"],
28-
center,
2934
};
3035
features.push(point);
3136
}
@@ -34,9 +39,20 @@ const nominatimGeocoder = {
3439
}
3540

3641
return {
42+
type: "FeatureCollection",
3743
features,
3844
};
3945
},
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+
},
4056
};
4157

4258
export default nominatimGeocoder;

0 commit comments

Comments
 (0)