Android判断是否定位在合理范围内

最近也是好久没有写博客,也是忙的不行,我们最近项目中加了打卡的功能,所以就涉及到了定位,看你这个人是不是在范围内打卡的,这个判断逻辑,我就在这里简单的记录一下,方便自己下次使用,当然,能帮助大家解决问题更好了!

接下来就是代码了。

/**

* 通过经纬度获取距离(单位:米)

* @param lat1

* @param lng1

* @param lat2

* @param lng2

* @return

*/

private static double EARTH_RADIUS = 6378.137;

 

private static double rad(double d) {

return d * Math.PI / 180.0;

}

 

public static double getDistance(double lat1, double lng1, double lat2,

double lng2) {

double radLat1 = rad(lat1);

double radLat2 = rad(lat2);

double a = radLat1 - radLat2;

double b = rad(lng1) - rad(lng2);

double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)

+ Math.cos(radLat1) * Math.cos(radLat2)

* Math.pow(Math.sin(b / 2), 2)));

s = s * EARTH_RADIUS;

s = Math.round(s * 10000d) / 10000d;

s = s*1000;

return s;

}

这个判断获取到的就是两点之间的距离,然后你再拿着获取的半径比较,如果你计算出来的距离小于等于半径,就说明你在正常范围内,

这里需要注意的是,你调用方法的时候参数不要传乱 了,前两个一对(你定位获取的经度纬度),后两个一对(网络获取的经度纬度),

这两对的顺序无所谓,就是你可以先放网络获取的,也可以先放你自己定位的都行,主要就是注意经度纬度不要放错了就行!!!

下面就是调用,TimeUtil是我的工具类

//通过经纬度获取距离

double distance1 = TimeUtil.getDistance(lat, lon, melatitude, melongitude);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下代码来判断一个是否在CAD块内: ```python def is_point_inside_block(point, block): # point: (x, y, z) coordinates of point # block: list of (x, y, z) coordinates of block vertices # Create a point in infinity inf_point = (float("inf"), float("inf"), float("inf")) # Initialize a counter for intersections count = 0 # Loop through each pair of vertices in the block for i in range(len(block)): v1 = block[i] if i == len(block) - 1: v2 = block[0] else: v2 = block[i+1] # Check if the point is on the same plane as the block face if (v1[0] == v2[0] == point[0] and min(v1[1], v2[1]) <= point[1] <= max(v1[1], v2[1]) and min(v1[2], v2[2]) <= point[2] <= max(v1[2], v2[2])): return True # Check if the point is on the same line as the block edge if (v1[0] == v2[0] and v1[0] == point[0] and min(v1[1], v2[1]) <= point[1] <= max(v1[1], v2[1]) and min(v1[2], v2[2]) <= point[2] <= max(v1[2], v2[2])): return True # Check if the point intersects the block edge if (v1[0] < point[0] <= v2[0] or v2[0] < point[0] <= v1[0]): if v1[1] == v2[1]: x = point[0] y = v1[1] else: slope = (v2[1] - v1[1]) / (v2[0] - v1[0]) x = point[0] y = slope * (x - v1[0]) + v1[1] if y == point[1] and point[2] < max(v1[2], v2[2]): count += 1 # Check if the block edge intersects the ray from the point to infinity if ((v1[2] > point[2]) != (v2[2] > point[2])): if v1[2] == v2[2]: x = v1[0] y = v1[1] else: slope = (v2[1] - v1[1]) / (v2[2] - v1[2]) x = v1[0] y = slope * (x - v1[2]) + v1[1] if y > point[1]: count += 1 return count % 2 == 1 ``` 该函数接受两个参数:点的坐标和块的坐标列表。如果点在块内,函数将返回True。否则,函数将返回False。 请注意,该函数只适用于无限平面上的块,并且假定点和块都不在无限远处。如果点或块在无限远处,则需要使用其他方法来解决这个问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值