Tepu
Tepu

Reputation: 1702

google place search location biasing

I am using Google Places search and I want "Location Biasing" on GCC countries (UAE, Saudi Arabia, Oman, Kuwait & Bahrain).

I want to achieve the below https://developers.google.com/maps/documentation/places/autocomplete#location_biasing

I want to provide Google Places search on a textbox where user can only see results from these countries UAE, Saudi Arabia, Oman, Kuwait & Bahrain?

Thanks,


I can "Location Bias" on one country using "componentRestrictions" argument as shown in the below code snippet

var mapOptions = {
          //bounds : defaultBounds,  
          center: new google.maps.LatLng(25.2644444, 55.31166669999993),
          zoom: 13,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById('map_canvas'),
          mapOptions);

        var input = document.getElementById('searchTextField');        
        var InputOptions = {
            types: ['(cities)'],
            **componentRestrictions: { country: 'AE' } // I want multiple counteries here**
        };                
        var autocomplete = new google.maps.places.Autocomplete(input, InputOptions);

But I want Location Biasing on multiple counteries that i mention above.

Upvotes: 2

Views: 3174

Answers (3)

xomena
xomena

Reputation: 32148

As of January 2017 you can now create filter for multiple countries in Place autocomplete.

You can now restrict Autocomplete predictions to only surface from multiple countries. You can do this by specifying up to 5 countries in the componentRestrictions field of the AutocompleteOptions.

https://developers.google.com/maps/documentation/javascript/releases

https://developers.google.com/maps/documentation/javascript/3.exp/reference#ComponentRestrictions

Upvotes: 2

Matt Handy
Matt Handy

Reputation: 30025

I think this is not possible (at the moment). Restricting results to one country was only added as feature some weeks ago. Your best option is to restrict the results by your map bounds as shown in this example from the Places Library website:

var defaultBounds = new google.maps.LatLngBounds(
  new google.maps.LatLng(-33.8902, 151.1759),
  new google.maps.LatLng(-33.8474, 151.2631)); 

var input = document.getElementById('searchTextField');
var options = {
  bounds: defaultBounds,
  types: ['establishment']
};

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

Find the bounds that fit best to your desired region.

Upvotes: 1

libjup
libjup

Reputation: 4089

What exactly do you want to do?

If I get you right, you just want to geocode one of these countries (UAE,Saudi Arabia, Oman, Kuwait & Bahrain)?

  function geocodeAddress() {
    var address = "Oman";
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        position: results[0].geometry.location // gives you the location
        });
      } else {
        alert("Error: " + status);
      }
    });
  }

Upvotes: 1

Related Questions