判断用户打卡是否再打卡范围以内(支持半径、多边形状态)

//判断用户是否再半径内的坐标
function isPointInCircle(lng1, lat1, lng2, lat2, radius) {
  const distance = getDistance(lng1, lat1, lng2, lat2);
  return distance <= radius;
}

// 距离计算函数
function getDistance(lng1, lat1, lng2, lat2) {
  // 使用合适的距离计算公式,如球面三角法或哈弗赛德公式
  // 这里仅作示例,可以替换为你自己的计算方式
  const dx = lng2 - lng1;
  const dy = lat2 - lat1;
  return Math.sqrt(dx*dx + dy*dy);
}

//多边形点判断
function isPointInPolygon(lng, lat, polygon) {
  let inside = false;
  for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
    const xi = polygon[i].lng, yi = polygon[i].lat;
    const xj = polygon[j].lng, yj = polygon[j].lat;

    const intersect = ((yi > lat) !== (yj > lat)) &&
      (lng < ((xj - xi) * (lat - yi) / (yj - yi)) + xi);
    if (intersect) inside = !inside;
  }
  return inside;
}

export {
	getIdData,
	getAddress,
	getDateTime,
	getDate,
	getDate1,
	getIdDataPuls,
	isPointInCircle,
	isPointInPolygon
}

我这里是写在tool里面,到时候直接调用就可以了,如果只是单一使用,可以直接在页面中直接使用,isPointInCircle(半径判断)

isPointInPolygon(多边形判断)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
判断打卡范围,需要通过定位功能获取当前位置的经纬度,然后与打卡范围进行比较。可以参考以下步骤: 1. 使用Android中的LocationManager来获取当前位置的经纬度。 2. 定义打卡范围,可以使用一个圆形区域表示,可以由圆心坐标和半径表示。 3. 计算当前位置与圆心之间的距离,如果距离小于等于半径,则说明当前位置在打卡范围内。 4. 根据需要进行相应的处理,如打卡成功或失败等。 以下是一个简单的示例代码: ```java LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); double latitude = currentLocation.getLatitude(); double longitude = currentLocation.getLongitude(); // 打卡范围圆心坐标 double centerLatitude = 31.225434; double centerLongitude = 121.482536; // 打卡范围半径(单位:米) double radius = 500; // 计算当前位置与圆心之间的距离 double distance = distance(latitude, longitude, centerLatitude, centerLongitude); if (distance <= radius) { // 打卡成功 } else { // 打卡失败 } // 计算两个经纬度之间的距离 private double distance(double lat1, double lng1, double lat2, double lng2) { double earthRadius = 6378137; // 地球半径(单位:米) double radLat1 = Math.toRadians(lat1); double radLat2 = Math.toRadians(lat2); double a = radLat1 - radLat2; double b = Math.toRadians(lng1) - Math.toRadians(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 *= earthRadius; return s; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值