Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.");
   }
  • The error message is: 'A Geolocation request can only be fulfilled in a secure context.'
  • My localhost url: 'http://localhost/voice-of-mankind/'
  • My subdomain url: 'http://vom.local.com/'
like image 509
Zahid Avatar asked Dec 18 '22 23:12

Zahid


1 Answers

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.

like image 68
liamlows Avatar answered Dec 20 '22 12:12

liamlows