Skip to content
yarcub edited this page Aug 22, 2011 · 5 revisions

Gives access to the browser Geolocation API.

##Javascript

If the flash movie isn't embed dynamically we can add the following line at the bottom of the HTML.

 <script src="js/osmosis.js"/>
 <!-- (...) -->
 <script> osmosis = new Osmosis("myflashobjectId"); </script>

In those cases were it's embedded via javascript we must be sure that the element is already present in the DOM before creating an Osmosis instance.

<script>
	var flashvars = {};
	var params = {
		menu: "false",
		scale: "noScale",
		allowFullscreen: "true",
		allowScriptAccess: "always", //<-- make sure script access is allowed
		bgcolor: "",
	};
	var attributes = {
		id:"myflashobjectId"
	};
	
   /*Wait until swf object does is work and make sure the listener is added before the DOM injection*/
	swfobject.addLoadEvent(function(){osmosis = new Osmosis(attributes.id);});
	
	swfobject.embedSWF(
		"flashfile.swf", 
		"altContent", "100%", "100%", "10.0.0", 
		"expressInstall.swf", 
		flashvars, params, attributes);
</script>

##Actionscript We can have several instances of Osmosis living in the same HTML document (e.g. multiple embedded swfs). Because of this behavior each AS3 instance need the javascript instance name on the global namespace.

 import os.flash.osmosis.geolocation.*;
 
 /* (...) */

 var geolocation:Geolocation = new Geolocation("osmosis"); //pass javascript variable name
 
 if(geolocation.isSupported()){

    /* Browser geolocation API is assynchronous */
    geolocation.addEventListener(GeoLocationEvent.SUCCESS, onGeoSuccess);
    geolocation.addEventListener(GeoLocationErrorEvent.ERROR, onGeoError);

    /* Start listening for position changes */
    geolocation.watchPosition();
  }

  /* (...) */

  function onGeoSuccess(event:GeoLocationEvent):void{
        trace("latitude: " + event.coordinates.latitude);
        trace("longitude: " + event.coordinates.longitude);
   }
Clone this wiki locally