5

I am using HTML5 Geolocation feature. My code is well running in 'localhost' but problem in 'subdomain' .My code below:

 if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition, showError);
  } else {
         console.log("Geolocation is not supported by this browser.");
   }
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Zahid
  • 470
  • 1
  • 3
  • 15
  • 1
    you need to secure your domain with an SSL certificate and call your code with an https url like https://vom.local.com – abraham63 Apr 18 '18 at 10:10
  • 1
    Actually the problem is mentioned in the error message. `A Geolocation request can only be fulfilled in a secure context.`. Please check the following answers: https://stackoverflow.com/questions/37192827/getcurrentposition-doesnt-work-once-deployed and the following one: https://stackoverflow.com/questions/37835805/http-sites-does-not-detect-the-location-in-chrome-issue – Mushfiqur Rahman Apr 18 '18 at 10:20

2 Answers2

14

In most cases, localhost will be treated as a secure URL regardless of HTTP or HTTPS connection. This is to ease the development process and allow you to make use of features that would only be accessible through a secure connection.

However, if you are still having issues with this on localhost or want to access the website remotely via HTTP with geolocation enabled, the following steps should set you up:

  1. Download chrome and paste the following URL into your address bar chrome://flags/#unsafely-treat-insecure-origin-as-secure.

  2. On that page locate the Insecure origins treated as secure flag and add the URL which is failing to load the geolocation in the text box. e.g. example.com or localhost in your case.

  3. Once you've added it, select enable on the right-hand side.

  4. Click the reload changes button and then revisit the website, it should now ask for geolocation.

liamlows
  • 564
  • 1
  • 6
  • 15
  • 3
    any way to do this on firefox? – akhilrawat001 Aug 05 '21 at 10:46
  • 1
    Yes please let us know about firefox also – Dinesh Shekhawat Aug 21 '21 at 08:19
  • Actually someone has posted a similar doubt for firefox also. However according to the answers given to that question, sadly there is no such feature in firefox. An open bug had been filed for the same. Please refer: https://stackoverflow.com/questions/66678123/is-there-an-equivalent-of-insecure-origins-treated-as-secure-of-chrome-flag-in – Dinesh Shekhawat Aug 21 '21 at 08:21
-1

Set the HTTPS environment variable to true, then start the dev server as usual with npm start: if your on Unix just do this HTTPS=true npm start Note that the server will use a self-signed certificate, web browser will stop connecting to the site and will show you an error page with the message, Warning: Potential Security Risk Ahead. I hope this helps happy coding :)

user3719458
  • 346
  • 3
  • 12