Link to home
Start Free TrialLog in
Avatar of JoachimPetersen
JoachimPetersenFlag for Denmark

asked on

Flex Mobile: Get Gps Coordinates

Hello
I am trying to make an app that will use the mobile's GPS sensor, the problem is that I wan't to make it as a function where it return the coordinate, how can I do this?

I have tried this:
Gps flex code example
private function SetGeoLocation:void
		{
			 if (Geolocation.isSupported)
			 {
				geo = new Geolocation();
				geo.setRequestedUpdateInterval(100);
				geo.addEventListener(GeolocationEvent.UPDATE, geolocationUpdateHandler);
			 }
			 else
			 {
				log.text =  "No geolocation support.";
				log.setTextFormat(format)
			 }
		}
      private function geolocationUpdateHandler(event:GeolocationEvent):void
      {
         log.text = "lat:" + event.latitude.toString() + " - ";
         log.appendText("long:" + event.longitude.toString() + "° - ");
         log.appendText("Accuracy:" + event.horizontalAccuracy.toString() + " m");
         log.setTextFormat(format)
}

Open in new window


How can I change this so I will just have a function where it will return the current gps location insted of having the UpdateHandler do it?
Avatar of Gary Benade
Gary Benade
Flag of South Africa image

geo.setRequestedUpdateInterval(100);

You are asking the app to update the gps coordinates every 100 milliseconds, if you just want a function that returns the GPS coordinates every time you call it you just need to store the GPS coordinates in the update handler and then return those co-ordinates in your new function

// create a private class variable to store the last known GPS coordinate in
private var last_gps_coord:Object;

// this event handler will get called every 100ms, update the private class variable when it is called with the last location data
 private function geolocationUpdateHandler(event:GeolocationEvent):void
{
           this.last_gps_coord = { latitude:event.latitude, longitude:event.longitude, horizontalAccuracy:event.horizontalAccuracy};
}

// call this function when you want the lastest coordinate
public function getGPSCoOrdinates():Object
{
     return this.last_gps_coord;
}

eg.
var cp:Object = yourClassInstance.getGPSCoOrdinates();
log.text = "lat:" + cp.latitude.toString() + " - ";
log.appendText("long:" + cp.longitude.toString() + "° - ");
log.appendText("Accuracy:" + cp.horizontalAccuracy.toString() + " m");
log.setTextFormat(format)

Regards
Gary
Avatar of JoachimPetersen

ASKER

The problem is having the gps to run every 100 mill sec, it will be power consuming, I need to read the gps coordinates from 3 different functions, I essentially neeed the gps result different times(not the same timer.), is this the only was as I would like to have only the newest gps results.
ASKER CERTIFIED SOLUTION
Avatar of Gary Benade
Gary Benade
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, just one last question: why can't I access the specefic result like only the latitude:
getGPSCoOrdinates().latitude? I am new to as3 so is there anything special you have to do to get the value for latitude from the array?
The call returns an object, you should be able to access the individual properties of the object to get what you need

var lastGpsCoord:Object = GpsLocator.getInstance().getGPSCoOrdinates();
var lat:int = lastGpsCoord.latitude;

var cp:Object = GpsLocator.getInstance().getGPSCoOrdinates();
log.text = "lat:" + cp.latitude.toString() + " - ";
log.appendText("long:" + cp.longitude.toString() + "° - ");
log.appendText("Accuracy:" + cp.horizontalAccuracy.toString() + " m");
log.setTextFormat(format)

Open in new window


to answer your last question (I haven't tested this)

(GpsLocator.getInstance().getGPSCoOrdinates()).latitude

Regards
Gary
Do you need any more help with this?

Regards
Gary