Titanium Community Questions & Answer Archive

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

How to send request to Google Maps and retrieve results?

Hi,

I just started with Appcelerator and man, am I lovin' it! My first app uses location information and, in addition the the current geolocation, I want the user to be able to enter or search for a location with Google Maps, then use the returned information to either prompt with a list of possible matches or, if the returned value is unique, update the Titanium.Map with that latitude and longitude. Suggestions?

— asked October 28th 2010 by Mark Pemburn
  • google
  • maps
  • mobile
  • search
0 Comments

2 Answers

  • Couldn't you also just use the built-in geocoding routines? From the kitchen sink:

    var addr = "2065 Hamilton Avenue San Jose California 95125";
    
    Titanium.Geolocation.forwardGeocoder(addr,function(evt) {
      Ti.API.info('in forward ')
      forwardGeo.text = "lat:"+evt.latitude+", long:"+evt.longitude;
      Titanium.Geolocation.reverseGeocoder(evt.latitude,evt.longitude,function(evt) {
        var text = "";
        for (var i = 0; i < evt.places.length; i++) {
          text += "" + i + ") " + evt.places[i].address + "\n"; 
        }
        Ti.API.info('Reversed forward: '+text);
      });
    });
    
    — answered October 30th 2010 by Tim Poulsen
    permalink
    2 Comments
    • Thanks Tim. I looked at the Kitchen Sink example but it didn't really 'sink' in that this was what they were doing. I'll give it a try!

      — commented October 30th 2010 by Mark Pemburn
    • Update next day: Tried this code and it does work to find the coords of a known location. If I replace the 'var addr =' line with var addr = "Guilford Ave", however, it returns one set of coordinates – and I know that there are lots of Guilford Ave's, so which one? Unless there's some way to get the underlying MapKit code to spit out an array of approximate matches, I think I'll stick with the method I'm using now. (see below).

      — commented October 31st 2010 by Mark Pemburn
  • Aha! Found what I need! Hope this helps the next seeker who comes this way:

    Google has a service called "The Google Geocoding API". It's pretty easy to use. Instructions and examples here:

    Geocoding API

    Here's a bit of code from my preliminary tests. It's searching for streets named "Guilford Ave" and telling it to return the results as xml (See output Here). The results are crunched by the Ti XML parser and, in this case, it spits out the 'formatted_address' elements – a means to show the user the list of near-matches. Still a lot of work to go to get what I want but this is a promising start!

    var xhr = Titanium.Network.createHTTPClient();
    xhr.onload = function() {
        var result = this.responseText;
        var xml = Ti.XML.parseString(result);
    
        var params = xml.documentElement.getElementsByTagName("formatted_address");
        for (var i=0; i<params.item.length; i++) {
            Ti.API.log('Param '+i+': Name: '+params.item(i).text);
        }
    
    };
    
    xhr.onerror = function() {
        Titanium.API.info('error');
    };
    
    xhr.open("GET","http://maps.googleapis.com/maps/api/geocode/xml?address=Guilford+Ave&sensor=true");
    xhr.send();
    

    Mark

    — answered October 29th 2010 by Mark Pemburn
    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.