0

I am writing an web application that requires the current geolocation of user. For identifying geolocation I have used the following code that uses HTML5 geolocation api. I have taken this code from W3schools.

<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to get your coordinates:</p>
<button onclick="getLocation()">Try It</button>
<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>
</body>
</html>

I have run this code with Tomcat, but it is not working neither in Firefox nor in Chrome. Then I have tried this code with "Try It Yourself" in W3Schools and it is not working even there also neither in Firefox nor in Chrome. I am behind the proxy.

I have introduced an alert() statement at the start of body of the function showPosition() and it did not get called. So it seems this function is not getting called at all. I have no clue how to fix that. I desperately need to fix that. I have previously posted that problem but got no response probably because of not framing the problem clearly. So I am again posting the problem.

Is there any other way to get the geolocation of the users who are accessing this application throuogh computers. So please help. Thank you.

EDIT: I have hosted this file on Tomcat. Each time I am executing, it is giving error code 3 i.e. timeout. But I have executed the same Javascript two months back. Then it executed perfectly. Is it a problem with current version of my browser?

Joy
  • 4,197
  • 14
  • 61
  • 131

1 Answers1

1

there is nothing for the proxy to do with the html5 geolocation api, it's detecting location based on your gps device if you have one on your machine, second it goes to use the wifi device, finally it uses the internet ip to detect your location, and if you connecting using proxy it'll show the location of your proxy. your issue may be because you didn't give the permission to the localhost on your browser, check it again and retry your code.

Prog Mania
  • 615
  • 9
  • 14
  • Sorry Sir, I don't know how to give permission to the localhost on my browser. Could you please clarify? – Joy Jun 02 '13 at 11:43
  • you can open your settings in chrome, and search for content settings, then scroll down to location options. – Prog Mania Jun 02 '13 at 11:45
  • Yes Sir, I have checked but in Content Setting I found no such thing related to localhost. – Joy Jun 02 '13 at 11:50
  • @Joy can you open your console and check if you have any errors thrown ? – Prog Mania Jun 02 '13 at 11:51
  • Actually Sir, nothing is getting printed in console. It seems that function showPosition is not getting called. I have no clue what is going on. I have introduced another alert() statement just before the line "navigator.geolocation.getCurrentPosition(showPosition);". It seems to be the last statement executed. – Joy Jun 02 '13 at 12:04
  • Yes Sir, got it. Error code 3 is coming. But I have no idea how to fix this error. – Joy Jun 02 '13 at 12:08
  • @Joy one last check, are you running the page in a web context, or just from your disc? – Prog Mania Jun 02 '13 at 12:11
  • I am running from Web Context, not from disk. – Joy Jun 02 '13 at 17:12
  • @Joy try this out https://mdn.mozillademos.org/en-US/docs/WebAPI/Using_geolocation$samples/Geolocation_Live_Example?revision=388351 – Prog Mania Jun 03 '13 at 06:43
  • Yes I have tried it but, when I clicked on the button "Show my location" it is just printing "Locating..." and nothing else. – Joy Jun 03 '13 at 06:49
  • See I am using Firefox of version 16.0.2. Is there any problem with this version? – Joy Jun 03 '13 at 06:49
  • @Joy I'm using ff 21 and it's working fine with me, you may try using chrome. it's working fine too. – Prog Mania Jun 04 '13 at 06:41
  • Have you executed in FF 16.0.2? So is it a browser related problem? – Joy Jun 04 '13 at 07:25
  • @Joy unfortunately i don't have ff 16 on my machine. – Prog Mania Jun 04 '13 at 07:26