Im trying to get the toggle switch to DISABLE and STOP the geolocation routine…
i get null object error when geo is set to new Geolocation — what am I missing here?
private function gogeo():void
{
if (Geolocation.isSupported)
{
var geo:Geolocation = new Geolocation();
geo.setRequestedUpdateInterval(2000);
geo.addEventListener(GeolocationEvent.UPDATE, geolocationUpdateHandler);
}
else
{
trace("No geolocation support.");
}
}
private function geolocationUpdateHandler(event:GeolocationEvent):void
{
trace("lat:" + event.latitude.toString() + " - ");
trace("long:" + event.longitude.toString() + "° - ");
trace("Accuracy:" + event.horizontalAccuracy.toString() + " m");
}
protected function toggleswitch1_changeHandler(event:Event):void
{
if (toggleswitch1.selected == false) {
trace("The function should STOP");
geo.removeEventListener(GeolocationEvent.UPDATE, geolocationUpdateHandler);
geo = null;
} else {
trace("The function should RESTART");
geo = new Geolocation();
geo.addEventListener(GeolocationEvent.UPDATE, geolocationUpdateHandler);
}
}
Define geo outside of the function:
variables are only available within their ‘scope’ – eg: if you define a variable inside a function, it is only available from within that function
edit:
To define geo as global:
In top level of your app (ie in the main mxml file), add a public declaration:
And then access it from anywhere in your app using:
instead of just “geo”; where MyAppClass is the name of your app (ie: the name of your main mxml class)