HTML Tutorial / Your Location Through the Browser

in #coding4 years ago

Studyingcoding.png
Hey Hive people, how´s it going?
Did you know, you can actually see your location through the browser using HTML?
Yep, its possible.
Basically, I wrote script code into HTML.
Working system is very easy- if you run this HTML code in your Browser and then click to the button what says "Try it", Browser then wants you to allow the positsioning.
pilt.png
In Firefox, I had to click the blue button

If you have allowed the positioning, browser will give to script your position, and HTML will give you your latitude and longitude.

If you are developer you can use this code for giving clients the nearest shop or something other.

CODE:

<html>
<body>
<p id="demo"></p>
    
<div id="grad4" style="text-align: center;"><p>Click the button to get your coordinates.</p>

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

<p id="demo"></p></div>
    
<style>
#grad4 {
  height: 100px;
  background-color: red;
  background-image: linear-gradient(-90deg, blue, yellow); 
  
}
</style>
<script>
var x = document.getElementById("demo");

function getLocation() {
  if (navigator.geolocation) {
    navigator.geolocation.watchPosition(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>


Picture of this:
pos.png
I brushed my longitude and latitude, for security

Have a nice day!
@m2nnari

Sort:  

kus javascripti rippisid?

see veel beginner java, koolis teinud seda korra ( õpetaja seletas ) :)

Ma ise ei teadnudki, et selline funktsioon eksisteerib. Duno milleks see hea oleks, probs mingi map appi jaoks vms.

Kui mingi e-pood ja mingi map siis saab allowida seda funktsiooni ja näitab lähima füüsilise poe või midagi.

I can't understand anything 🙂

sad.

😪😪😪