Skip to content

Commit abfcfc1

Browse files
authored
Merge pull request #49 from developmentseed/feature/geocoding
Add geocoder
2 parents 539f15f + d0a056a commit abfcfc1

File tree

3 files changed

+6332
-3929
lines changed

3 files changed

+6332
-3929
lines changed

packages/web/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
"lint": "eslint . --ext .js,.ts,.tsx"
1212
},
1313
"dependencies": {
14+
"@maplibre/maplibre-gl-geocoder": "^1.5.0",
1415
"@turf/area": "^6.5.0",
1516
"@turf/bbox-polygon": "^6.5.0",
1617
"date-fns": "^3.6.0",
18+
"events": "^3.3.0",
1719
"flatgeobuf": "^3.26.2",
1820
"lodash.get": "^4.4.2",
1921
"maplibre-gl": "^3.3.1",
@@ -23,7 +25,9 @@
2325
"devDependencies": {
2426
"@babel/runtime": "^7.22.15",
2527
"@preact/preset-vite": "^2.5.0",
28+
"@types/events": "^3.0.3",
2629
"@types/lodash.get": "^4.4.7",
30+
"@types/node": "^20.14.2",
2731
"@types/turf": "^3.5.32",
2832
"@typescript-eslint/eslint-plugin": "^6.7.0",
2933
"@typescript-eslint/parser": "^6.7.0",

packages/web/src/app/map/index.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { useEffect } from "preact/hooks";
22
import MapLibreGL, { MapMouseEvent } from "maplibre-gl";
33
import { AppActionTypes, AppDispatch, AppState } from "../reducer";
4+
import MaplibreGeocoder from "@maplibre/maplibre-gl-geocoder";
5+
import "@maplibre/maplibre-gl-geocoder/dist/maplibre-gl-geocoder.css";
46

57
interface MapProps {
68
appState: AppState;
@@ -162,6 +164,45 @@ export function Map(props: MapProps) {
162164
},
163165
});
164166

167+
const geocoderApi = {
168+
forwardGeocode: async (config: { query: string }) => {
169+
const features = [];
170+
try {
171+
const request = `https://nominatim.openstreetmap.org/search?q=${config.query}&format=geojson&polygon_geojson=1&addressdetails=1`;
172+
const response = await fetch(request);
173+
const geojson = await response.json();
174+
for (const feature of geojson.features) {
175+
const center = [
176+
feature.bbox[0] + (feature.bbox[2] - feature.bbox[0]) / 2,
177+
feature.bbox[1] + (feature.bbox[3] - feature.bbox[1]) / 2,
178+
];
179+
const point = {
180+
type: "Feature",
181+
geometry: {
182+
type: "Point",
183+
coordinates: center,
184+
},
185+
place_name: feature.properties.display_name,
186+
properties: feature.properties,
187+
text: feature.properties.display_name,
188+
place_type: ["place"],
189+
center,
190+
};
191+
features.push(point);
192+
}
193+
} catch (e) {
194+
// eslint-disable-next-line no-console
195+
console.error(`Failed to forwardGeocode with error: ${e}`);
196+
}
197+
198+
return {
199+
features,
200+
};
201+
},
202+
};
203+
204+
map.addControl(new MaplibreGeocoder(geocoderApi, { zoom: 10 }));
205+
165206
function onClick(e: MapMouseEvent) {
166207
// @ts-expect-error - MapMouseEvent doesn't know about features
167208
const features = e.features as GeoJSON.Feature[];

0 commit comments

Comments
 (0)