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:

<uses-permission android:name=”android.permission.INTERNET” />

In this example, I am receiving the latitude and longitude data from Geolocation Event 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);
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:

<Result>
<quality>99</quality>
<latitude>40.757630</latitude>
<longitude>-73.987167</longitude>
<offsetlat>40.757630</offsetlat>
<offsetlon>-73.987167</offsetlon>
<radius>500</radius>
<name>40.7576303 -73.98716655000001</name>
<line1>230 Rodgers &amp; Hammerstein Row</line1>
<line2>New York, NY 10036-3906</line2>
<line3/>
<line4>United States</line4>
<house>230</house>
<street>Rodgers &amp; Hammerstein Row</street>
<xstreet/>
<unittype/>
<unit/>
<postal>10036-3906</postal>
<neighborhood/>
<city>New York</city>
<county>New York County</county>
<state>New York</state>
<country>United States</country>
<countrycode>US</countrycode>
<statecode>NY</statecode>
<countycode>NY</countycode>
<hash/>
<woeid>12761367</woeid>
<woetype>11</woetype>
<uzip>10036</uzip>
</Result>

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.


Posted

in

by

Tags: