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 08faa81..cc5d228 100644 --- a/app/src/main/java/com/casic/detector/extensions/AMapLocation.kt +++ b/app/src/main/java/com/casic/detector/extensions/AMapLocation.kt @@ -4,6 +4,7 @@ import com.amap.api.maps.AMapUtils import com.amap.api.maps.model.LatLng import kotlin.math.acos +import kotlin.math.pow /** * 计算两个经纬度之间连线的方位角,因为距离很近(一般是5米内),球面近似为平面 @@ -11,41 +12,41 @@ * 手机坐标轴Y轴反置,所以右下角才是第一象限 * */ fun AMapLocation.calculateAngle(target: LatLng): Int { - //构造直角辅助点C - val auxiliaryPoint = LatLng(target.latitude, this.longitude) + //构造辅助点C + val auxiliaryPoint = LatLng(this.latitude, target.longitude) //AB线段长度 - val distance = AMapUtils.calculateLineDistance( + val AB = AMapUtils.calculateLineDistance( LatLng(this.latitude, this.longitude), target ) //AC线段长度 - val auxiliaryDistance = AMapUtils.calculateLineDistance( + val AC = AMapUtils.calculateLineDistance( auxiliaryPoint, target ) - //求∠A的余弦值 - val cosine = auxiliaryDistance / distance + //BC线段长度 + val BC = AMapUtils.calculateLineDistance( + auxiliaryPoint, target + ) + + //余弦定理求∠BAC的余弦值 + val cosine = (AB.pow(2) + AC.pow(2) - BC.pow(2)) / (2 * AB * AC) //反余弦得弧度 val radian = acos(cosine) //弧度转角度,度=弧度×180°/π val angle = (radian * 180) / Math.PI - - //判断方位 - val direction = if (this.longitude < target.longitude && this.latitude < target.latitude) { - //东北方 - 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 direction + return angle.toInt() +// val direction = if (target.longitude > this.longitude && target.latitude > this.latitude) { +// angle.toInt() +// } else if (target.longitude > this.longitude && target.latitude < this.latitude) { +// (angle + 90).toInt() +// } else if (target.longitude < this.longitude && target.latitude < this.latitude) { +// (angle + 180).toInt() +// } else { +// (angle + 270).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 08faa81..cc5d228 100644 --- a/app/src/main/java/com/casic/detector/extensions/AMapLocation.kt +++ b/app/src/main/java/com/casic/detector/extensions/AMapLocation.kt @@ -4,6 +4,7 @@ import com.amap.api.maps.AMapUtils import com.amap.api.maps.model.LatLng import kotlin.math.acos +import kotlin.math.pow /** * 计算两个经纬度之间连线的方位角,因为距离很近(一般是5米内),球面近似为平面 @@ -11,41 +12,41 @@ * 手机坐标轴Y轴反置,所以右下角才是第一象限 * */ fun AMapLocation.calculateAngle(target: LatLng): Int { - //构造直角辅助点C - val auxiliaryPoint = LatLng(target.latitude, this.longitude) + //构造辅助点C + val auxiliaryPoint = LatLng(this.latitude, target.longitude) //AB线段长度 - val distance = AMapUtils.calculateLineDistance( + val AB = AMapUtils.calculateLineDistance( LatLng(this.latitude, this.longitude), target ) //AC线段长度 - val auxiliaryDistance = AMapUtils.calculateLineDistance( + val AC = AMapUtils.calculateLineDistance( auxiliaryPoint, target ) - //求∠A的余弦值 - val cosine = auxiliaryDistance / distance + //BC线段长度 + val BC = AMapUtils.calculateLineDistance( + auxiliaryPoint, target + ) + + //余弦定理求∠BAC的余弦值 + val cosine = (AB.pow(2) + AC.pow(2) - BC.pow(2)) / (2 * AB * AC) //反余弦得弧度 val radian = acos(cosine) //弧度转角度,度=弧度×180°/π val angle = (radian * 180) / Math.PI - - //判断方位 - val direction = if (this.longitude < target.longitude && this.latitude < target.latitude) { - //东北方 - 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 direction + return angle.toInt() +// val direction = if (target.longitude > this.longitude && target.latitude > this.latitude) { +// angle.toInt() +// } else if (target.longitude > this.longitude && target.latitude < this.latitude) { +// (angle + 90).toInt() +// } else if (target.longitude < this.longitude && target.latitude < this.latitude) { +// (angle + 180).toInt() +// } else { +// (angle + 270).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 052bd42..4b0146b 100644 --- a/app/src/main/java/com/casic/detector/view/MainActivity.kt +++ b/app/src/main/java/com/casic/detector/view/MainActivity.kt @@ -1557,7 +1557,7 @@ locationManager.getCurrentLocation(false, object : OnGetLocationListener { override fun onAMapLocationGet(location: AMapLocation?) { location?.apply { - "[${longitude}, ${latitude}]".show(context) +// "[${longitude}, ${latitude}]".show(context) renderDataPoint(this) } } @@ -1583,6 +1583,9 @@ LatLng(it.lat.toDouble(), it.lng.toDouble()) ) +// withContext(Dispatchers.Main){ +// "$angle".show(context) +// } val formatDistance = String.format("%.2f", distance) dataPoints.add(RadarScanView.DataPoint(angle, formatDistance.toFloat())) }