Yesterday I’ve started to experiment with geolocation features built into Firefox 3.5 (check here) and Chrome with Gears installed. What was ment as a Flex Cookbook post grew into a handy library. It supports both Firefox and Chrome (including the loading of the address), includes functions to check the users geolocation, track him, etc.
For the geolocation to work you need to have some javascript code in your page, the library injects the code into the DOM so that you don’t need to do it manually, for those of you who are interested how to do it here’s a cookbook post.
The project is hosted on Google CodeĀ here, where you can also find a detailed documentation. Below you can find a sample application.
A simplest example I could think off:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:geolocation="com.robertbak.geolocation.*"
creationComplete="geo.send()">
<geolocation:GeoLocator id="geo"
applicationID="YOUR_APPLICATION_ID_HERE"/>
<mx:Label text="Lat: {geo.lastResult.coords.latitude}"/>
</mx:Application>
As you can see the whole configuration consists of adding the library and setting the applicationID, it’s usually the same as your main class name (eg. if you create an application as App.mxml, the applicationID should be set to “App”). This is needed so that javascript in the DOM of the page can call your application with the results.
The lastResult implements the IPosition interface:
* coords (ICoordinates)
o latitude (Number)
o longitude (Number)
o altitude (Number)
o accuracy (Number)
o altitudeAccuracy (Number)
o heading (Number)
o speed (Number)
* address (IAddress)
o streetNumber (String)
o street (String)
o premises (String)
o city (String)
o county (String)
o region (String)
o country (String)
o countryCode (String)
* timestamp (Date)
Sample (view source enabled): link

by Sebastien Arbogast
21 May 2010 at 22:05
I’ve just discovered your library and it’s exactly what I was looking for. But I can’t figure out how to make it work in Flex 4. I have to initialize the GeoLocator in ActionScript:
var application:Application = FlexGlobals.topLevelApplication as Application;
geolocator = new GeoLocator();
geolocator.applicationID = application.className;
geolocator.addEventListener(“result”, onGeolocatorResult);
geolocator.addEventListener(“fault”, onGeolocatorFault);
But then when I call geolocator.send(), I don’t receive any event. Am I doing something wrong?
by Robert Bak
26 May 2010 at 13:34
It should work, check the example on google code: http://code.google.com/p/asgeolocation/issues/detail?id=1&can=1 .
The reasons that might make in not work:
1) Javascript disabled
2) The bowser doesn’t support geolocation (Internet Explorer?)