diff --git a/app/src/main/java/com/casic/detector/extensions/AMapLocation.kt b/app/src/main/java/com/casic/detector/extensions/AMapLocation.kt index efa6785..08faa81 100644 --- a/app/src/main/java/com/casic/detector/extensions/AMapLocation.kt +++ b/app/src/main/java/com/casic/detector/extensions/AMapLocation.kt @@ -3,24 +3,15 @@ import com.amap.api.location.AMapLocation import com.amap.api.maps.AMapUtils import com.amap.api.maps.model.LatLng -import com.casic.detector.model.CalculateResult import kotlin.math.acos /** - * 计算两个经纬度之间连线的方位角,因为距离很近(一般是3米内),球面近似为平面 - * C B - * |-------/ - * | / - * | / - * | / - * | / - * | / - * | / - * |/ - * A + * 计算两个经纬度之间连线的方位角,因为距离很近(一般是5米内),球面近似为平面 + * A(探测仪所在位置),B(最近的标识器位置),C(辅助点位置) + * 手机坐标轴Y轴反置,所以右下角才是第一象限 * */ -fun AMapLocation.calculateDirection(target: LatLng): CalculateResult { - //构造直角辅助点 +fun AMapLocation.calculateAngle(target: LatLng): Int { + //构造直角辅助点C val auxiliaryPoint = LatLng(target.latitude, this.longitude) //AB线段长度 @@ -30,10 +21,10 @@ //AC线段长度 val auxiliaryDistance = AMapUtils.calculateLineDistance( - LatLng(auxiliaryPoint.latitude, auxiliaryPoint.longitude), target + auxiliaryPoint, target ) - //求余弦值 + //求∠A的余弦值 val cosine = auxiliaryDistance / distance //反余弦得弧度 @@ -43,42 +34,18 @@ val angle = (radian * 180) / Math.PI //判断方位 - val direction = if (target.latitude > this.latitude && target.longitude > this.longitude) { + val direction = if (this.longitude < target.longitude && this.latitude < target.latitude) { //东北方 - "东偏北" - } else if (target.latitude < this.latitude && target.longitude < this.longitude) { - //西南方 - "西偏南" - } else if (target.latitude > this.latitude && target.longitude < this.longitude) { + angle.toInt() + } else if (this.longitude > target.longitude && this.latitude < target.latitude) { //西北方 - "西偏北" + (angle + 90).toInt() + } else if (this.longitude > target.longitude && this.latitude > target.latitude) { + //西南方 + (angle + 180).toInt() } else { - "东偏南" + //东南方 + (angle + 270).toInt() } - return CalculateResult(angle.toInt(), direction, distance.toInt()) -} - -fun AMapLocation.calculateAngle(target: LatLng): Int { - //构造直角辅助点 - val auxiliaryPoint = LatLng(target.latitude, this.longitude) - - //AB线段长度 - val distance = AMapUtils.calculateLineDistance( - LatLng(this.latitude, this.longitude), target - ) - - //AC线段长度 - val auxiliaryDistance = AMapUtils.calculateLineDistance( - LatLng(auxiliaryPoint.latitude, auxiliaryPoint.longitude), target - ) - - //求余弦值 - val cosine = auxiliaryDistance / distance - - //反余弦得弧度 - val radian = acos(cosine) - - //弧度转角度,度=弧度×180°/π - val angle = (radian * 180) / Math.PI - return angle.toInt() + return direction } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/extensions/AMapLocation.kt b/app/src/main/java/com/casic/detector/extensions/AMapLocation.kt index efa6785..08faa81 100644 --- a/app/src/main/java/com/casic/detector/extensions/AMapLocation.kt +++ b/app/src/main/java/com/casic/detector/extensions/AMapLocation.kt @@ -3,24 +3,15 @@ import com.amap.api.location.AMapLocation import com.amap.api.maps.AMapUtils import com.amap.api.maps.model.LatLng -import com.casic.detector.model.CalculateResult import kotlin.math.acos /** - * 计算两个经纬度之间连线的方位角,因为距离很近(一般是3米内),球面近似为平面 - * C B - * |-------/ - * | / - * | / - * | / - * | / - * | / - * | / - * |/ - * A + * 计算两个经纬度之间连线的方位角,因为距离很近(一般是5米内),球面近似为平面 + * A(探测仪所在位置),B(最近的标识器位置),C(辅助点位置) + * 手机坐标轴Y轴反置,所以右下角才是第一象限 * */ -fun AMapLocation.calculateDirection(target: LatLng): CalculateResult { - //构造直角辅助点 +fun AMapLocation.calculateAngle(target: LatLng): Int { + //构造直角辅助点C val auxiliaryPoint = LatLng(target.latitude, this.longitude) //AB线段长度 @@ -30,10 +21,10 @@ //AC线段长度 val auxiliaryDistance = AMapUtils.calculateLineDistance( - LatLng(auxiliaryPoint.latitude, auxiliaryPoint.longitude), target + auxiliaryPoint, target ) - //求余弦值 + //求∠A的余弦值 val cosine = auxiliaryDistance / distance //反余弦得弧度 @@ -43,42 +34,18 @@ val angle = (radian * 180) / Math.PI //判断方位 - val direction = if (target.latitude > this.latitude && target.longitude > this.longitude) { + val direction = if (this.longitude < target.longitude && this.latitude < target.latitude) { //东北方 - "东偏北" - } else if (target.latitude < this.latitude && target.longitude < this.longitude) { - //西南方 - "西偏南" - } else if (target.latitude > this.latitude && target.longitude < this.longitude) { + angle.toInt() + } else if (this.longitude > target.longitude && this.latitude < target.latitude) { //西北方 - "西偏北" + (angle + 90).toInt() + } else if (this.longitude > target.longitude && this.latitude > target.latitude) { + //西南方 + (angle + 180).toInt() } else { - "东偏南" + //东南方 + (angle + 270).toInt() } - return CalculateResult(angle.toInt(), direction, distance.toInt()) -} - -fun AMapLocation.calculateAngle(target: LatLng): Int { - //构造直角辅助点 - val auxiliaryPoint = LatLng(target.latitude, this.longitude) - - //AB线段长度 - val distance = AMapUtils.calculateLineDistance( - LatLng(this.latitude, this.longitude), target - ) - - //AC线段长度 - val auxiliaryDistance = AMapUtils.calculateLineDistance( - LatLng(auxiliaryPoint.latitude, auxiliaryPoint.longitude), target - ) - - //求余弦值 - val cosine = auxiliaryDistance / distance - - //反余弦得弧度 - val radian = acos(cosine) - - //弧度转角度,度=弧度×180°/π - val angle = (radian * 180) / Math.PI - return angle.toInt() + return direction } \ No newline at end of file diff --git a/app/src/main/java/com/casic/detector/view/MainActivity.kt b/app/src/main/java/com/casic/detector/view/MainActivity.kt index 5e6c9cc..2da8b1e 100644 --- a/app/src/main/java/com/casic/detector/view/MainActivity.kt +++ b/app/src/main/java/com/casic/detector/view/MainActivity.kt @@ -1754,7 +1754,7 @@ } /** - * 计算并渲染数据点 + * 计算并渲染数据点。点太多采用协程计算,不然会有点卡顿 * */ private fun renderDataPoint(location: AMapLocation) { lifecycleScope.launch(Dispatchers.IO) {