Get only country specific result using google geocode api

steve picture steve · Sep 26, 2013 · Viewed 44.7k times · Source

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

Answer

Deminetix picture Deminetix · Feb 27, 2014

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){
    //...
});