#Geo - Prefill Address or Street using Google API
One way to prefill street names, street addresses, country, or anything that can be geocoded, is to call the Google Maps API.
The following functions show how to get an address number and a street name. The third function is what calls the Google reverse geocoding URL and receives the JSON response.
You must obtain a Google API key and insert it where you find [API_KEY] in the code.
To apply it to a form, simply put the following code in the page-level javascript and then call one of the functions (ex. "getStreetName(location1.latitude, location1.longitude)") in the dynamic value of the element you want prefilled.
-
Thanks Matthew, I'm new to the iform format. How do you link the Java script at the form level to the element. ie. can you please explain "getStreetName(location1.latitude, location1.longitude)") in the dynamic value of the element you want prefilled. What input to you attache the dynamic value to?
-
Thanks Matthew - this is excellent. I've been trying to get it going for Android as well as iOS (which is working well) - I'd thought it was because Android didn't provide .latitude and .longitude - but that's wrong so it seems to be around XMLHttpRequest not work - any one with any ideas??
-
It appears this is a known issue with Android. See the following article... http://www.kinvey.com/blog/107/how-to-build-a-service-that-supports-every-android-browser.
We work exclusively with iOS devices so I have no proven solution.
-
Hi Matthew - thanks for response. In testing the code on Android the function call fails at this point " xmlHttp = new XMLHttpRequest();" it never gets past this point - IE before it sends the request. So not sure if it is the issue you point to. We have about 40-50% Android users so will keep trying to figure out where it fails.
-
I found the functions would break the field if that specific data point (normally it was the zip code) was not applicable. I fixed up the functions a bit and also added a getFormattedAddress() function.
https://gist.github.com/jhsuZerion/4f9a9bb44f7021f792a71b6ca6ac934a
-
I know this is a very late reply and we apologize, but for those folks who were struggling with making API calls work on Android, unfortunately Android JS library doesn't support XmlHttpRequest. Android JS is handled by Android webkit and you will be able to find all the classes supported: https://developer.android.com/reference/android/webkit/package-summary.html
Penny
Please sign in to leave a comment.
Comments
9 comments