Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

jquery - Why the location is being repeated on my PWA with JavaScript?

I have a problem to get the location of my PWA's users. The location that is being taken are repeated and is not being updated.

$("#storeLocation").click(function () {
  navigator.geolocation.getCurrentPosition(
    function (e) {
      a = e.coords.latitude + "," + e.coords.longitude; 

      // HERE I STORE THE COORDS IN MY DATABASE
    },
    { enableHighAccuracy: true, timeout: Infinity, maximumAge: 0 }
  );
});

The button is working because I could see that when I click on it is storing the location, but is storing always the same location.

question from:https://stackoverflow.com/questions/65945142/why-the-location-is-being-repeated-on-my-pwa-with-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to continually monitor the user's location, use the Geolocation API method, watchPosition(). It works in the same way, except it will fire multiple times as the location of the user changes, or gets more accurate.

$("#storeLocation").click(() => {
  const posOpts = { enableHighAccuracy: true, timeout: Infinity, maximumAge: 0 };
  navigator.geolocation.watchPosition((e) => {
    const position = a = e.coords.latitude + "," + e.coords.longitude;
    // Store position here
  }, posOpts);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...