Get only country specific result using google geocode api

主宰稳场 提交于 2019-11-27 01:35:53

问题


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


回答1:


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.




回答2:


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



回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/19026856/get-only-country-specific-result-using-google-geocode-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!