Developing Android Applications with Adobe AIR [54]
var variables:URLVariables = new URLVariables();
variables.q = event.latitude.toString() + "\n"
+ event.longitude.toString();
variables.gflags = "R";
variables.appid = applicationID;
request.data = variables;
request.method = URLRequestMethod.GET;
loader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLocationLoaded);
loader.addEventListener(IOErrorEvent.IO_ERROR, onLocationLoaded);
loader.load(request);
}
function onError(event:IOErrorEvent):void {
trace("error", event);
}
Parse the XML received from the service to get city and country information:
function onLocationLoaded(event:Event):void {
loader.removeEventListener(Event.COMPLETE, onLocationLoaded);
geolocation.removeEventListener(GeolocationEvent.UPDATE, onTravel);
var xml:XML = new XML(event.target.data);
var city:String = xml.Result.city.text();
var country:String = xml.Result.country.text();
trace(xml);
}
The XML comes back with a ResultSet.Result node, which includes a street address, city, and country. For example, this is the result for my office building located in New York City’s Times Square:
This address is actually correct for this particular location. It is not my postal address, but it is where I am currently sitting on the west side of the building.
Other interesting data in the XML is woeid and woetype (“woe” is short for Where On Earth). GeoPlanet (http://developer.yahoo.com/geo/geoplanet/guide/) was developed by Yahoo! as a way to identify some features on Earth in a unique, language-neutral manner. This is particularly important because many places in the world have the same name, such as Paris, France, and Paris, Texas.
woeid is a 32-bit unique and nonrepetitive identifier. Numbers follow a hierarchy such as country, state, and city. woetype is the type used to identify a place—in this case, 11 refers to the postal code.
Twitter and Flickr are currently using the GeoPlanet system.
Maps
Several geocoding systems and companies offer web services for the consumer market. They all provide similar features. A map is received. It is drawn or a composite of satellite pictures or street tiles is drawn, the latter being more common for mobile devices. It can pan and zoom. Geographical locations or points of interest are represented in the form of markers. Additional features include custom itineraries, the display of specific areas in color, driving and biking directions, and business searches.
Some of the better-known geocoding systems are Google Maps, Yahoo! Maps, Bing Maps, GeoNames, and USC Geocoder. As the technology is rapidly growing, this list may soon expand or change. A lot of map services get their information from NAVTEQ and Tele Atlas, companies that sell databases of geodata, or from MaxMind which sells IP geolocation data. Google now has its own full set of geodata, gathered by its street-view cars.
Launching Google Maps
As we previously discussed, you can collect a point location (latitude, longitude) using the Geolocation class, and pass it to the device using a URI handler. It then presents the user with the option of using the native Maps application or launching Google Maps in the browser:
import flash.events.GeolocationEvent;