开发者

HTML5 Geolocation - Detect location within a specific area

开发者 https://www.devze.com 2023-03-14 03:27 出处:网络
I haven\'t had much of an opportunity to look at HTML5 Geolocation yet, but I\'m curious: Is it possible to build a web app that can detect when a user enters a certain area (perimeter) and then retur

I haven't had much of an opportunity to look at HTML5 Geolocation yet, but I'm curious: Is it possible to build a web app that can detect when a user enters a certain area (perimeter) and then return a message or someth开发者_StackOverflow社区ing like that?


You can use watchPosition to get periodic updates of the browser's location, and then in your callback, test to see if the new position is within your area of interest. So if you've defined a function isInArea that checks a position to see if it's in your area of interest, you could do something like:

function positionCallback(position) {
  if (isInArea(position)) {
    alert("Honey, I'm home!");
  }
}

function handleError(error) {
  alert("Error!")
}

// Request repeated updates.
var watchId = navigator.geolocation.watchPosition(positionCallback, handleError);

Based on Example of requesting repeated position updates from w3c.

0

精彩评论

暂无评论...
验证码 换一张
取 消