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

I am writing a simple code to test the geolocation feature in HTML5; I am practicing the code from www.w3schools.com and it is working fine in the website. But when I write the same code in a textpad and save it as HTML, after asking permission for sharing the location it is showing blank page. Is it something like I need to host the web-page in some server and then only it will work?
the code I am using is :-
HTML
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button  önclick="getLocation()">Try It</button>
<script>
//var x=document.getElementById("demo");
function getLocation()
  {
  if (navigator.geolocation)
    {
	alert("it works");
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else
  {
  alert("Geolocation is not supported by this browser.");
  }
  }
function showPosition(position)
  {
  alert("Latitude: " + position.coords.latitude + 
  "<br />Longitude: " + position.coords.longitude);	
  }
</script>
</body>
</html>



Please help............
Thanks
Jashobanta
Posted
Updated 28-Jul-20 19:02pm

After a little testing and research, I found this: http://stackoverflow.com/questions/5423938/html-5-geo-location-prompt-in-chrome[^]. I happened to have Python installed, so I copied a test file to the Python directory, started the web server the link mentions with "python -m SimpleHTTPServer", and confirmed it worked. With my Chrome browser, I did see that Chrome was blocking the Geolocation request from the local file.

Also, another interesting link - http://thoughtresults.com/geolocation-api[^]
Scott
 
Share this answer
 
v2
navigator.geolocation.getCurrentPosition(success => {
alert("Good to go");
}, failure => {
if (failure.message.startsWith("Only secure origins are allowed")) {
alert("Failed, Secure login issue");
}
});



As updated policies over web componants by google, Geolocation can only be used with secure origins i.e, using HTTPS connection.(Strongly recommended) Although i am too working to find a alternate way.
 
Share this answer
 

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