Actionscript & PHP Development

Building apps using Adobe Air, Actionscript 3 and PHP

By
Warning: file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?latlng=,&sensor=false): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home1/foncused/public_html/dev/wp-content/plugins/google-seo-author-snippets/microdata_form.php on line 228

Warning: file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?latlng=,&sensor=false): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home1/foncused/public_html/dev/wp-content/plugins/google-seo-author-snippets/microdata_form.php on line 228
Richard Mackenzie

Using Geolocation in your IOS app with Adobe AIR

GelocationSo you want to integrate Geolocation into your Adobe AIR IOS/Android application. Before you get started you will need to make sure you have the latest release of Adobe AIR (3.5.0.890).
Now we need to import the following code into our app so that the geolocation code is available to us

import flash.sensors.Geolocation;
import flash.events.GeolocationEvent;
									

We need to add in a check to if the the device supports/allows geolocation services. So we display an error message is geolocation is not available, and if it is we run the code to fetch your co-ordinates.  Using the code below I check your location ever 100 milliseconds. You can set it to run at intervals, or just keep running until it gets your location then just remove the event listener.

if(!Geolocation.isSupported)
{
  geotext.text = "Geolocation is not supported on this device.";
}
else
{
  var fl_Geolocation:Geolocation = new Geolocation();
  
  fl_Geolocation.setRequestedUpdateInterval(100);
  fl_Geolocation.addEventListener(GeolocationEvent.UPDATE, UpdateGeolocation);
  
  var i=0
}
									

Now we need to get the co-ordinates to display, with the magic of Google maps API  we can now turn that into a physical address.

function fl_UpdateGeolocation(event:GeolocationEvent):void
{
  fl_GeolocationDisplay.text = "latitude: ";
  fl_GeolocationDisplay.appendText(event.latitude.toString() + "n");
  fl_GeolocationDisplay.appendText("longitude: ");
  fl_GeolocationDisplay.appendText(event.longitude.toString() + "n");
  fl_GeolocationDisplay.appendText("altitude: ");
  fl_GeolocationDisplay.appendText(event.altitude.toString() + "n");
}
									

So now we have our co-ordinates, we can use that and fire of a request to the Google maps API. Basically we just send a request via query sting to the url below with our co-ords inside, and google with send back a massive amount of information in JSON format, which we can then use to pull out any information we need.

function UpdateGeolocation(event:GeolocationEvent):void
{  

fl_Geolocation.removeEventListener(GeolocationEvent.UPDATE, UpdateGeolocation);
////// Google maps api
var myRequestGEO:URLRequest = new URLRequest("http://maps.googleapis.com/maps/api/geocode/json?latlng="+event.latitude.toString()+","+event.longitude.toString()+"&sensor=false");
var myLoaderGEO = new URLLoader();
myLoaderGEO.addEventListener(Event.COMPLETE, onloadGEO);
myLoaderGEO.load(myRequestGEO);

function onloadGEO(evt:Event):void
{
var myDataGEO:Object = com.adobe.serialization.json.JSON.decode(myLoaderGEO.data);

trace(myDataGEO.results[0].address_components[0].long_name);
trace(myDataGEO.results[0].formatted_address);
geotext.text =myDataGEO.results[0].formatted_address;

}
									

Zalebs IOS application

Zalebs IOS application

You can read up more on the Google maps API here. Check back soon for a tutorial on how to decode JSON in AS3 for use inside your IOS/Android app.

 

For my use I just needed the street address and country to information so I can tag all photos taken with my app. Which will hopefully be launching later this year :).

 

And thats it, now your IOS/Android app has Geolocation support.

Warning: file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?latlng=,&sensor=false): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home1/foncused/public_html/dev/wp-content/plugins/google-seo-author-snippets/microdata_form.php on line 228