Click here to Skip to main content
14,667,169 members
Rate this:
Please Sign up or sign in to vote.
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 :-
<!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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100