Giter VIP home page Giter VIP logo

Comments (4)

Tintef avatar Tintef commented on September 6, 2024

Hi @DeanKamali,

The correct way of adding restrictions to the autocomplete is by adding thm to the getPlacePredictions call on line 9:

this.placesService.getPlacePredictions(
      {
        componentRestrictions: {...},
        location: {...},
        input: value,
      },
      this.fetchSuggestionsCallback,
    );

I haven't tested this myself, but you can take a look here for why I think this is the correct approach:

I'll be able to add that feature next week, but if this seems to work feel free to open a PR! ๐Ÿ˜„

from react-google-places-autocomplete.

Tintef avatar Tintef commented on September 6, 2024

@DeanKamali I have tested this and it works! Working on a new version with the feature now

from react-google-places-autocomplete.

DeanKamali avatar DeanKamali commented on September 6, 2024

@Tintef thatโ€™s awesome! Iโ€™m also hoping if you could figure out how to get postal code / zip code! Itโ€™s crucial to have this info when processing payments for example.

from react-google-places-autocomplete.

Tintef avatar Tintef commented on September 6, 2024

@DeanKamali

It's not possible to get the postal code from the placesPrediction call. In order to get that information another API call is necessary using the Geocoder. Here's a little script on how to obtain the postal code:

const geocoder = new google.maps.Geocoder();
geocoder.geocode({ placeId: <placeId> }, (res, status) => {
  if (status === 'OK') {
    const postalCode = res[0].address_components.find(component => (
      component.types.some(type => type === 'postal_code')
    ));
    if (postalCode) {
      /* use the obtained postal code
          notice postalCode is an object like the following:
            { long_name: <longName>, short_name: <shortName> }
      */
    }
  } else {
    /* Error */
  }
});

You should get the placeId from the onSelect function provided to the GooglePlacesAutocomplete component.

Hope this helps, I'll try to add this option later on, but this should do the trick for the moment.

from react-google-places-autocomplete.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.