Click here to Skip to main content
15,908,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying out the geolocation feature of html5. Here is my code:

XML
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
        var lat = document.getElementById("latitude");
        var lon = document.getElementById("latitude");

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

        function showPosition(position)
        {
            lat.value = position.coords.latitude;

            lon.value = position.coords.longitude;
        }

    </script>

</head>
<body>
    <form>
        <button id="GetGeoLocation" onclick="getLocation()">Get Geo Location</button><br/>
        Latitude: <label id="latitude" /> <br/>
        Longitude: <label id="longitude" />
    </form>
</body>
</html>


On clicking the button the code does not reach the javascript.

Thanks
-
Sameer
Posted
Comments
max_nowak 3-Sep-13 4:31am    
Have a look in the javascript console of your browser because your javascript probably produced an error
sameerkulkarni777 3-Sep-13 7:11am    
It is chrome Version 29.0.1547.62 m. This supports HTML 5.

And I did check the javascript console. It has no errors.

1 solution

I just did a test on your code and the java
script files is getting called

C#
function getLocation() {
    alert("GetLocaiton Function called");

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


if your else part, you have x.innerHTML but there is no element called 'x' in your code ?
 
Share this answer
 
Comments
sameerkulkarni777 3-Sep-13 4:55am    
Thanks Bala,

I rectified that part of var x. But anyways since the code never enters the else section, we never encounter this line.

Even after the change, the code does not work.

Thanks
-
Sameer
Bala Selvanayagam 3-Sep-13 5:35am    
SamKulk

I have put an alert inside the getLocation function and when I click on the button the alert pops up and the else part is executed for me.

Not sure whether this is a browser specific issue and what browser you are using ? did you try different browsers ? please let me know
sameerkulkarni777 3-Sep-13 7:14am    
Hi Bala,

It is chrome Version 29.0.1547.62 m

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900