Geolocation forwardGeocoder URL is broken
Currently only the URL specified in KitchenSink will geocode successfully. I've dug into the Titanium source code and it looks like the fault is with an Appcelerator server script found at http://api.appcelerator.net/p/v1/geo. I believe the script only allows the "q" query value to match the address specified in the KitchenSink rather than actually looking up that value.
Please devs, fix this!
2 Answers
-
Please see this ticket for details on how we are handling this: TIMOB-4880
In the meantime, you can do the forward geocoding yourself very easily, something like this:
xhr = Titanium.Network.createHTTPClient(); var query = 'Pittsburgh PA 15202'; // or whatever you want to forward geocode xhr.open('GET', 'http://maps.googleapis.com/maps/geo?output=json&q=' + query); xhr.onload = function() { var json = JSON.parse(this.responseText); Ti.API.info(json); }; xhr.send();
-
I've seen the Titanium Geolocation work & not work so many times. I kind of suspect at least on the emulators they send all the requests to their servers and then call Google from their server. This sometimes leads to errors that too many requests have been made (even though I only have tested like 5 times). As the item in the kitchen sink always seems to work….I suspect they simply return the stored value rather than call Google to reduce the number of requests. I don't know if all of this is true, but it is what I believe most likely to have explained the errors I have gotten sometimes while geocoding.
Still, I have found it more reliabe to simply call Google's geocode APIs myself.
You can find the Google documentation hereHere's an example of how to Geocode
//Get LAT & LON from user entered location function getGeoLocation(strLocation) { var lat = 0; var lon = 0; var location = ''; if (!Titanium.Network.online) { return; } else { var xhrGeocode = Titanium.Network.createHTTPClient(); xhrGeocode.setTimeout(120000); xhrGeocode.onload = function (e) { var response = JSON.parse(this.responseText); if (response.status == 'OK' && response.results != undefined && response.results.length > 0) { lat = response.results[0].geometry.location.lat; lon = response.results[0].geometry.location.lng; location = strLocation; } }; xhrGeocode.onerror = function (e) {}; var url = "http://maps.google.com/maps/api/geocode/json?address=" + strLocation.replace(' ', '+'); url += "&sensor=" + (Titanium.Geolocation.locationServicesEnabled == true); xhrGeocode.open("GET", url); xhrGeocode.setRequestHeader('Content-Type', 'application/json; charset=utf-8'); xhrGeocode.send(); } }