-1

I am trying to learn to use the geolocation using javascript. However, the script does not work as i thought. Below is my code :

<!doctype html>
<html>
<title> Testing Geolocation</title>
<head>
  <script>
    function displayLocation(){
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    var div = document.getElementById("myLocation");
    div.innerHTML =" You are at Latitude "+latitude+" and longitude "+longitude);}          
    window.onload = getMyLocation;}
    function getMyLocation{
    if(navigator.geolocation){
    navigator.geolocation.getCurrentPosition(displayLocation);}
    else{
    alert("Oops! Geolocation function not supported");}

</script>
</head>
<body>
<div id="myLocation">
Your location will go here.
</div>
</body>
</html>

Hi, after correcting the typo, the script is still not working. i.e. the latitude and longitude is not showing.

  • My guess without an exception is, `navigator.gelocation` is spelt wrong and should be `navigator.geolocation`. – Ash Burlaczenko Feb 26 '13 at 13:17
  • Also as this post explains, http://stackoverflow.com/questions/3397585/navigator-geolocation-getcurrentposition-sometimes-works-sometimes-doesnt, define the `displayLocation` function before `getMyLocation()`. – Ash Burlaczenko Feb 26 '13 at 13:19
  • Hi, after correcting the typo, the script is still not working. i.e. the latitude and longitude is not showing. – user2106416 Feb 26 '13 at 13:44
  • please fix up your sample code - function getMyLocation does not even come close to being well formatted – Jaak Kütt Jan 17 '14 at 21:11

2 Answers2

1

Add argument 'position' to your displayLocation function.

UPD: ..and some typos. Do you use Chrome/FF console for error tracking? Try this:

<script>
function displayLocation(position){
  var latitude = position.coords.latitude;
  var longitude = position.coords.longitude;
  var div = document.getElementById('myLocation');
  div.innerHTML = "You are at Latitude "+latitude+" and longitude "+longitude;
}          

function getMyLocation(){
    if(navigator.geolocation){
        navigator.geolocation.getCurrentPosition(displayLocation);
    } else {
        alert('Oops! Geolocation function not supported');
    }
}

getMyLocation();
</script>
Latikov Dmitry
  • 956
  • 5
  • 8
0

You are missing () for your getMyLocation function, and a final } for that function (and you have an extra } after the window.onload line):

<!doctype html>
<html>
<title> Testing Geolocation</title>
<head>
<script>
    function displayLocation(position){
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
        var div = document.getElementById("myLocation");
        div.innerHTML =" You are at Latitude "+latitude+" and longitude "+longitude;}          

    function getMyLocation(){
        if(navigator.geolocation){
            navigator.geolocation.getCurrentPosition(displayLocation);}
        else{
            alert("Oops! Geolocation function not supported");}
    }

window.onload = getMyLocation;
</script>
</head>
<body>
<div id="myLocation">
Your location will go here.
</div>
</body>
</html>
Robot Woods
  • 5,677
  • 2
  • 21
  • 30