mostlylucid

STATIC ARCHIVE of mostlylucid.co.uk of old
posts - 906, comments - 722, trackbacks - 11

My Links

News

Archives

Post Categories

Misc. Coding

Dead Simple Geolocation…

Messing around with this stuff of late, @TheCodeJunkie requested it so here’s a really simple way to get a location and show a map :). This version uses the W3C Geolocation API on browsers that support it (FireFox 3.5, Safari, iPhone, Opera) and falls back to using Google Gears on other browsers (including IE).

Just a really dumb sample right now but who knows, someone might find it useful! I’ll update later with some of the more advanced stuff I’m working on in this area…

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2"></script>
    <script src="Scripts/gears_init.js" type="text/javascript"></script>
    <script type="text/javascript">
        function SetLocation() {
            if (typeof (navigator.geolocation) != "undefined") {
                geo = navigator.geolocation;
                positionOptions = { maximumAge: 600000 };
            }
            else {
                if (!window.google || !google.gears) {
                returnUrl = this.location.href;
                location.href = "http://gears.google.com/?action=install&message=Gears now enabled!" +
                    "&return=" + returnUrl;
                }
                geo = google.gears.factory.create('beta.geolocation');
                positionOptions = {enableHighAccuracy:true, maximumAge: 600000};
            }
            geo.getCurrentPosition(GetMap, handleError,positionOptions);
        }
        function GetMap(position) {
            latitude = position.coords.latitude;
            longitude = position.coords.longitude;
            map = new VEMap('myMap');
            map.HideDashboard();
            map.LoadMap(new VELatLong(latitude, longitude), 15, 'h', false);
            var shape = new VEShape(VEShapeType.Pushpin, map.GetCenter()); shape.SetTitle('You are here!'); map.AddShape(shape);
        }
        function handleError(positionError) {
            alert('Attempt to get location failed: ' + positionError.message);
        }      
    </script>
</head>
<body onload="SetLocation();">
    <div id='myMap' style="position: relative; width: 1000px; height: 1000px;">
    </div>
</body>
</html>

Print | posted on Monday, October 12, 2009 9:56 PM |

Feedback

# re: Dead Simple Geolocation…

Cool, I tried to do something about a year ago, in a bid to make a simple GPS mapping system using the OS maps on multimap, and such like... this was just hacked for the iPhone, but I couldn't get it reliable enough, and had to use a 3rd party app extension... it was a horrid mess.

Never even knew this API existed. That's the weekend schedule ruined. :)

Oh, and welcome home! Enjoy Chunk, I know some folks there :)
11/6/2009 1:23 AM | AlanG
Comments have been closed on this topic.

Powered by: