0

I'm working currently with the geolocation API.

When allowing my browser - localhost or webhost- to fetch my position, my console.log returns :

Unknown error acquiring position

I can't figure out since I have authorized the connection and my code seems to be clean since I have fetched it from the Mozilla official MDN.

Here my client.js :

      var options = {
            enableHighAccuracy: true,
            timeout: 5000,
            maximumAge: 0
      };

      function success(pos) {
        var crd = pos.coords;

        console.log('Votre position actuelle est :');
        console.log(`Latitude : ${crd.latitude}`);
        console.log(`Longitude: ${crd.longitude}`);
        console.log(`Plus ou moins ${crd.accuracy} mètres.`);
      };

      function error(err) {
        console.warn(`ERROR(${err.code}): ${err.message}`);
      };

      navigator.geolocation.getCurrentPosition(success, error, options);

any hint would be great, Thanks

Webwoman
  • 10,196
  • 12
  • 43
  • 87

2 Answers2

2

Google has changed (May-June) some policies on API key (now a valid API key is required). If you go to Firefox and type about:config and search for property geo.wifi.uri, you'll see google url with no api key. You can change the url to another geo service (https://location.services.mozilla.com/v1/geolocate?key=test), but for production, it is better to perform direct ajax call as below:

    const res = await fetch('https://location.services.mozilla.com/v1/geolocate?key=test').then(el=>el.json())
    const point = [res.location.lat, res.location.lng]

Once Firefox solves the issue you could go back to use the geolocation API as before.

(see also html geolocation: Unknown error acquiring position)

angelito
  • 412
  • 4
  • 10
0

Currently this is what I use to get my users lat/lng

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function (position) {
    var pos = {
      lat: position.coords.latitude,
      lng: position.coords.longitude
    };
    console.log("Lat: " + pos.lat + ". Lng: " + pos.lng);
  })
}

This uses the browsers geolocation and returns the users lat and lng. Then I use google's reversegeocoding to get the actual address.

Luay
  • 789
  • 4
  • 15
  • thanks, it returns me nothing so far, like "null", no log, nothing – Webwoman Jul 22 '18 at 00:54
  • What browser are you using? And does that browser allow sharing the users location? – Luay Jul 22 '18 at 01:12
  • yep I have just installed Chrome, it works fine on Chrome but not on other browsers - Firefox 61.0.1 _ Opera 54.0.2952.54 -, surely a browser's issue so – Webwoman Jul 22 '18 at 01:20
  • I have installed Google Chrome 67.0.3396.99 more precisely. And I'm connect with my ethernet cable, wifi not enabled I think in my devices, I had problem with an issue about my hardware and Ubuntu 16.04 seems – Webwoman Jul 22 '18 at 01:29
  • That really shouldn't be a problem, have you tried checking if the firefox has privacy mode off and it grants the website the users location. I just tested on firefox and Edge on windows 10 and this code worked like a charm. Firefox did have a little bit of a delay but nothing major. – Luay Jul 22 '18 at 01:37