Geolocation fallback example

by braincracking

HTML

<ol id="logger">
    <li>logger</li>
</ol>
<div id="map">Mets moi Ă  jour fort</div>

JavaScript

var MY = {
    // logger function, used to write stuff in this window ==>
    log:function(sLog) {
        var oLi = document.createElement('LI');
        oLi.innerHTML = sLog;
        document.getElementById('logger').appendChild( oLi );
    }
};

YUI().use('node-base', function(Y) { // load YUI3, just to use their JS loader
    // give that method a Position object as per the specs with latitude and longitude
    MY.updateMap = function(oPosition) {
        MY.log('Latitude : '+oPosition.coords.latitude);
        MY.log('Longitude : '+oPosition.coords.longitude);
        // displays a google maps tile
        var latitude = oPosition.coords.latitude,
            longitude = oPosition.coords.longitude,
            sUrl = 'http://maps.google.com/maps/api/staticmap?center='+
                latitude+','+longitude+
                '&sensor=false&size=350x350'+
                '&maptype=roadmap&markers='+
                latitude+','+longitude+'6&visible='+
                (+latitude+0.015)+','+(+longitude+0.015);
         document.getElementById('map').innerHTML = '<img src="'+sUrl+'" >';
  
    };
    
    // sub namespace
    MY.geolocation = {};
    // wrapper method to acces to geolocation
    // give him a callback function
    MY.geolocation.getCurrentPosition = function( fSuccess ) {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(fSuccess);
        } else {
            Y.Get.script( 'http://j.maxmind.com/app/geoip.js',
                          // the maxmind script defines 2 methods returning latitude and longitude from the calling IP
                          {onSuccess:function() {
                                  fSuccess.call(this, {
                                     // reproduce Position object signature
                                    coords:{
                                        latitude:geoip_latitude(),
                                        longitude:geoip_longitude()
                         ...