Synapsis
Synapsis

Reputation: 1017

Force user to use only place autocomplete results

I'm using the "Places" Google API to make the address input field autocomplete.

The service that the website offers is restricted to a certain area so I restricted the autocomplete filter. The only problem is that the user is not forced to use the google filter but can type in anything.

How do I force the user to select something from the autocomplete filter?

This is the JsFiddle of my code

Upvotes: 1

Views: 387

Answers (1)

evan
evan

Reputation: 5691

If all you need is to do nothing when the user doesn't select a place from the Place Autocomplete list, then you can simply do this:

  autocomplete.addListener('place_changed', function() {
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      // User entered the name of a Place that was not suggested and
      // pressed the Enter key, or the Place Details request failed.
      return;
    }

    // If the place has a geometry, then present it on a map.
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(17);
    }
  });

Here's the jsfiddle based off of Google's example.

Hope this helps!

Upvotes: 1

Related Questions