Skip to content

Commit

Permalink
nominatimGeocoder: add empty reverseGeocode and types
Browse files Browse the repository at this point in the history
  • Loading branch information
starsep committed Oct 16, 2024
1 parent c9683fa commit a887041
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/components/map.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @ts-ignore
import MaplibreGeocoder from "@maplibre/maplibre-gl-geocoder";
import MaplibreGeocoder, {
type MaplibreGeocoderOptions,
} from "@maplibre/maplibre-gl-geocoder";
import "@maplibre/maplibre-gl-geocoder/dist/maplibre-gl-geocoder.css";
import maplibregl, {
type MapGeoJSONFeature,
Expand Down Expand Up @@ -196,7 +198,8 @@ const MapView: FC<MapViewProps> = ({ openChangesetId, setOpenChangesetId }) => {
const newMaplibreGeocoder = new MaplibreGeocoder(nominatimGeocoder, {
maplibregl,
placeholder: t("sidebar.find_location"),
});
reverseGeocode: false,
} as MaplibreGeocoderOptions);
newMaplibreGeocoder.setLanguage(language);
map.addControl(newMaplibreGeocoder);
maplibreGeocoderRef.current = newMaplibreGeocoder;
Expand Down
34 changes: 25 additions & 9 deletions src/components/nominatimGeocoder.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
interface GeocoderConfig {
query: string;
}
import type {
CarmenGeojsonFeature,
MaplibreGeocoderApi,
MaplibreGeocoderApiConfig,
MaplibreGeocoderFeatureResults,
} from "@maplibre/maplibre-gl-geocoder";

const nominatimGeocoder = {
forwardGeocode: async (config: GeocoderConfig) => {
const features = [];
const nominatimGeocoder: MaplibreGeocoderApi = {
forwardGeocode: async (
config: MaplibreGeocoderApiConfig,
): Promise<MaplibreGeocoderFeatureResults> => {
const features: CarmenGeojsonFeature[] = [];
try {
const request = `https://nominatim.openstreetmap.org/search?q=${config.query}&format=geojson&polygon_geojson=1&addressdetails=1`;
const response = await fetch(request);
const geojson = await response.json();
for (let i = 0; i < geojson.features.length; i += 1) {
const feature = geojson.features[i];
const center = [
const center: number[] = [
feature.bbox[0] + (feature.bbox[2] - feature.bbox[0]) / 2,
feature.bbox[1] + (feature.bbox[3] - feature.bbox[1]) / 2,
];
const point = {
const point: CarmenGeojsonFeature = {
id: feature.id,
type: "Feature",
geometry: {
type: "Point",
Expand All @@ -25,7 +31,6 @@ const nominatimGeocoder = {
properties: feature.properties,
text: feature.properties.display_name,
place_type: ["place"],
center,
};
features.push(point);
}
Expand All @@ -34,9 +39,20 @@ const nominatimGeocoder = {
}

return {
type: "FeatureCollection",
features,
};
},
reverseGeocode(
_config: MaplibreGeocoderApiConfig,
): Promise<MaplibreGeocoderFeatureResults> {
// Empty implementation as it's required in TypeScript
const emptyResult: MaplibreGeocoderFeatureResults = {
type: "FeatureCollection",
features: [],
};
return Promise.resolve(emptyResult);
},
};

export default nominatimGeocoder;

0 comments on commit a887041

Please sign in to comment.