Titanium Community Questions & Answer Archive

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

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!

— asked August 26th 2011 by James Jeffery
  • api
  • forwardgeocoder
  • geocoding
  • geolocation
1 Comment
  • Oh, I'd like to add that the problem seems to only affect Android. The API seems to geocode for iOS just fine.

    — commented August 26th 2011 by James Jeffery

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();
    
    — answered November 30th 2011 by Tony Lukasavage
    permalink
    1 Comment
    • Just to complete your code, latitude and longitude could be extracted from the json response this way :
      var longitude = json.Placemark[0].Point.coordinates[0];
      var latitude = json.Placemark[0].Point.coordinates[1];

      Easy !

      — commented January 8th 2012 by Stéphane Rouilly
  • 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 here

    Here'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();
        }
    }
    
    — answered September 2nd 2011 by Mike Robinson
    permalink
    3 Comments
    • Well maybe more accurately, we don't hit their servers…..but we might be using their API key behind the scense to call the service.

      — commented September 2nd 2011 by Mike Robinson
    • This function doesn't seem to return anything. The appcelerator forward geocoding isn't working and I am desperate for a solution.

      — commented November 30th 2011 by Nate Maxfield
    • Its May 2013, and this code (Geocoder example) still does not work (maybe earlier in the day, but not when I am trying it), No matter what address I enter I get 'Unable to Geocode This Address'

      — commented May 20th 2013 by Jens Moller
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.