diff --git a/change/@itwin-geo-tools-react-6582ffe5-6858-4b66-9546-d92b561112ee.json b/change/@itwin-geo-tools-react-6582ffe5-6858-4b66-9546-d92b561112ee.json new file mode 100644 index 0000000000..467e06a174 --- /dev/null +++ b/change/@itwin-geo-tools-react-6582ffe5-6858-4b66-9546-d92b561112ee.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "GoogleAddressProvider does not always resolve location requests correctly. Now handling different API variants", + "packageName": "@itwin/geo-tools-react", + "email": "Johannes.Goltz@bentley.com", + "dependentChangeType": "patch" +} diff --git a/packages/itwin/geo-tools/src/GoogleAddressProvider.ts b/packages/itwin/geo-tools/src/GoogleAddressProvider.ts index dbe3bcbd1a..634dee45b0 100644 --- a/packages/itwin/geo-tools/src/GoogleAddressProvider.ts +++ b/packages/itwin/geo-tools/src/GoogleAddressProvider.ts @@ -73,8 +73,19 @@ export class GoogleAddressProvider implements AddressProvider { } }); const json: any = await response.json(); - const lat = json?.location?.geometry?.latitude - const long = json?.location?.geometry?.longitude + // Google Address Validation API response shape: { location: { latitude, longitude } } + // @see https://developers.google.com/maps/documentation/address-validation/reference/rest/v1/TopLevel/validateAddress#Location + const lat = json?.location?.latitude + // Google Geocoding API response shape: { result: { geometry: { location: { lat, lng } } } } + // @see https://developers.google.com/maps/documentation/geocoding/requests-geocoding#Results + ?? json?.result?.geometry?.location?.lat + // Legacy/wrapper format: { location: { geometry: { latitude, longitude } } } + ?? json?.location?.geometry?.latitude; + + const long = json?.location?.longitude + ?? json?.result?.geometry?.location?.lng + ?? json?.location?.geometry?.longitude; + if (lat === undefined || long === undefined) { throw new Error("Invalid location data"); }