Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | /**
* @fileoverview localization helper functions
* @module localization
* @description Contains helper functions to get the city of a given latitude and longitude.
*/
import { GEOAPIFY_API_KEY } from '@env'
import { get } from './common'
type GeoCodeJSON = {
type: string
version: string
features: {
type: string
geometry: {
type: string
coordinates: [number, number]
}
properties: {
label: string
score: number
id: string
name: string
postcode: string
citycode: string
x: number
y: number
city: string
context: string
type: string
importance: number
street: string
distance: number
}
}[]
attribution: string
licence: string
filters: {
type: string
}
center: [number, number]
limit: number
}
/**
* @function getCityUsingGeoapify gets the city of user from lat long using the Geoapify API
* @param props Object containing the latitude and longitude of the user
* @param props.latitude The latitude of the user
* @param props.longitude The longitude of the user
* @returns Promise of a GeoCodeJSON object
*/
async function getCityUsingGeoapify({
latitude,
longitude,
}: {
latitude: number
longitude: number
}): Promise<GeoCodeJSON> {
try {
const response = await get({
url: 'https://api.geoapify.com',
endpoint: `/v1/geocode/reverse?lat=${latitude}&lon=${longitude}&apiKey=${GEOAPIFY_API_KEY}`,
})
return response.json()
} catch (error) {
console.error('Error in getCityUsingGeoapify:', error)
throw error
}
}
/**
* @function getCityUsingGouv gets the city of user from lat long using the Gouv API
* @param props Object containing the latitude and longitude of the user
* @param props.latitude The latitude of the user
* @param props.longitude The longitude of the user
* @returns Promise of a GeoCodeJSON object
*/
async function getCityUsingGouv({
latitude,
longitude,
}: {
latitude: number
longitude: number
}): Promise<GeoCodeJSON> {
try {
const response = await get({
url: 'https://api-adresse.data.gouv.fr',
endpoint: `/reverse/?lon=${longitude}&lat=${latitude}`,
})
return response.json()
} catch (error) {
console.error('Error in getCityUsingGouv:', error)
throw error
}
}
/**
* @function getCity gets the city of user from lat long
* @description Try with gouv API that successes most of the time if user is in france, if not try with geoapify
* @param props Object containing the latitude and longitude of the user
* @param props.latitude The latitude of the user
* @param props.longitude The longitude of the user
* @returns A Promise containing the city of the user
*/
export default async function getCity({
latitude,
longitude,
}: {
latitude: number
longitude: number
}): Promise<string> {
const defaultCity = 'Nantes'
try {
const gouvResponse = await getCityUsingGouv({ latitude, longitude })
if (gouvResponse.features?.length === 0) {
console.log('Unable to get data from gouv, trying with GeoApify')
const geoResponse = await getCityUsingGeoapify({ latitude, longitude })
return geoResponse.features[0]?.properties.city || defaultCity
}
return gouvResponse.features[0]?.properties.city || defaultCity
} catch (error) {
console.error('Error in getCity:', error)
return defaultCity
}
}
|