skynet2 / ngx-google-places-autocomplete

Google Places autocomplete for angular web project
MIT License
91 stars 76 forks source link

Issues Returning Certain Addresses #98

Open mtwallace opened 3 years ago

mtwallace commented 3 years ago

So we've had the autocomplete live on our site for several weeks and our QA person found some issues that I can't quite put a finger on.

Here's the options in my component:

options = {
      componentRestrictions: { country: ["US"] },
      fields: ["address_components"],
      types: ["address"]
  }

Here's my handleAddressChange method:

handleAddressChange = (address: Address) => {
    const streetNumber = this.getComponentByType(address, "street_number");
    const streetNumberSN = streetNumber ? streetNumber.short_name : '';
    const street = this.getComponentByType(address, "route");
    const streetSN = street ? street.short_name : '';
    const city = this.getComponentByType(address, "locality");
    const citySN = city ? city.short_name : '';
    const state = this.getComponentByType(address, "administrative_area_level_1");
    const stateSN = state ? state.short_name : '';
    const zip = this.getComponentByType(address, "postal_code");
    const zipSN = zip ? zip.short_name : '';

    if (streetNumberSN) {
        this.showValidation = false;
        this.AddressForm.controls['location'].disable();
        this.sessionLead.Address = streetNumberSN ? streetNumberSN + " " + streetSN : streetSN;
        this.sessionLead.City = citySN;
        this.sessionLead.State = stateSN;
        this.sessionLead.ZipCode = zipSN;
    }
}

getComponentByType:

public getComponentByType(address: Address, type: string): AddressComponent {
    if (!type)
        return null;

    if (!address || !address.address_components || address.address_components.length == 0)
        return null;

    type = type.toLowerCase();

    for (let comp of address.address_components) {
        if (!comp.types || comp.types.length == 0)
            continue;

        if (comp.types.findIndex(x => x.toLowerCase() == type) > -1)
            return comp;
    }

    return null;
}

This first address, which is outside of the US, causes the page to become unresponsive and crash: "1519 Prindsen Gade Charlotte Amalie, St Thomas"

This second address doesn't return a street number despite entering one and picking corresponding option which includes the street number: "21627 Hardingrove Road, Milesville, SD, 57553"

Is there anything that can be done to fix these things? Is this on Google or is it me? Thanks!

mtwallace commented 3 years ago

First bug was because of my validation regex but still not able to get the street number for the second address.