The HTML5 Flash Marriage: Geolocation

I probably don’t need to tell you that there’s a lot of buzz (and fuzz) about how HTML5 is going to kill Flash. You probably know how I feel about this… I think the web is big enough for both of them… Even better… I think they could potentially complement each other!

Geolocation is a good example. HTML5 is going to get a geolocation API that works just beautifully even on devices with no GPS. Flash based applications will (currently) only get access to geolocation APIs when targeting the AIR runtime on mobile. Some browsers (I only know of Firefox 3.5 on Mac and the WebKit browser on the Nexus One) already support the HTML5 geolocation API… So why not use that to get geo information into your Flash based application?

It’s actually extremely easy to do…

But first a little bit of background as to why I was looking for this functionality. I’m actually building “this demo app” that needs the geolocation in order to have the functionality I was looking for. I want this app to work in as many places as possible. With the Flash Platform I can build this for my browser and my desktop. For the Apple phone I can export it as a native app and for the Nexus One I can use the device browser with Flash Player 10.1.

Now… How does it work? The HTML5 geolocation API is extremely easy to use and, like I said earlier, you don’t even need to have a GPS enabled device.

function getGEO()
{
	// First check if your browser supports the geolocation API
	if (navigator.geolocation)
	{
		alert("HTML 5 is getting your location");
		// Get the current position
		navigator.geolocation.getCurrentPosition(function(position)
		{
			lat = position.coords.latitude
			long = position.coords.longitude;
			// Pass the coordinates to Flash
			passGEOToSWF(lat, long);
		});
	} else {
		alert("Sorry... your browser does not support the HTML5 GeoLocation API");
	}
}
function passGEOToSWF(lat,long)
{
	alert("HTML 5 is sending your location to Flash");
	// Pass the coordinates to mySWF using ExternalInterface
	document.getElementById("mySWF").passGEOToSWF(lat,long);
}

In my Flash application, I’m using ExternalInterface so I can communicate between JavaScript and my SWF. When my Google Maps component is ready, I call the GetGEO JavaScript method:

ExternalInterface.call("getGEO");

When the JavaScript method gets a result from the geolocation API, it will pass it on to the passGEOToSWF method. In my Flash application, I just listen for that method call and then call the code to update the map.

ExternalInterface.addCallback("passGEOToSWF", onPassGEOToSWF);

If you don’t have an HTML5 ready browser, check out this video of the application running in Firefox 3.6. Even cooler is that this also works in the browser on my Flash Player 10.1 enabled Nexus One (Please note that the network is slower on the N1 and thus it isn’t able to keep up with loading new map images. This has nothing to do with Flash Player 10.1 or the application.):

If you have Firefox 3.5 or newer installed, you can give it a try yourself: http://www.webkitchen.be/geolocation. I’m sure there are other browsers out there that also already have the geolocation API but this is the only one I tested on the Mac.

Hopefully this gives you a good idea of how HTML5 and Flash can also just work together (instead of killing each other ;-)). Flash on!

UPDATE: While writing this blog post, @robertbak pinged me on Twitter saying that he wrote a library to use in your Flex applications. Check it out on the Flex Exchange.

UPDATE: For the source files check out this blogpost.

Tags: , , , , , , ,

39 Responses to “The HTML5 Flash Marriage: Geolocation”

  1. Riaan 05. Mar, 2010 at 4:29 pm #

    Nice one Serge, my eyes are slowly opening up to the possibilities!

    Now to just get hold of a copy of Player 10.1 for my Nexus One… :)

  2. jacopogio 05. Mar, 2010 at 4:46 pm #

    Cool Serge ! I tried the Demo and I was localized at just 20m !! but with a laptop !!?? Amazing ! With FF3.7a2

  3. Steven Verbruggen 05. Mar, 2010 at 5:11 pm #

    are you home tonight?
    :)

  4. Serge Jespers 05. Mar, 2010 at 5:12 pm #

    @Steven: LOL! Thankfully it’s not 100% accurate ;-)

  5. James 05. Mar, 2010 at 5:27 pm #

    Cool idea. Localized to just 160 miles!

  6. J. 05. Mar, 2010 at 5:46 pm #

    Phew, I’m thought I staying in Singapore Parliament.

  7. Map solutions 05. Mar, 2010 at 7:28 pm #

    Wow, this is billion. I thought I using Google Earth.

    Thanks for sharing.

  8. Robert Bak 05. Mar, 2010 at 8:32 pm #

    It also works on Chrome with small modifications. It would be more than great to get this feature into AIR 2 at some point, with a mandatory pop-up asking for users permission. It needs to gather some info about the surrounding wi-fi signals so it’s just a step away from the flash.net.NetworkInfo ;)

  9. JP 05. Mar, 2010 at 10:08 pm #

    Doesn’t work for me. Unsupported browser message. Safari 4.0.3

  10. Wouter Verweirder 08. Mar, 2010 at 2:32 pm #

    Heya,

    Nice demo. I’ve actually posted another HTML5 – Flash example on my blog, demonstrating cross-browser drag- and drop. Now you can drag/drop files, text, … from other applications on your swfs running in the browser :-)

    Wouter

  11. Al 08. Mar, 2010 at 3:29 pm #

    Cool app, but it missed me pretty badly. I’m just north of Chicago and it placed me somewhere on the eastern plains of Colorado. About 850 miles off.

  12. MickyMike 08. Apr, 2010 at 7:46 pm #

    Hi there, thanks for this post!

    I need some help to know the cardinal direction of my mobile device, for example to know if my device is pointing north.

    I mean it’s relative to geolocation, but I’m not sure. Any feedback is welcome!

    Many Thanks.

Trackbacks/Pingbacks

  1. Greg Wilson - 05. Mar, 2010

    RT Tip @techmeme @sjespers: Blogged: The HTML5 Flash Marriage: Geolocation (HTML5 and Flash can also work together) http://bit.ly/aEKABk

  2. Dan Sumption - 05. Mar, 2010

    #HTML5 is going to perfectly completely #Flash: http://bit.ly/bAJLkm via @sjespers

  3. Emma Wilkinson - 05. Mar, 2010

    RT @sjespers: Wait… What? #HTML5 and #Flash can just work together? #GetOutaHee :D http://bit.ly/bAJLkm

  4. Rachel Luxemburg - 05. Mar, 2010

    Wait, what? @sjespers got HTML5 and Flash to work together?! How could this be? http://bit.ly/bFliht

  5. Robin Charney - 05. Mar, 2010

    RT @sjespers: Wait… What? #HTML5 and #Flash can just work together? #GetOutaHee :D http://bit.ly/bAJLkm

  6. HTML-Flash Geo | Ryan Stewart – Rich Internet Application Mountaineer - 05. Mar, 2010

    [...] HTML and Flash working together to find each other. Geolocation is a good example. HTML5 is going to get a geolocation API that works just beautifully even on devices with no GPS. Flash based applications will (currently) only get access to geolocation APIs when targeting the AIR runtime on mobile. Some browsers (I only know of Firefox 3.5 on Mac and the WebKit browser on the Nexus One) already support the HTML5 geolocation API… So why not use that to get geo information into your Flash based application? [...]

  7. HTML5 ir Flash jungtuv?s! | ifmi - 06. Mar, 2010

    [...] Taip pat jis parod? kaip HTML5 ir Flash’as gali veikti kartu. [...]

  8. Umut Muhaddisoglu - 08. Mar, 2010

    The HTML5 Flash Marriage: Geolocation – http://bit.ly/dr6JJO #html5 #flash

  9. Marco Kuiper - 08. Mar, 2010

    RT @umutm The HTML5 Flash Marriage: Geolocation – http://bit.ly/dr6JJO #html5 #flash

  10. Pinceladas da Web - 08. Mar, 2010

    RT @umutm: The HTML5 Flash Marriage: Geolocation – http://bit.ly/dr6JJO #html5 #flash

  11. Arturo Paracuellos - 09. Mar, 2010

    The HTML5 Flash Marriage: Geolocation | Serge Jespers http://bit.ly/9l5PgL

  12. Demetrio Fortman - 09. Mar, 2010

    RT @sjespers: The HTML5 Flash Marriage: Geolocation http://bit.ly/bAJLkm

  13. mauriciomassaia - 09. Mar, 2010

    RT @sjespers: The HTML5 Flash Marriage: Geolocation http://bit.ly/bAJLkm

  14. seacloud9 - 10. Mar, 2010

    The #HTML5 Flash Marriage: #Geolocation: http://bit.ly/aBAode

  15. seacloud9 - 10. Mar, 2010

    The #HTML5 Flash Marriage: #Geolocation http://bit.ly/aBAode

  16. Jérémie Anderlin - 11. Mar, 2010

    RT @umutm: The HTML5 Flash Marriage: Geolocation – http://bit.ly/dr6JJO #html5 #flash

  17. HTML5 and Flash can work together insists Adobe evangelist [Video] - SlashGear - 17. Mar, 2010

    [...] however?  ”Adobe platform evangelist” Serge Jespers reckons so, and he’s thrown together the code to demonstrate [...]

  18. Mario Vasquez - 18. Mar, 2010

    Geolocation – The HTML 5 Flash Marriage: http://bit.ly/bTnYbO Works on newer browsers and the Nexus One (Flash Player 10.1).

  19. Rodrigo - 18. Mar, 2010

    Flash y HTML5: geolocalización http://bit.ly/bTnYbO . Está padre pero dice que estoy del otro lado de la ciudad.

  20. html5watcher - 18. Mar, 2010

    RT @xumerio: Flash y HTML5: geolocalización http://bit.ly/bTnYbO . Está padre pero dice que estoy del otro lado de la ciudad.

  21. ActionScriptter - 18. Mar, 2010

    The HTML5 Flash marriage : geolocation. http://www.webkitchen.be/2010/03/05/the-html5-flash-marriage-geolocation/

  22. The HTML5 Flash Marriage » Garuna Web Designer - 19. Mar, 2010

    [...] http://www.webkitchen.be/  Powered by Max Banner Ads  Share and Enjoy: [...]

  23. Adobe defends its mobile strategy | VisionMobile :: blog - 30. Mar, 2010

    [...] less powerful feature phones we’ve got Flash Lite, and all of these platforms will demonstrate Flash living happily with HTML5 where it’s [...]

  24. Jim Tochterman - 31. Mar, 2010

    RT @agup: Interesting geolocation article on HTML 5 + Flash (via @DasFX) http://bit.ly/bTnYbO

  25. BCS - 31. Mar, 2010

    RT @jtochterman: RT @agup: Interesting geolocation article on HTML 5 + Flash (via @DasFX) http://bit.ly/bTnYbO

  26. Nick Furness - 01. Apr, 2010

    RT @agup: Interesting geolocation article on HTML 5 + Flash (via @DasFX) http://bit.ly/bTnYbO

  27. Sergej - 08. Apr, 2010

    The HTML5 Flash Marriage: Geolocation http://tinyurl.com/ykfrymd

Leave a Reply