Jmocke
Jmocke

Reputation: 271

Get user Geolocation from web browser asp.net

i have this code in asp.net web form

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


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

<asp:Button ID="Button1" OnClientClick="getLocation()" runat="server" Text="Try It" />

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

i want to display the current of the gps position, when the user clicks on the button.

This code does not work as it should.

Any help

Upvotes: 0

Views: 7322

Answers (1)

Ayoub
Ayoub

Reputation: 50

Your script doesn't recognize "demo" element because it is ran before the page loads completely. Simply put your script tag after <p id="demo"></p>.

Plus, disable Postback on the button by adding the following:

<asp:Button ID="Button1" OnClientClick="getLocation();return false;" runat="server" Text="Try It" />

Upvotes: 1

Related Questions