ForwardGeocoder error handling / App. crashes
How can I handle a case where the forwardGeocoder fails or not return an address? Neither the Kitchensink nor the docs seem to have anything to say in this context.
I've tried something like this here:
Ti.Geolocation.forwardGeocoder(mySearchQuery, function(e) {
if (e.error) {
// throw error, show alert, etc.
} else {
// success!
}
});
But every time I'm trying to geocode a nonsense address, my whole application crashes. Anyone know how I'm supposed to handle this properly? I'm looking for a simple A: yes, address was successfully geocoded / B: sorry, something went technically wrong OR sorry, could not geocode address –type–of–solution.
Thanks a lot!
7 Answers
-
I was just about to file a ticket concerning this issue and had yesterday qiuckly thrown together a few lines which would always cause a crash but all of a sudden I'm getting lat:0/lng:0, which is acceptable, I guess.WTF? I didn't update or the SDK or anything else for that matter?
-
The crash happens if I give address like '12' and the behaviour is consistent.
-
try{ Ti.Geolocation.forwardGeocoder(mySearchQuery, function(e) { // success! }); }catch(err){ // throw error, show alert, etc. }
See if that works.
-
Ti.Geolocation.forwardGeocoder(location, function(e){ if (e.error) { Ti.UI.createAlertDialog({ title:'Error', message:'Could not find location - please try again.' }).show(); return; } //Do Something with lat/lng here }
-
I have the same issue, trying to forward geocode a nonsense address crashes the app. I reviewed Florian's test case and geocoding "kakakakakakakakakkakakakakakaka" doesn't crash the app, but geocoding something like "cxzcczxczxczxcxzccscxxczcz" does crash it :(
Very strange, neither of the solutions posted here work for me, I just can't catch the error. Anyone have any ideas on this?
-
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 know this thread is from 2 years ago, but I just had to do this task and wanted to post some updated code that works:
function getCoordinates(address, callback) { var xhr = Titanium.Network.createHTTPClient(); var url = 'http://maps.google.com/maps/api/geocode/json?sensor=false&address=' + address; xhr.open('GET', url); xhr.onload = function() { var result = JSON.parse(this.responseText); var location = result.results[0].geometry.location; //Ti.API.info(location.lat); //Ti.API.info(location.lng); callback(location); }; xhr.send(); }