如何测试一个地理位置是否在“现实世界"中被访问过? [英] How to test if a geographic location has been visited in the "real world"?

查看:13
本文介绍了如何测试一个地理位置是否在“现实世界"中被访问过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我已经有了这个问题的答案,但我花了很长时间才找到它,所以我想我会分享它,特别是因为 某人问我,但在 不相关的问题

OK, so I already have the answer to this question but it took me a long time to reach it so I thought I would share it, particularly since someone asked me but under an unrelated question

我为引导步行创建了一个基于 Phonegap 的导航应用程序,它使用 GPS 跟踪用户在预定义路线周围的位置.我的路线在路线周围的特定地理位置放置了触发器,这些触发器根据用户的当前位置向用户提供指示.问题是 GPS 不是超级准确或可靠,所以即使我允许在 20 米左右的位置周围有一个访问半径",但实际测试导致这些触发器有时会被错过,因为 GPS 位置更新会稍早发生用户输入访问半径并稍稍稍晚一些.我尝试增加半径,但这意味着触发器触发得太早,无法与用户的当前位置相关.

I created a Phonegap-based navigation app for guided walks that tracks the user's location around a predefined route using GPS. My routes have triggers placed at specific geo-locations around the route which give instructions to the user based on their current location. The problem is that GPS is not super accurate or reliable, so even though I allowed a "visiting radius" around those locations of around 20 metres, real-world testing resulted in those triggers sometimes being missed because a GPS position update would occur slightly before the user entered the visiting radius and slightly afterwards. I tried increasing the radius but this meant that the trigger fired too early to be relevant to the user's current position.

那么我该如何以使用真实世界"GPS 数据的方式解决这个问题?

So how do I solve this in a way that works using "real-world" GPS data?

推荐答案

我偶然发现了 这个奇妙的页面由可移动类型的地理空间计算公式.更好的是,大多数公式 已经用 Javascript 编写 这对我的 Phonegap 应用程序来说非常酷.但是,引起我注意的公式是 this one 用于计算两点之间的跨轨距离.就我的应用程序和实际使用而言,这意味着即使 GPS 更新频率低到无法错过目标位置的半径,我也可以根据最近位置和最近位置之间的路径计算用户是否访问了目标它的前身.

I stumbled upon this fantastic page by Movable Type of formulae for geospatial calculations. Even better, most of the formulae are already written in Javascript which was super-cool for my Phonegap app. However, the formula that caught my attention was this one for calculating the cross-track distance between two points. In terms of my app and real-world use, this means that even if the GPS updates are infrequent enough to miss the radius of my target location, I can calculate if the user visited the target based on the path between the most recent position and its predecessor.

编辑 24/04/15:我已经修复了 constrainedCrossTrackDistance 函数中的一个错误,因此任何使用它的人都应该将他们的实现更新为这个答案中的那个.

EDIT 24/04/15: I've fixed a bug in the constrainedCrossTrackDistance function, so anyone using it should update their implementation to the one in this answer.

JS 库没有包含这个公式,所以我扩展了 Movable Type 库来实现它:

The JS library didn’t include this formula, so I extended the Movable Type library to implement it:

/**
 * Calculates distance of a point from a great-circle path (also called cross-track distance or cross-track error)
 * 
 * Formula: dxt = asin(sin(d13/R)*sin(b13-b12)) * R
 * where 
 *  d13 is distance from start point to third point
 *  b13 is (initial) bearing from start point to third point
 *  b12 is (initial) bearing from start point to end point
 *  R is the earth's radius
 * 
 * @param {LatLon} startPoint - Point denoting the start of the great-circle path
 * @param {LatLon} endPoint - Point denoting the end of the great-circle path
 * @param {Number} [precision=4] - no of significant digits to use for calculations and returned value
 * @return {Number} - distance in km from third point to great-circle path
 */
LatLon.prototype.crossTrackDistance = function(startPoint, endPoint, precision){
    var R = this._radius;
    var d13 = startPoint.distanceTo(this, 10);
    var b13 = startPoint.bearingTo(this).toRad();
    var b12 = startPoint.bearingTo(endPoint).toRad();
    var d = Math.asin(Math.sin(d13/R)*Math.sin(b13-b12)) * R;
    return d.toPrecisionFixed(precision);
}

但是,实际测试再次表明这并不能完全发挥作用.问题是这个函数给出了误报,因为它没有考虑两个端点和半径形成的边界框.这导致我添加了一个进一步的函数来将横向距离限制在该边界框内:

However, real-world testing again showed this didn’t quite do the job. The problem was that this function was giving false positives because it didn’t take it into consideration the bounding box formed by the two end points and the radius. This led me to add a further function to constrain the crosstrack distance to within that bounding box:

/**
 * Calculates distance of a point from a great-circle path if the point is within the bounding box defined by the path.
 * Otherwise, it returns the distance from the point to the closest end of the great-circle path.
 * 
 * @param {LatLon} startPoint - Point denoting the start of the great-circle path
 * @param {LatLon} endPoint - Point denoting the end of the great-circle path
 * @param {Number} [precision=4] - no of significant digits to use for calculations and returned value
 * @return {Number} - distance in km from third point to great-circle path
 */
LatLon.prototype.constrainedCrossTrackDistance = function(startPoint, endPoint, precision){

  var bAB = startPoint.bearingTo(endPoint);
  var bAB_plus_90 = Geo.adjustBearing(bAB, 90);
  var bAB_minus_90 = Geo.adjustBearing(bAB, -90);
  var bAC = startPoint.bearingTo(this);
  var bBC = endPoint.bearingTo(this);
  var dAC = startPoint.distanceTo(this, 10);
  var dBC = endPoint.distanceTo(this, 10);
  if(Geo.differenceInBearings(bAC, bBC) > 90 && ((bBC > bAB_plus_90 && bAC < bAB_plus_90) || (bAC > bAB_minus_90 && bBC < bAB_minus_90))){
    return Math.abs(this.crossTrackDistance(startPoint, endPoint, precision));
  }else if((bBC < bAB_plus_90 && bAC < bAB_plus_90) || (bBC > bAB_minus_90 && bAC > bAB_minus_90)){
    return Math.abs(dBC);
  }else if((bBC > bAB_plus_90 && bAC > bAB_plus_90) || (bBC < bAB_minus_90 && bAC < bAB_minus_90)){
    return Math.abs(dAC);
  }else{
    return (Math.abs(dBC) < Math.abs(dAC) ? Math.abs(dBC) : Math.abs(dAC));
  }
}

这可用于确定目标位置是否已在真实场景中被访问过:

This can then be used to determine if the target position has been visited in a real-world scenario:

// Calculate if target location visited
    if(
        currentPos.distanceTo(targetPos)*1000 < tolerance ||
        (prevPos && Math.abs(targetPos.constrainedCrossTrackDistance(prevPos, currentPos)*1000) < tolerance)
    ){
        visited = true;
    }else{
        visited = false;
    }

这里有一个小提琴说明了一个用例

我花了很长时间和大量测试才想出这个,所以我希望它可以对其他人有所帮助:-)

It took me a long time and lot of testing to come up with this so I hope it can be of help to other people :-)

这篇关于如何测试一个地理位置是否在“现实世界"中被访问过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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