18

"A Geolocation request can only be fulfilled in a secure context"

Its great that HTTPS is needed for geolocation but I need to debug on insecure local server before it makes it to live.

I was hoping I could edit Firefoxes 'about:config' to disable this safeguard for debugging. How would I do this? Are there any other/better ways to debug location in insecure context? (mock https)

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
BFalcon
  • 331
  • 1
  • 3
  • 10
  • 1
    this: https://medium.freecodecamp.org/how-to-get-https-working-on-your-local-development-environment-in-5-minutes-7af615770eec sounds like a lot of work, but if you really need it you could try it ;) but im actualy wondering.. I have a project with geolocation on localhost and it actualy works for example in Firefox Developers – Christopher Supertramp Jul 18 '18 at 13:49

2 Answers2

2

You can test on localhost without encryption

Firefox will not send Geolocation over a non secure connection and this behaviour cannot be disabled. However, localhost is considered to be a secure connection so that might be an option when testing. This also explains why Christopher Supertramp could try his code over http- he's on localhost.

This is from Mozilla documentation:

Locally delivered files such as http://localhost and file:// paths are considered to have been delivered securely.

https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts

amundsmark
  • 33
  • 5
  • Welcome to SO! Even if your answer is right, try to explain it a little bit. Besides, you are supporting one external link that can disappear, so better to explain your answer just in case that one disappear. – David García Bodego Oct 22 '19 at 09:50
  • This might work if the hosname is localhost, but what if you are running with a different hostname mapped to 127.0.0.1? – kloddant Jan 14 '20 at 14:24
  • 1
    The behaviour can actually be changed by setting _geo.security.allowinsecure_ to True (tested on Firefox 96) – Mattia Galati Aug 11 '22 at 07:11
  • Thanks @MattiaGalati that's what I was looking for. In FF 110 the flag must be added: it's not present out of the box. – massic80 Feb 14 '23 at 11:02
  • 1
    why does it allow the geolocation, but returns undefined coords? – massic80 Feb 14 '23 at 11:08
1

Im wondering why its still working for me but this is my code:

    function geolocate() {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            function(position) {
                let lat = position.coords.latitude;
                let lng = position.coords.longitude;
                var geolocation = {
                    lat: lat,
                    lng: lng
                };
                document.getElementById('lat').value = lat;
                document.getElementById('lng').value = lng;
                console.log("----------------------------------------------");
                console.log("Found Location: "+ lat + " / " + lng);

                var google_maps_geocoder = new google.maps.Geocoder();
                google_maps_geocoder.geocode(
                    { 'latLng': geolocation },
                    function( results, status ) {
                        let street = results[0].address_components[1].long_name;
                        let number = results[0].address_components[0].long_name;
                        let plz = results[0].address_components[6].long_name;
                        let city = results[3].address_components[0].long_name;
                        //let country = results[7].formatted_address;
                        let full = street+" "+number+", "+plz+" "+city;

                        // write the values in the fields
                        document.getElementById('autocomplete').value = full;
                        document.getElementById('route').value = street;
                        document.getElementById('street_number').value = number;
                        document.getElementById('postal_code').value = plz;
                        document.getElementById('locality').value = city;
                        //document.getElementById('country').value = country;
                        console.log("User Address: "+street+" "+number+", "+plz+" "+city);
                        $.ajax({
                            type: 'POST',
                            url: 'include/set-location.inc.php',
                            data: {lat: lat, lng: lng, street: street, number: number, plz: plz, city: city, full: full},
                            dataType: 'json',
                            success: function(response) {
                                if(response.status === 'success') {
                                    console.log("Saved address in a cookie!");
                                    if (site === "jobs") {
                                        location.reload(true);
                                    }
                                }
                            }
                        });
                    }
                );
            });
      }
    }

the browser "question": the browser "question":

my console: enter image description here

I hope my code will help you ;)