Diagathe Josué
Diagathe Josué

Reputation: 12036

navigator.geolocation.getCurrentPosition returns "Unknown error acquiring position"

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

Upvotes: 0

Views: 1859

Answers (2)

angelito
angelito

Reputation: 451

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)

Upvotes: 2

Luay
Luay

Reputation: 814

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.

Upvotes: 0

Related Questions