-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocoderApi.js
31 lines (30 loc) · 1 KB
/
geocoderApi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const geocoderApi = {
forwardGeocode: async (config) => {
const features = [];
try {
const request = `https://api-adresse.data.gouv.fr/search/?q=${config.query}&limit=1`;
const response = await fetch(request);
const data = await response.json();
// Assuming the first result is the most relevant
const result = data.features[0];
const point = {
type: "Feature",
geometry: {
type: "Point",
coordinates: [result.geometry.coordinates[0], result.geometry.coordinates[1]],
},
place_name: result.properties.label,
properties: result.properties,
text: result.properties.label,
place_type: ["place"],
center: [result.geometry.coordinates[0], result.geometry.coordinates[1]],
};
features.push(point);
} catch (e) {
console.error(`Failed to forwardGeocode with error: ${e}`);
}
return {
features,
};
},
};