Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

getAddressComponent utility function #1069

Open
99darshan opened this issue Sep 5, 2022 · 3 comments
Open

getAddressComponent utility function #1069

99darshan opened this issue Sep 5, 2022 · 3 comments

Comments

@99darshan
Copy link

Feature Request

Describe the Feature

I was thinking if we could add a getAddressComponent(result: GeocodeResult) utility function? 馃

This would be similar to getZipCode utility function. getAddressComponent can be applicable to extract other address components like state, county, country, street number, route, city, etc. from the GeoCodeResult object.

Describe the Solution You'd Like

I was thinking of something like below, I have a WIP Commit HERE

export enum AddressComponent {
  STREET_NUMBER = "street_number",
  ROUTE = "route",
  NEIGHBORHOOD = "neighborhood",
  LOCALITY = "locality",
  ADMINISTRATIVE_AREA_LEVEL_1 = "administrative_area_level_1",
  ADMINISTRATIVE_AREA_LEVEL_2 = "administrative_area_level_2",
  POSTAL_CODE = "postal_code",
  COUNTRY = "country",
}

export const getAddressComponent = (
  result: GeocodeResult,
  addressComponent: AddressComponent,
  useShortName: false
): string | undefined => {
  const foundAddressComponent = result.address_components.find(({ types }) =>
    types.includes(addressComponent)
  );

  if (!foundAddressComponent) return undefined;

  return useShortName
    ? foundAddressComponent.short_name
    : foundAddressComponent.long_name;
};
@hardik-javascript
Copy link

This is also what i need because in need to use country short name which is 2 digit alpha code of country

@davedotdev
Copy link

Any movement on this? Seems dead?

@triethuynh2301
Copy link

This would be a great addition to the library. I am not sure if this will be implemented anytime soon, but there is a way to get more details about the address of the location. In the getGeoCode utility function. The result should return more details of the location search

  // Get latitude and longitude via utility functions
  getGeocode({ address: suggestion.description }).then((results) => {
    // Contains details of the description (address, zip code, country, etc...)
    console.log(results)

    const { lat, lng } = getLatLng(results[0])
    onChangeLocation(lat, lng)
  })

Hope this helps anyone needing to find the actual address of the search.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants