HTML5地理位置 - 检测特定区域内的位置 [英] HTML5 Geolocation - Detect location within a specific area

查看:179
本文介绍了HTML5地理位置 - 检测特定区域内的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还没有很多机会看看HTML5地理位置,但我很好奇:是否可以构建一个Web应用程序,该应用程序可以检测用户何时进入某个区域(周边),然后返回消息或类似的东西?

解决方案

您可以使用 watchPosition 定期更新浏览器的位置,然后在回调中测试新位置是否位于您感兴趣的区域内。所以如果你定义了一个函数 isInArea 来检查一个位置,看它是否在你感兴趣的区域,你可以这样做:

  function positionCallback(position){
if(isInArea(position)){
alert(亲爱的,我在家!) ;



函数handleError(错误){
alert(Error!)
}

//请求重复更新。
var watchId = navigator.geolocation.watchPosition(positionCallback,handleError);

根据 请求重复位置更新的示例 http://dev.w3.org/geo/api/spec-source.htmlrel =nofollow> w3c 。


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?

解决方案

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.

这篇关于HTML5地理位置 - 检测特定区域内的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆