Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find latitude and longitude of current location

I am using this code for finding current location latitude and longitude.

$(document).ready(function() {
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
    }
    else 
    {
        alert('It seems like Geolocation, which is required for this page, is not enabled in your browser.');
    }       
});

function successFunction(position) 
{
    var lat = position.coords.latitude;
    var long = position.coords.longitude;
    alert('Your latitude is :'+lat+' and longitude is '+long);
}

function errorFunction(position) 
{
    alert('Error!');
}

It's working fine in Chrome but not in Mozilla. There is no alert error message either.

like image 751
Maneesh Mehta Avatar asked Jul 09 '13 05:07

Maneesh Mehta


People also ask

Can you search by latitude and longitude on Google Earth?

Use coordinates to search Open Google Earth. In the Search box in the left-hand panel, enter coordinates using one of these formats: Decimal Degrees: such as 37.7°, -122.2° Degrees, Minutes, Seconds: such as 37°25'19.07"N, 122°05'06.24"W.


2 Answers

Try this i hope this will help you:

<!DOCTYPE html>
<html>
  <head>
   <script type="text/javascript">
     function initGeolocation()
     {
        if( navigator.geolocation )
        {
           // Call getCurrentPosition with success and failure callbacks
           navigator.geolocation.getCurrentPosition( success, fail );
        }
        else
        {
           alert("Sorry, your browser does not support geolocation services.");
        }
     }

     function success(position)
     {

         document.getElementById('long').value = position.coords.longitude;
         document.getElementById('lat').value = position.coords.latitude
     }

     function fail()
     {
        // Could not obtain location
     }

   </script>    
 </head>

 <body onLoad="initGeolocation();">
   <FORM NAME="rd" METHOD="POST" ACTION="index.html">
     <INPUT TYPE="text" NAME="long" ID="long" VALUE="">
     <INPUT TYPE="text" NAME="lat" ID="lat" VALUE="">
 </body>
</html>
like image 159
MJ X Avatar answered Sep 24 '22 20:09

MJ X


You can try this code:-

<!DOCTYPE html>
<html>
<body>

<p>Click the button to get your coordinates.</p>

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else { 
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;
}
</script>

</body>
</html>

Reference

like image 21
Ayman Elshehawy Avatar answered Sep 20 '22 20:09

Ayman Elshehawy