hibiken / react-places-autocomplete

React component for Google Maps Places Autocomplete
https://hibiken.github.io/react-places-autocomplete/
MIT License
1.38k stars 388 forks source link

Search by Post Codes #248

Open saranshbansal opened 5 years ago

saranshbansal commented 5 years ago

I need to be able to search via post/zip codes but currently it is not showing any results. Here is my setup:


<PlacesAutocomplete
            value={this.state.value}
            onChange={this.handleChange}
            onSelect={this.handleSelect}
            onError={this.handleError}
            clearItemsOnError={true}
            searchOptions={{ types: ['address'] }}
            shouldFetchSuggestions={this.state.value.length >= 3}
          >
{({ getInputProps, suggestions, getSuggestionItemProps }) => {
              const hasSuggestions = !!suggestions && !!suggestions.length;
              return (
                <div>
                  {hasGoogleAddress && <div style={{ marginTop: '1rem' }}>Change Street Address</div>}

                  <div className="form-help-text">
                    Start typing the address below and then choose the suggestion that matches the full address.
                  </div>

                  <TextField {...getInputProps()} hintText="Enter street address" fullWidth />

                  {hasSuggestions && (
                    <div className="google-place-form-field__suggestions">
                      <Card>
                        {suggestions.map(suggestion => (
                          <div
                            {...getSuggestionItemProps(suggestion, {
                              className: 'google-place-form-field__suggestion',
                            })}
                          >
                            <span>{suggestion.description}</span>
                          </div>
                        ))}
                      </Card>
                    </div>
                  )}
                </div>
              );
            }}
 </PlacesAutocomplete>

Can you please let me know what changes are required in order to do so? I tried searching via postcodes in Google Maps and it works so I assume it should work here as well. Thanks.
deftomat commented 5 years ago

@saranshbansal Change searchOptions={{ types: ['address'] }} to searchOptions={{}}.

eneskaya commented 5 years ago

Try setting searchOptions to searchOptions={{ ["(regions")] }}, also see: https://developers.google.com/places/supported_types#table3

keirstaple commented 5 years ago

searchOptions={{ types: ["(regions)"] }} worked for me. Note the parentheses are required. You can then filter by postal_code in address_components, types array.