google directions API keeps changing the destination postalcode - google-maps

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.

Related

google map api autocomplete goes to wrong address

I've typed in an address that I know exists into a google api autocomplete :
642 Great Western Hwy Girraween NSW 2145
map example link
-The autocomplete recognizes it, but...
The result I get is incorrect:
642 Great Western Hwy, Faulconbridge NSW 2776, Australia
For other addresses like this,
is there any workaround for a fix?
I've investigated the Autocomplete.getPlaces()
and can't seem to find a way around this.
-I even looked into AutocompleteServices objects (where the autocomplete gives the correct prediction - but is not within official address_components.
Any suggestions?
This is ongoing issue on Google side and it was reported in Google issue tracker several days ago. You can find it here:
Locality/postal_code in Place Details does not align with Place Autocomplete description.
Google is looking into it and hopefully they will find solution soon. I would suggest starring the bug to add your vote and subscribe to notifications from Google.
In the meantime the unique workaround I can think of is using a formatted string from autocomplete prediction and resolve it using Geocoding API service instead of getting place details using a place ID.
For example, you execute autocomplete request
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=642%20Great%20Western%20Hwy%20Girraween%20NSW%202145&key=YOUR_API_KEY
It returns the following values in the response JSON:
"description":"642 Great Western Hwy, Girraween NSW 2145, Australia"
"place_id":"EjQ2NDIgR3JlYXQgV2VzdGVybiBId3ksIEdpcnJhd2VlbiBOU1cgMjE0NSwgQXVzdHJhbGlhIjESLwoUChIJ1Ww4O998EmsRmSCZE6S6OVMQggUqFAoSCdtIcBNscBJrESbra3OWkCBW"
If you use place ID to get details it resolves to wrong address
https://maps.googleapis.com/maps/api/place/details/json?placeid=EjQ2NDIgR3JlYXQgV2VzdGVybiBId3ksIEdpcnJhd2VlbiBOU1cgMjE0NSwgQXVzdHJhbGlhIjESLwoUChIJ1Ww4O998EmsRmSCZE6S6OVMQggUqFAoSCdtIcBNscBJrESbra3OWkCBW&key=YOUR_API_KEY
However, if you use geocoding with text from description you get correct address:
https://maps.googleapis.com/maps/api/geocode/json?address=642%20Great%20Western%20Hwy%20Girraween%20NSW%202145&key=YOUR_API_KEY
You can also see this in Geocoder tool:
https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D642%2520Great%2520Western%2520Hwy%2520Girraween%2520NSW%25202145
I hope this helps!

Google geocoding API returns ZERO_RESULTS for postal code

I am trying to geocode user entered data using the Google maps API, and got an error for the Australian postal code "2010"
maps.google.com/maps/api/geocode/json?components=country:AU|postal_code:2010
However, if I search in Google maps, I get a result. Am I doing something wrong in my request?
Your request is completely OK, but unfortunately, Google experience issues with searching 4-digit postal codes. This bug has already been reported in Google issue tracker and you can see it here:
4-digit postal codes are hard to geocode (AT, AU, BE, DK, NZ, SI)
I would suggest starring the bug to add your vote and subscribe to further notifications from Google.
Also you can see that suggested workaround by Google is using place autocomplete request with types (region) and country components filter.
So, in your case you can run the following query
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=2010&types=(regions)&components=country%3AAU&key=YOUR_API_KEY
It will return a place ID for postal code 2010: ChIJ3QyubXuuEmsREIe6P2t9ARw
And you can use geocoding with place ID to get required information
https://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJ3QyubXuuEmsREIe6P2t9ARw&key=YOUR_API_KEY
I hope this helps!
I experienced a similar issue periodically, when geocoding US zip-codes. Appending the country name after the zip-code seemed to fix the problem. So, instead of 60162 use 60162 USA.
When a URL is opened in the browser based on its IP context, it identifies the country and automatically deduces the query format for a zip code is 99.999-99.
The format above is for Brazil, but there are different standards for each country such as USA and Portugal.
One solution when calling an API via CURL, either by code or by terminal, include the "&components = country: US" parameter, changing the "US" country code to what you want to filter.
The Google API returns ZERO_RESULTS for safety, because a search would be too slow and could do a global search worldwide and not just the country. For performance and speed reasons, Google creates this automatic ID for help, but unfortunately is not provided to developers about this process.
Good luck!

Google maps api request using components issues with postal code

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!

Link to street view URL using address?

Is it possible to link directly to street view from an URL using only the address (and not lat/lng)?
For example, is there a way to do something like
http://maps.google.com/maps?q=&layer=c&address=Street,number,state&cbp=11,0,0,0,0
Instead of
http://maps.google.com/maps?q=&layer=c&cbll=31.335198,-89.287204&cbp=11,0,0,0,0
?
Lat/lng from geocoding tends to not be recognized by street view as being too far from a street, sometimes getting the wrong street altogether, because the lat/lng it generates may be off the street, as it tries to match the location of a house.
I have looked everywhere and even tried playing with google's URLs on my own, but I can't find anything on it. Most sources won't even mention address. I'm currently using the url sample from this question, but it still isn't really what I'm looking for.
If this is really not possible, could someone link to a source/documentation where it says so?
Edit: Thank you everyone for the answers, but none really address the issue with street view and addresses. I'll look into forwarding this to google. I'll post here if I get a reply.
Let's enhance your code, if you paste the code in browser then you will be redirect to google maps and will show result what you requested.
http://maps.google.com/maps?&q=Space+Needle,Seattle+WA
If you would like to pass the value (address) via php or any other code type then simply it will not work, you cannot use google maps without API to enhance your requirement. Lat & Lng is basic practice to show map and not require any api, but if you would like to pass additional properties then you will have to use google maps api to do so. Detailed documentation can be found here
https://developers.google.com/maps/documentation/embed/guide
but let's try an example try this code in browser
https://www.google.com/maps/embed/v1/place?q=Mumbai,+Maharashtra,+India
You will get an error that api key not found but if you try this one
https://www.google.com/maps/embed/v1/place?key=AIzaSyD4iE2xVSpkLLOXoyqT-RuPwURN3ddScAI&q=Space+Needle,Seattle+WA
Then you will get output. Hope it helps!
First reverse geocode to find the lat lng using another Google Service API.
Then feed the resulting lat lng into the streetview endpoint parameter.

Location not found with Google Maps API

I have an address that is a valid address according to the USPS...
1606 ULSTER AVE, LAKE KATRINE, NY 12449
Searching Google Maps for this address in my browser yields the proper results. However, Google Maps API yields ZERO_RESULTS:
{
"results" : [],
"status" : "ZERO_RESULTS"
}
This is happening for about 1% of my locations. Why is the API returning separate results? How do I get it to yield the same results as the browser UI?
Perhaps if you were to validate/standardize the address before submitting it for Google geocoding you would get better results. A quick Google search for "address validation" will give you a number of good tools to do that.
#geocodezip is right that when you remove the ZIP code you get a more predictable response. That might be because the ZIP code is a numeric representation of the city/state combination and is usually regarded as a positive identifier in many tables. However, since the ZIP code can be changed at any time by the US Postal Service, a more consistent response might be obtained (if you encounter issues) by always removing the ZIP code. The problem with always removing the ZIP code is that you have now removed 20% of the address information that you have, and many times a ZIP code can trump a bad city/state combination and still get you valid results.
Consider the following somewhat invalid address: This fails:
275 north main Hollywsdfsdfsdfsdfood CA 84627
If you discard the ZIP code, you get nothing because 275 north main is not a valid address in "Hollywsdfsdfsdfsdfood CA"
However, if you keep the ZIP code and use that instead of the unlikely "Hollywsdfsdfsdfsdfood CA" as the city/state designator, you end up with a completely valid address in Ephraim, Utah 84627.
Here's an example on SmartyStreets since Google Maps was unable to handle it with the bogus city name. The validated address is:
275 N Main St Ephraim UT 84627-1107
So, going back to the idea of address validation BEFORE geocoding, you could then put that address into Google Maps and easily get your geocode. Maybe even keep your current process and then just add a separate routine for the 1% that fail.
[I deal with this kind of thing every day, I work at SmartyStreets]
This is a Places API result, not from the geocoder.
Looks like the zip code is confusing the geocoder, if I remove the zip code, it returns a result:
http://www.geocodezip.com/v3_example_geo2.asp?addr1=1606%20ULSTER%20AVE,%20LAKE%20KATRINE,%20NY&geocode=1