Google maps api request using components issues with postal code - google-maps

I am having an issue with finding an address when all I have is the street number + street name and a postal code (assuming in US).
Using url parameters like:
address=1234+street+rd%2C12345%2Cunited+states
seems to always interpret <12345> as part of the street, like a sub unit or something. Using components parameters like:
address=1234+street+rd%2Cunited+states&components=postal_code:12345
will find the correct postal code but does not return the address information for the specific location like street number and name.
If I also add the route component and specify the street name then the full address information will be returned. Likewise if I use the locality component, then the full information will be returned:
address=1234+street+rd&components=locality:city+land|postal_code:12345
address=1234+street+rd&components=route:street+rd|postal_code:12345
The issue is I am using the geocoder to parse the addresses so I am relying on a full address response to get the components. In this specific case Im assuming the user only entered a postal code and street info so I dont have the option to include locality (without using a second maps calls) nor do I want to assume Ive broken down the street info correctly.
Can anybody shed some light on possible issues Im running into or possibly how to make a maps api call with only street number, street name, postal code, and country being known?

The Geocoding API documentation says the following:
A query containing a component filter only returns the geocoding results that match the filter. If no matches are found, the geocoder returns a result that matches the filter itself.
https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering
So, if address=1234+street+rd%2Cunited+states&components=postal_code:12345 founds a postal code it is likely because the address is missing from Google database.
You have an option to report missing addresses as described here:
https://support.google.com/maps/answer/3094045?hl=en&ref_topic=3093612
Hope this helps!

Related

google directions API keeps changing the destination postalcode

I am having troubles using the google directions api.
for example I want to retreive the distance between two cities, I send the following command :
https://maps.googleapis.com/maps/api/directions/json?origin=8380 Belgium&destination=2030 Belgium&sensor=false&alternatives=true&key=<mykey>
it should return details about the route between postalcode 8380 (belgium) and postalcode 2030 (belgium).
But in the result I see that google had altered postalcode 2030 into 4730 !
That is a complete different location.
See also this
If i simply use the maps url like this
http://www.google.be/maps/dir/8380 Belgium/2030 Belgium
then it works as expected.
How can I force google to NOT change the postal code into something else but keep using the postal code i send ?
EDIT
I have been playing with it and it turns out that if I use B-2030 in stead of 2030 as postalcode, it works as expected.
If I use BE-2030 it gets me close but still 15 km to far.
But using a prefix like B- on all postalcodes is something I rather not do, maybe it will work wrong on some other postal codes, and we also have a lot of foreign addresses I dont want to keep a list of all country codes...
EDIT
I had put this question as a ticket with google, I received an answer that they are aware of this issue and the developers are looking for a fix.
I will update this post when I receive more news from them.
You are right this is a known issue with 4-digit postal codes that was reported in Google issue tracker for a bunch of countries:
https://issuetracker.google.com/issues/75985322
Hopefully Google will solve it soon.
In the meantime it looks like the only reliable workaround consists in resolving the postal codes to their places IDs via place autocomplete service and applying place IDs in directions requests.
E.g. execute the place autocomplete requests for your examples:
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=8380%20Belgium&types=(regions)&key=YOUR_API_KEY
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=2030%20Belgium&types=(regions)&key=YOUR_API_KEY
These requests will return place ID ChIJo0saJPpVw0cRzkf6DZ5gRBo for '8380 Belgium' and place ID ChIJ4ejQlRcIxEcRnGOzGMCkbYc for '2030 Belgium'.
So, now you can execute directions request with these place IDs and get expected results
https://maps.googleapis.com/maps/api/directions/json?origin=place_id%3AChIJo0saJPpVw0cRzkf6DZ5gRBo&destination=place_id%3AChIJ4ejQlRcIxEcRnGOzGMCkbYc&mode=driving&alternatives=true&key=YOUR_API_KEY
Here is the same request in directions calculator:
https://directionsdebug.firebaseapp.com/?origin=place_id%3AChIJo0saJPpVw0cRzkf6DZ5gRBo&destination=place_id%3AChIJ4ejQlRcIxEcRnGOzGMCkbYc&mode=driving&alternatives=true
I hope this workaround might be helpful.

Find city in Google Geocoding API

I am using google Geocoding API to store the cities in the DB on demand.I provided an input field for the user to enter their city.I used googles places autocomplete for that.
I would like to store the city and its country what ever enter ed in the input field. Calling google geocoding API with the with data from input field, returned data in JSON.
I looked into address_components got country in it. Then I took object in address_components where types = locality. The problem is,For some locations typed in the input field, The JSON from google geocoding API doesn't have types = locality. So in these cases, which one among the objects in address_components should I store as city in my DB.
this website is exampale of google geocoding api https://findcity.in/
Need suggestions. The question may not be clear earlier. So I edited it. Thanks
You are right, the type locality is not always present in the address components array. Depending on the country or even regions of the same country the city name might be found in different address components. This depends on the data modelling for certain area and address templates that uses Google. Unfortunately, there is no documentation for this on per country basis.
In the official documentation you can find the following explanation
The selection of specific address components in this example is based on a typical address format. You may need to select different components to align with the postal address formats of different countries. For example, the code selects the locality component, which often represents the city part of the address. However, note the following examples of how the components may differ:
In the UK and Sweden the correct component to display the city is postal_town.
In Japan, the component differs across prefectures.
Brooklyn and other parts of New York City do not include the city as part of the address. They use sublocality_level_1 instead.
source: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
I hope this helps a bit.
administrative_area_3 indicates a third-order civil entity below the country level.
An example of this would be a search query for Montgomery returning Montgomery County in your specific state before Montgomery, AL. This is partially due to region biasing returning specified results based on your current location.
Here is some documentation from Google Which goes over the GeocoderResult.
I'd like to encourage you to make sure to take a look at the Google Maps APIs reference guide before asking a question directly. It is rather robust and you can find a lot of useful information there.

Google Geocode api returning incorrect result for a particular address

When I try to geocode one of the adress using Google geocode api, Google api returns an incorrect location.
search address : republic of estonia
result from google api: spain
I am using javascript Google api to geocode the address. For some other location the result is correct but for above address its incorrect.
If we enter below url in browser, we get the JSON back and it is showing result somewhere in Spain rather than showing result in Estonia.
http://maps.googleapis.com/maps/api/geocode/json?address=republic%20of%20estonia
I tried same addres in maps.google.com and it takes me to correct location.
Have a look at the Estonia country feature in Map Maker:
https://www.google.com/mapmaker?iwloc=0_0&fmi=0_0&gw=39&fid=5085290329182063613:4677726785527621059&dtab=overview&ll=58.620408,24.93212&spn=3.101226,8.668213&z=7&lyt=large_map_v3&htll=58.560236,25.449737&hyaw=278.7642140009582
If you check the list of names for this feature, you will see that there is no 'Republic of Estonia' in the list. The name is simply 'Estonia'.
The request for 'Estonia' works as expected:
https://maps.googleapis.com/maps/api/geocode/json?address=estonia&key=YOUR_API_KEY
If you believe that 'Republic of Estonia' must be in the list of the names please Send a feedback (bottom right corner) to Google from this page
https://www.google.com/maps/place/Estonia/#58.7223142,23.1414399,7z/data=!4m5!3m4!1s0x4692949c82a04bfd:0x40ea9fba4fb425c3!8m2!3d58.595272!4d25.013607?hl=en
Google assumed you were looking for "Calle Estonia" or "Estonia Street".
You should request that Google add "Republic of Estonia" to their list of spaces. Google, and most other mapping services, only have "Estonia" or "EST" in their list of places.
Also, you may want to use a different API if you need to get an accurate answer. Google is really good, but Google tries to "guess" what you mean and always gives you a result. But, a service like SmartyStreets matches searches to databases of addresses (so if the search is not a "real" place, it will tell you).
(Full disclosure: I have worked for SmartyStreets.)

Geocoding localities (towns/cities) with Google geocoding API

Is there a way to restrict the Google geocoding API to search only for settlements as opposed to full addresses? The API documentation specifies a 'components' argument but that seems intended to specify a specific locality rather than localities in general.
For example a search for the town of Maskiot in Israel..
http://maps.googleapis.com/maps/api/geocode/json?address=Maskiot&sensor=false&components=country:IL
returns 'Maskit Street' near Tel Aviv, but the actual place I'm after is miles away, as a Google Maps search shows:
https://www.google.co.uk/maps/place/Maskiyot/#32.3178645,35.502754,15z/data=!3m1!4b1!4m2!3m1!1s0x151cf24adf97e761:0x4c10482c54ff115c
Very grateful for advice.
This problem is the result of the fact that you misspelled the name of the place.
In your original request you were using "Maskiot" whereas the place is actually "Maskiyot". If you change this name in your original request it all works fine.

Google Maps API: Searching

I'm working on a shipping system for my company where user's enter (along with a bunch of other information) the destination that something needs to get shipped to. The system that we are migrating from just used this information as static text, so people would enter shorthand addresses such as "Alsip 60801".
My original thought was that that sort of shorthand text would work perfectly with Google Maps API, because I can type shorthand things into maps.google.com, and it generally works.
So, I wrote up some code to run geocoding on their shorthand inputs. To my unfortunate surprise, the results returned from the geocode search were greatly different from those that are returned from maps.google.com.
For example, when I search for "Alsip 60801" on maps.google.com, I get Alsip, IL 60801. That's correct. But when I search using the Google Maps API, I get some Alsip up in Canada.
Is there another search function that I should be using for the Google Maps API? Or some sort of flag that I am missing?
For reference, this is the short version of my code:
var geocode = new google.maps.Geocoder();
geocode.geocode( {'address': 'Alsip 60801' }, function(results, status) {
console.log(results[0].geometry.location);
//Outputs the LatLng of a Canada address
});
Edit 1: I forgot to mention, I did try setting the region on the geocode request. I set it to us, and got the same results.
Update - Answered: I marked #Jitimaro's answer as correct, because tacking the country code onto the end of the results does seem to work. I'm not sure if that will work with every sort of input (it's hard to predict shorthand address formats), but it seems to work fine for now.
However, I actually am handling this a different way. Instead of just tacking a country code onto the end of the address, I'm actually providing a LatLngBounds parameter on the end of my geocode request. That gives all addresses within my bounds preferential treatment as it is searching.
When you search with the website you should get a preview like this: http://maps.google.com/maps?q=Alsip+60801. When you search a reverse geocode you must parse the result to match your country http://maps.googleapis.com/maps/geo?q=Alsip+60801. You can check the different url with a network sniffer. Or you can add the country to your url http://maps.googleapis.com/maps/geo?q=Alsip+60801+USA.