Developing Android Applications with Adobe AIR [53]
Check that it is supported on your device:
if (NetworkInfo.isSupported) {
// network information supported
}
NetworkInfo stores a list of possible network interfaces that you can get by calling:
import flash.net.NetworkInfo;
var network:NetworkInfo = NetworkInfo.networkInfo;
for each (var object:NetworkInterface in network.findInterfaces()) {
trace(object.name);
}
For example, the list of interfaces for the Nexus One, Droid 2, and Samsung Galaxy Tab is:
mobile, WIFI, mobile_mms, mobile_supl, mobile_dun, mobile_hipri
You can find out which method is active, and its name, using the following code:
import flash.net.NetworkInterface;
var network:NetworkInfo = NetworkInfo.networkInfo;
for each (var object:NetworkInterface in network.findInterfaces()) {
trace(object.name);
if (object.active) {
if (object.name == "WIFI") {
// you are using wifi } else {
// you are using GPS
}
}
}
AIR and Android
The AIR API, under the hood, uses the Android LocationManager class to receive location and accuracy information. It also receives additional information about satellites and fixes, but it doesn’t expose this information at the time of this writing.
If you are interested in seeing the Android process, use the Android command logcat, as explained in Chapter 3, and read the logs for SensorManager, Sensors, GpsLocationProvider, and LocationManager:
androidSDK/tools/adb logcat
On Android, if you want to simulate geolocation coordinates, select Settings→Applications→Development→Mock locations. This is a setting you could choose if you are using the emulator to simulate an itinerary without actually being there. As the time of this writing, it is not supported in AIR for Android:
Reverse Geocoding
Unless you are displaying a map, providing an address or nearby points of interest is more tangible than latitude and longitude.
Reverse geocoding is the process of reverse-coding a point location to fetch a readable address and place name. It is widely used in combination with location-based services (LBS) to retrieve local weather data or business information, as well as by public safety services such as Enhanced 911. Such information is not immediately available, but there are free services that provide it.
The Yahoo! Geocoding API is well documented and reliable. You need to apply for an application ID that is required as an argument in your requests. It is not exclusive to mobile use and can be used in the browser, assuming it has a way to detect location. Go to http://developer.yahoo.com/geo/placefinder/ and read the “How Do I Get Started” section to request an ID. You need to log in with a Yahoo! account and fill out the form. Provide the URL of a website, even though it will not be used on the device.
First, add the permission to go out to the Internet:
In this example, I am receiving the latitude and longitude data from GeolocationEvent and passing it along with my required applicationID and gFlags = R for reverse geocoding:
import flash.events.GeolocationEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.sensors.Geolocation;
var geolocation:Geolocation;
const YAHOO_URL:String = "http://where.yahooapis.com/geocode";
const applicationID:String = "GET_YOUR_ID_ON_YAHOO_SITE";
var loader:URLLoader;
Set the geolocation listener:
if (Geolocation.isSupported) {
geolocation = new Geolocation();
geolocation.addEventListener(GeolocationEvent.UPDATE, onTravel);
}
Request reverse geolocation data from the Yahoo! service passing the coordinates:
function onTravel(event:GeolocationEvent):void {
var request:URLRequest = new URLRequest(YAHOO_URL);