Get only country specific result using google geocode api - google-maps

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

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

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.

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

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

Related

How to get city name from google maps location?

I am trying to get the city name from what is returned by geocode but seem to not working in any way I've tried. Can anyone help me pleasE?
here is where I get the location data which returns an object with an array of region, street, city etc like this
This line:
<Text>Location: {this.state.locationResult}</Text>
Returns this
[{"region":"England","street":"Holborn street","city":"London"}]
I've tried with locationResult[0].city but still doesn't work.
Тry this:
let obj = this.state.locationResult;
<Text>Location: {obj[0].city}</Text>

Google Geocode API - Postal Code not returned

I have a site that validates zip codes. If a user types in a zip code I store the formatted address, lat, long, etc. If they go back in to edit the form the next day, I set the field to the formatted address and revalidate. The problem is not all formatted addresses return the zip the second time - even though I'm passing it in to the query.
-- RETURNS FORMATTED ADDRESS: Woodland, CA 95776, USA
https://maps.googleapis.com/maps/api/geocode/json?address=95776&key=
-- DOESN'T RETURN A POSTAL CODE - GIVES APPROXIMATE EVEN THOUGH I'M PASSING IT IN
https://maps.googleapis.com/maps/api/geocode/json?address=Woodland, CA 95776, USA&key=
-- RETURNS POSTAL CODE AS EXPECTED, EVEN THOUGH LENEXA, HAS MULTIPLE POSTAL CODES
/maps/api/geocode/json?address=Lenexa, KS 66227, USA&key=
I would suggest storing the place ID instead of the address string. This is recommended by Google:
https://developers.google.com/places/web-service/place-id#save-id
So the first time you execute
https://maps.googleapis.com/maps/api/geocode/json?address=95776&key=YOUR_API_KEY
and store the resulting place ID ChIJARdFFWLRhIARK6RIUxEbJ30.
The next time you should execute the place ID lookup to get exactly the same result as the original request
https://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJARdFFWLRhIARK6RIUxEbJ30&key=YOUR_API_KEY

How to find a place id in Google Maps API if city has an identical name as region, province or municipality

Using the Google places API I am trying to fetch place_id's. I have issues when trying to fetch a place_id when multiple parts of the place location have the same name. For example: The city Utrecht (in the Netherlands) lies in the municipality of Utrecht, Which lies in the province of Utrecht. They all have the same name.
When I fetch Utrecht from the places service it returns the place_id for city (which is the default for maps incase there are multiple options)
I found a hack/solution to fetch the administrative areas: adding localised name of the thing i'm trying to fetch. For example I can use Gemeente(municipality) or Provincie(province)
Is there a way to request specifically the type of region I want to fetch from the API?
I see that the places API has all this information, but sofar my attempts to fetch the place_id for the administrative_area_level_2 or administrative_area_level_1 without modifying the address are futile.
My "fix" works for the Netherlands but I have 12 more European countries with similar issues hence my question.
The most common solution is using the reverse geocoding with a result type filter.
For example, your sample request for Utrecht returns the following coordinate 52.09073739999999,5.1214201
Now use reverse geocoding with result type.
https://maps.googleapis.com/maps/api/geocode/json?latlng=52.09073739999999%2C5.1214201&result_type=administrative_area_level_1&key=YOUR_API_KEY
This returns place ID ChIJPzM8M01oxkcRsFwejVreAAM for admin area level 1
https://maps.googleapis.com/maps/api/geocode/json?latlng=52.09073739999999%2C5.1214201&result_type=administrative_area_level_2&key=YOUR_API_KEY
This returns place ID ChIJA9Xkz5BoxkcRzAM9YkCZGTs for admin area level 2
Hope it helps!

Querying Geonames API to get only city country name

Too much useless json data is being fetched when I request a specific country cities by hitting this URL:
http://api.geonames.org/searchJSON?username=ksuhiyp&country=us&maxRows=1000
This returns too many json fields as you can see, question is hot to play with the query to get only city, country name ?
You can try with the additional parameter style=SHORT
http://api.geonames.org/searchJSON?username=ksuhiyp&country=us&maxRows=1000&style=SHORT
The docs says: style, String SHORT,MEDIUM,LONG,FULL (optional), verbosity of returned xml document, default = MEDIUM

Search in specific country only?

So, starting with the following query, I can pass user input to the Geocode API and return a json object/response with the locations details.
http://maps.googleapis.com/maps/api/geocode/json?address=10010
My problem is I have the following query with component filters with the country set to the US (United States). However, if you enter in a zipcode like this which is returning an area in Russia it still renders the map and sets the marker for the users current location.
http://maps.googleapis.com/maps/api/geocode/json?address=16721**&components=country:US**&sensor=false
Do I need to check the short_name within the returned object in a simple if statement and choose whether to issue a map request or not?
I thought component filters would restrict the user to search within the country specified? Then, return ZERO_RESULTS if there is not a match.
Any input, feedback is welcomed. Thanks!
Remove stars from from country name:
http://maps.googleapis.com/maps/api/geocode/json?address=16721**&sensor=false&components=country:US