0

Anyone can please help me Why below given code is not working and How can I resolve it? And sorry my grammatical or spelling mistake.

JS:

<script>

    function getLocation(){
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        }
    }
    function showPosition(position){
        console.log(position.coords.latitude);
        console.log(position.coords.longitude);
    }

</script>
Shah Rushabh
  • 31
  • 2
  • 6

3 Answers3

1
<script>

    function getLocation(){
        if (navigator.geolocation) {
            showPosition(navigator.geolocation.getCurrentPosition());
        }
    }
    function showPosition(position){
        console.log(position.coords.latitude);
        console.log(position.coords.longitude);
    }

</script>

You never called showPosition, If I´m right you want to call showPosition with the value of getCurrentPosition.

Doomenik
  • 868
  • 1
  • 12
  • 29
  • can you please check on my url http://newseinstein.com/Rwork/index.php/Listing where I am wrong? – Shah Rushabh May 15 '17 at 11:37
  • look at the console, getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. See url for more details. Its not based on your code by it self, it just get blocked cause you dont use secure https – Doomenik May 15 '17 at 12:07
1

you should create a div for position and you can add the position innerHtml of the div. just like that;

<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>
0

you don't call getLocation() function you should trigger the get Location() with event for example on click