Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only country specific result using google geocode api

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

like image 341
steve Avatar asked Sep 26 '13 11:09

steve


People also ask

Is Google's geocoding API free?

The Geocoding API uses a pay-as-you-go pricing model. Geocoding API requests generate calls to one of two SKUs depending on the type of request: basic or advanced.

How do I use Google geocode API?

To learn more, see Set up in Cloud Console. The Geocoding API is a service that provides geocoding and reverse geocoding of addresses. This service is also available as part of the client-side Google Maps JavaScript API, or for server-side use with the Java Client, Python Client, Go Client and Node.


2 Answers

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){
    //...
});
like image 180
Deminetix Avatar answered Oct 07 '22 05:10

Deminetix


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.

like image 36
Dr.Molle Avatar answered Oct 07 '22 04:10

Dr.Molle