Showing posts with label Maps. Show all posts
Showing posts with label Maps. Show all posts

Friday, 27 January 2012

Javascript to request GPS location from web page

There is no direct method natively built in Javascript stack. Don't give up though. I am giving your few lines of code to provide the result you look for. The code runs well on IE9, Chrome, FireFox, Safari on both PC and mobile device platforms.

 
function requestGeoLocation() {
 if (navigator.geolocation) {
 navigator.geolocation.getCurrentPosition(function(loc){
 // do nothing
 //document.getElementById('myloc').innerHTML = 'Location: ' + 
 //    loc.coords.latitude + ', ' +
 //    loc.coords.longitude;
    
        }, function() { alert('request denied or something went wrong.');});
} else { 
 alert('GPS is supported.'); 
}