user3610340
user3610340

Reputation: 19

Google maps Places API V3 autocomplete - restrict only allow to select hotels in canberra

My new project need get address info from google map, but our service only pickup or dropoff from all the hotels in canberra. My questions is how I restrict the autocomplete only choose the hotels in canberra.

Here is my code:

var defaultBounds = new google.maps.LatLngBounds(
  new google.maps.LatLng(-35.3345571, 149.0334956, 15),
  new google.maps.LatLng(-35.1910655, 149.1817188, 12));

    var options = {
        bounds: defaultBounds,
        types: ['establishment'],
        keyword:'hotel',
        componentRestrictions: {
            country: 'au'
        }
    };

var input = document.getElementById('dropoff_address2Fromgoogle');    

var autocomplete = new google.maps.places.Autocomplete(input,options); 

google.maps.event.addListener(autocomplete, 'place_changed', function () {
    var place = autocomplete.getPlace();
    document.getElementById('dropoff_address2Fromgoogle').value = place.name 
     + ", " + place.formatted_address
      + ", " + place.address_components[5].short_name;

});

I use bounds to bounds the area in canberra, but still search result will return other area, and Type filter only have establishment seem not return more result then I am expecting.

Upvotes: 1

Views: 1350

Answers (2)

Diego Cerdan Puyol
Diego Cerdan Puyol

Reputation: 1164

The problem is that LatLngBounds expects south-west and north-east corners.

Instead of:

var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(-35.3345571, 149.0334956, 15),
    new google.maps.LatLng(-35.1910655, 149.1817188, 12));

It should be:

var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(-35.1910655, 149.0334956),
    new google.maps.LatLng(-35.3345571, 149.1817188));

Upvotes: 0

Koh
Koh

Reputation: 1570

Here is sample code that searches for hotels in a selected city.

Basically, the sample code makes calls to Google Maps APIs in two phases: one to search for a city (onPlaceChanged() was called), and another to search for hotels. In the latter search, you will call nearbySearch() on a Places object that has a bounds parameter from the city search and a types parameter with a value of 'lodging'.

Upvotes: 1

Related Questions