HTML5 Geolocation - Detect location within a specific area

时光毁灭记忆、已成空白 提交于 2019-12-25 18:01:10

问题


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 something like that?


回答1:


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.



来源:https://stackoverflow.com/questions/6388047/html5-geolocation-detect-location-within-a-specific-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!