Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use watchPosition () to monitor visitors' activities

Shulou Source: shulou.com Published: 2022-06-01 03:52:18 09月22日 Update

Today, the editor will share with you the relevant knowledge about how to use watchPosition () to monitor the activities of visitors. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

All the examples we have used so far depend on this getCurrentPosition () method. However, geolocation objects have another method, watchPosition (), which allows you to track the movement of visitors by returning to the updated location when the location changes.

The watchPosition () input parameter is the same as getCurrentPosition (). However, watchPosition () may trigger the success function multiple times-the first time a location is fetched, and each time a new location is detected. Let's see how it works:

Watching Position

/ / Set global variable

Var watchID

Function showPosition () {

If (navigator.geolocation) {

WatchID = navigator.geolocation.watchPosition (successCallback)

} else {

Alert ("Sorry, your browser does not support HTML5 geolocation.")

}

}

Function successCallback (position) {

ToggleWatchBtn [XSS _ clean] = "Stop Watching"

/ / Check position has been changed or not before doing anything

If (prevLat! = position.coords.latitude | | prevLong! = position.coords.longitude) {

/ / Set previous location

Var prevLat = position.coords.latitude

Var prevLong = position.coords.longitude

/ / Get current position

Var positionInfo = "Your current position is (" + "Latitude:" + position.coords.latitude + "," + "Longitude:" + position.coords.longitude + ")"

Document.getElementById ("result") [xss_clean] = positionInfo

}

}

Function startWatch () {

Var result = document.getElementById ("result")

Var toggleWatchBtn = document.getElementById ("toggleWatchBtn")

ToggleWatchBtn.onclick = function () {

If (watchID) {

ToggleWatchBtn [XSS _ clean] = "Start Watching"

Navigator.geolocation.clearWatch (watchID)

WatchID = false

} else {

ToggleWatchBtn [XSS _ clean] = "Aquiring Geo Location..."

ShowPosition ()

}

}

}

/ / Initialise the whole system (above)

_ window.onload = startWatch

Start Watching

These are all the contents of the article "how to use watchPosition () to monitor the activities of visitors". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Location knowledge articles visitors methods activities monitoring content updates different big same success functions parameters geography geographical location most objects that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker Shulou Technology OPPO Reno MySQL