Recently I worked on a mobile website. For this website it was very important to determine the geolocation of a visitor.

    After some research I created a simple javascript that allows you to get the latitude and longitude of a visitor.

    geolocation.js [download]

    /**
     * Package: Geolocation
     * Version: 1.0
     * Author: Jeroen Weustink (http://www.jeroenweustink.nl/)
     */
    
    geolocation = {
    
    	lat	: false,
    	lng	: false,
    
    	/**
    	 * Get location of visitor
    	 */
    	get : function()
    	{	
    
    		// Check if navigator.geolocation is available
    		if(navigator.geolocation){
    
    			// Get position
    			navigator.geolocation.getCurrentPosition(function(position){
    
    				// Set lat & lng
    				geolocation.lat = position.coords.latitude;
    				geolocation.lng = position.coords.longitude;
    
    				// Callback
    				geolocation.set();
    
    			});
    
    		} else {
    
    			// No navigator.geolocation support
    			alert('No geolocation support');
    
    	    }
    	},
    
    	/**
    	 * Callback function when location is available
    	 */
    	set : function()
    	{
    		alert(geolocation.lat+'/'+geolocation.lng);
    	}
    }

    All you need to do is run geolocation.get().

    You are free to change the script.

    Enjoy…

    Featured Video

    Twitter updates