我应该如何计算一个人是否在使用地理位置的商店50米范围内?

时间:2014-03-03 11:06:19

标签: javascript geolocation mobile-browser

我将商店的纬度和经度保存在数据库中,用户的地理定位是通过手机浏览器中的javascript获得的。

我应该如何计算用户与商店的距离是否在50米以内?

也许(x1-x2)**2+(y1-y2)**2 < a certain threshold

P.S。这就像foursquare checkin。

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

此网站(Calculate distance, bearing and more between Latitude/Longitude points)提供了一个很好的解释和计算公式。你可能想看一下。

答案 2 :(得分:0)

以下是使用javascript的示例:

function calcDistance(p1, p2){
    return Math.sqrt(Math.pow(p2.x-p1.x, 2) + Math.pow(p2.y-p1.y, 2));
}

var store = { x:21, y:36 };
var device = { x:11, y:56 };

console.log( calcDistance(store, device) );
console.log( "Within 50m ? " + (calcDistance(store, device) < 50) );

结果:

  

22.360679774997898
  Within 50m ? true