From 1b0ab84923642e6282a4e37dcf4fbf23e6bdd175 Mon Sep 17 00:00:00 2001 From: evlist Date: Sun, 9 Oct 2022 22:11:03 +0200 Subject: [PATCH] Querying nominatim reverse (OSM) to get addresses. --- src/components/map/LocationInfo.tsx | 44 +++++++++++++++++++++++++++-- src/i18n/index.tsx | 4 +++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/components/map/LocationInfo.tsx b/src/components/map/LocationInfo.tsx index 3aecf97..54397ff 100644 --- a/src/components/map/LocationInfo.tsx +++ b/src/components/map/LocationInfo.tsx @@ -26,10 +26,12 @@ const LocationInfo: React.FC<{}> = () => { }; const [elevation, setElevation] = useState(undefined); + const [address, setAddress] = useState<{ display_name: any } | undefined>( + undefined + ); - const ionModalWillPresentHandler = async () => { + const findElevation = async () => { setElevation(undefined); - console.log('ionModalWillPresentHandler'); const response = await fetch( // `https://api.opentopodata.org/v1/mapzen?locations=${scope.center.lat},${scope.center.lon}`, `https://api.open-meteo.com/v1/elevation?latitude=${scope.center.lat}&longitude=${scope.center.lon}`, @@ -39,6 +41,26 @@ const LocationInfo: React.FC<{}> = () => { setElevation(data.elevation[0]); }; + const findAddress = async () => { + setAddress(undefined); + const response = await fetch( + `https://nominatim.openstreetmap.org/reverse?lat=${ + scope.center.lat + }&lon=${ + scope.center.lon + }&format=jsonv2&addressdetails=1&extratags=1&namedetails=1&accept-language=${i18n.getLanguage()}`, + {} + ); + const data = await response.json(); + console.log(JSON.stringify(data)); + setAddress(data); + }; + + const ionModalWillPresentHandler = async () => { + findElevation(); + findAddress(); + }; + return ( = () => { {elevation !== undefined && <>{elevation} m} - {' '} + + + {i18n.locationInfo!.address} + + + {i18n.locationInfo!.display_name} + {address === undefined && } + {address !== undefined && + address.display_name.split(',').map((value: string) => ( + <> + {value} +
+ + ))} +
+
+
); }; diff --git a/src/i18n/index.tsx b/src/i18n/index.tsx index 628d25d..d3c8c06 100644 --- a/src/i18n/index.tsx +++ b/src/i18n/index.tsx @@ -55,6 +55,8 @@ const strings = new LocalizedStrings({ lat: 'Latitude: ', lon: 'Longitude: ', elevation: 'Elevation:', + address: 'Address', + display_name: 'Full address:', }, }, fr: { @@ -121,6 +123,8 @@ const strings = new LocalizedStrings({ lat: 'Latitude : ', lon: 'Longitude : ', elevation: 'Altitude :', + address: 'Addresse', + display_name: 'Adresse complète :', }, }, });