Skip to content

Commit

Permalink
Make sure address_components are there when fetching addresses with g…
Browse files Browse the repository at this point in the history
…etReverseGeocode_Google().
  • Loading branch information
igoramadas committed Oct 13, 2024
1 parent f7b97dc commit a9d7c3d
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/maps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,15 @@ export class Maps {
const res = await this.googleClient.reverseGeocode(geoRequest)
if (res?.data?.results?.length > 0) {
let result: GeocodeResult
result = res.data.results.find((r) => r.address_components.find((c) => c.types.includes(AddressType.locality)))
result = res.data.results.find((r) => r.address_components?.find((c) => c.types.includes(AddressType.locality)))
if (!result) {
result = res.data.results.find((r) => r.address_components.find((c) => c.types.includes(AddressType.administrative_area_level_2)))
result = res.data.results.find((r) => r.address_components?.find((c) => c.types.includes(AddressType.administrative_area_level_2)))
}
const components = result.address_components
if (!components) {
logger.warn("Maps.getReverseGeocode_Google", coordinates.join(", "), "Address not found")
return null
}

// Get relevant address components.
const neighborhood = components.find((c) => c.types.includes(AddressType.neighborhood || AddressType.sublocality))
Expand Down

0 comments on commit a9d7c3d

Please sign in to comment.