Skip to content

getAddressComponent utility function #1069

@99darshan

Description

@99darshan

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;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions