Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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!

— asked April 22nd 2010 by Florian Plank
  • crash
  • error
  • forwardgeocoder
  • handling
  • iphone
  • mobile
0 Comments

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?

    — answered April 26th 2010 by Florian Plank
    permalink
    0 Comments
  • The crash happens if I give address like '12' and the behaviour is consistent.

    — answered June 7th 2010 by Gaurav Srivastva
    permalink
    0 Comments
  • try{
      Ti.Geolocation.forwardGeocoder(mySearchQuery, function(e) {
            // success! 
      });
    }catch(err){
      // throw error, show alert, etc.
    }
    

    See if that works.

    — answered June 7th 2010 by Sj Singh
    permalink
    0 Comments
  • 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
    }
    
    — answered July 9th 2010 by Roger Chapman
    permalink
    0 Comments
  • 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?

    — answered August 19th 2010 by Leigh Kayley
    permalink
    1 Comment
    • I ended up using the Google API directly. Works fine so far.

      — commented August 20th 2010 by Florian Plank
  • 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();
    
    — answered November 30th 2011 by Tony Lukasavage
    permalink
    0 Comments
  • 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();
    
        }
    
    — answered January 27th 2012 by Justin Toth
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.