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?
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); }); });
-
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:
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