Zahid
Zahid

Reputation: 604

A Geolocation request can only be fulfilled in a secure context

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.");
   }

Upvotes: 5

Views: 16510

Answers (2)

user3719458
user3719458

Reputation: 386

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 :)

Upvotes: -1

liamlows
liamlows

Reputation: 613

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.

Upvotes: 14

Related Questions