21

I am using google geocode api for fetching lat long using postal code of singapore

I have tried following to fetch data:

(1) http://maps.googleapis.com/maps/api/geocode/json?address=505468&sensor=false

(2) http://maps.googleapis.com/maps/api/geocode/json?address=Singapore%20505468&sensor=false

(3) http://maps.googleapis.com/maps/api/geocode/json?address=Singapore%20505468&sensor=false&region=sg

But it returns location from India

https://developers.google.com/maps/documentation/geocoding/?hl=fr#RegionCodes

Any other way to get only country specific (Singapore) result.

I have tried following it returns correct result

http://maps.googleapis.com/maps/api/geocode/json?address=Singapore%20133224&sensor=false

Kara
  • 6,115
  • 16
  • 50
  • 57
steve
  • 1,365
  • 4
  • 19
  • 33
  • possible duplicate of [Google's Geocoder returns wrong country, ignoring the region hint](http://stackoverflow.com/questions/2647086/googles-geocoder-returns-wrong-country-ignoring-the-region-hint) – symcbean Feb 28 '14 at 23:22

4 Answers4

31

The correct way of doing this is by providing componentRestrictions

For example:

var request = {
    address: address,
    componentRestrictions: {
        country: 'UK'
    }
}
geocoder.geocode(request, function(results, status){
    //...
});
Deminetix
  • 2,866
  • 26
  • 21
  • 1
    Exact duplicate of [your answer](http://stackoverflow.com/questions/2647086/googles-geocoder-returns-wrong-country-ignoring-the-region-hint/22081927#22081927), stop posting duplicate answers and there by not creating redundancy. – Paresh Mayani Feb 28 '14 at 16:01
  • 3
    You both saw it was a duplicate question: the correct procedure is to close the question on that basis. – symcbean Feb 28 '14 at 23:22
  • Saved my life! Thanks – extraxt Mar 09 '20 at 16:28
27

To restrict results to a specific country use the component-filtering.

The url should be http://maps.googleapis.com/maps/api/geocode/json?address=Singapore%20505468&sensor=false&components=country:SG

But that's not the issue here, the geocoder-result is wrong, because the result has the country set to SG, but the location is wrong(placed in india).

I'm afraid with the given address(it appears that the postcode doesn't exists), the only thing you can do is to report the wrong result to google.

Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
  • Thanks for your help and I have found this solution if we pass `postal_code` with api request than we will get only specific postal code result. `http://maps.google.com/maps/api/geocode/json?address=505468&components=country:SG|postal_code:505468&sensor=false` `http://maps.google.com/maps/api/geocode/json?address=787878&components=country:SG|postal_code:787878&sensor=false` But I want that user can search by any address, street name or postal code so above solution will not work. I will going to report about this to google. – steve Sep 27 '13 at 05:36
  • multiple country is not supported components=country:us|components=country:pr is not working – arjavlad Feb 18 '17 at 07:33
  • 1
    how to set multiple countries in components while make a google api autocomplete call ? – Hardik Vasani Mar 20 '17 at 10:17
  • 2
    https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Raf&sensor=false&types=(regions)&key=YOUR_API_KEY&components=country:ESP|country:uk|country:us – Vivek Barai Mar 20 '17 at 10:26
11

you can set single country or multiple countries

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Raf&sensor=false&types=(regions)&key=YOUR_API_KEY&components=country:ESP

or

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Raf&sensor=false&types=(regions)&key=YOUR_API_KEY&components=country:ESP|country:uk|country:us

tanguy_k
  • 11,307
  • 6
  • 54
  • 58
Vivek Barai
  • 1,338
  • 13
  • 26
1

You should be able to use this approach http://maps.googleapis.com/maps/api/geocode/json?components=country:US|postal_code:5037

remove the address and to the component pass the postal code and make sure if the address has a value let pass another value selected by the user with the postal code to validate your api or else without postal code passed it will leak to another address

Jabes Pauya
  • 173
  • 1
  • 2
  • 10