UNPKG

@tplc/business

Version:

80 lines (70 loc) 2.26 kB
import type { LightMapConfig, LocationRecord } from './types' /** * 使用 Haversine 公式计算两个经纬度坐标之间的距离 * @param lat1 起点纬度 * @param lng1 起点经度 * @param lat2 终点纬度 * @param lng2 终点经度 * @returns 距离(单位:米) */ export const getDistanceByLoc = ( lat1: number = 0, lng1: number = 0, lat2: number = 0, lng2: number = 0, ): number => { const rad1 = (lat1 * Math.PI) / 180.0 const rad2 = (lat2 * Math.PI) / 180.0 const a = rad1 - rad2 const b = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0 const r = 6378140 // 地球半径(米) const distance = r * 2 * Math.asin( Math.sqrt( Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2), ), ) return distance } /** * 获取指定地图配置的点样式 */ const getDotStyleConfig = (mapConfig: LightMapConfig) => { return mapConfig.mapConfig.dotStyle } /** * 计算位置点相对于地图的百分比位置 */ export const getPosition = (item: LocationRecord, mapConfig: LightMapConfig) => { const { latitude, longitude } = item const { boundaries, physicalSize } = mapConfig.mapConfig const { southwest } = boundaries const { xSize, ySize } = physicalSize // 计算该点到西南角的X轴距离(保持纬度不变,只改变经度) const x = getDistanceByLoc(southwest.latitude, southwest.longitude, southwest.latitude, longitude) // 计算该点到西南角的Y轴距离(保持经度不变,只改变纬度) const y = getDistanceByLoc(southwest.latitude, southwest.longitude, latitude, southwest.longitude) return { left: (x / xSize) * 100, bottom: (y / ySize) * 100, } } /** * 获取点的完整样式 */ export const getDotStyle = (item: LocationRecord, mapConfig: LightMapConfig) => { const position = getPosition(item, mapConfig) const style = getDotStyleConfig(mapConfig) return { left: `${position.left}%`, bottom: `${position.bottom}%`, width: `${style.width}rpx`, height: `${style.height}rpx`, backgroundColor: style.backgroundColor, marginLeft: `-${style.width / 2}rpx`, marginBottom: `-${style.height / 2}rpx`, } }