Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5: Access to geolocation was blocked over insecure connection to http://localhost:3000

I'm using html5 geolocation for my rails app, but when I click on the try it button the following error appears inside the safari browser console under the show web inspector console :

getLocation — localhost:83[blocked] Access to geolocation was blocked over insecure connection to http://localhost:3000.

and here is the code:

<button onclick="getLocation()">Try It</button>

<p id="demo"></p>

<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>
like image 312
Dev Avatar asked Sep 21 '17 08:09

Dev


People also ask

Is it possible to use rails for geolocation?

The relevant code is taken from Google's Geolocation Demo and run through rails. At present, the geolocation occassionally succeeds through Chrome (~10% of the time), always works almost immediately in opera, works ~80% of the time in Firefox and always fails immediately in Safari. In failing browsers, only Safari returns an error in the console.

Why is my connection denied by geolocation setting?

Connection denied by Geolocation Setting. The connection was denied because this country is blocked in the Geolocation settings. How do I correct or change this? Community content may not be verified or up-to-date. Learn more. use a VPN ? You can change/turn off your geolocation in the settings under Privacy and Security.

How do I Turn Off my geolocation?

You can change/turn off your geolocation in the settings under Privacy and Security. Go to Permissions and make any changes to Block or Allow based on the website you are accessing. however the website may be using the IP address of your internet service provider to determine your location.


1 Answers

Safari (unlike Chrome and Firefox) does not allow access to geolocation over the HTTP protocol - only HTTPS. Even for localhost. Thanks a lot Apple.

The solution is to either use another browser in development or serve Rails over HTTPS. You can do that by generating a self-signed certificate and setting up the Rails development server (Webrick or Puma) to serve over HTTPS.

Rails 5 defaults to Puma, while earlier versions used Webrick.

The exact approach varies depending on your OS and which server is in use.

  • Rails 5, Puma & OS-X
  • Rails 4, OS-X / Ubunto, Webrick
like image 175
max Avatar answered Nov 15 '22 15:11

max