chuangte_bike_newxcx/pages/index/utils/mapFenceUtil.js

60 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2026-06-01 17:31:04 +08:00
/**
* 运营区电子围栏解析边界计算中心点 map.includePoints 使用
*/
function toMapPoint(lat, lng) {
const latitude = parseFloat(lat)
const longitude = parseFloat(lng)
if (!isFinite(latitude) || !isFinite(longitude)) return null
if (Math.abs(latitude) > 90 || Math.abs(longitude) > 180) return null
return { latitude, longitude }
}
export function parseBoundaryStrToPoints(boundaryStr) {
if (!boundaryStr || typeof boundaryStr !== 'string') return []
try {
const coords = JSON.parse(boundaryStr)
if (!Array.isArray(coords)) return []
return coords
.map((coord) => {
if (!Array.isArray(coord) || coord.length < 2) return null
return toMapPoint(coord[1], coord[0])
})
.filter(Boolean)
} catch (e) {
return []
}
}
export function collectOperationAreaPoints(polyline) {
const list = Array.isArray(polyline) ? polyline : []
const points = []
list.forEach((poly) => {
if (!poly || !poly.isOperationArea || !Array.isArray(poly.points)) return
poly.points.forEach((pt) => {
if (!pt) return
const p = toMapPoint(pt.latitude, pt.longitude)
if (p) points.push(p)
})
})
return points
}
export function calcBoundsCenter(points) {
if (!points || !points.length) return null
let minLat = Infinity
let maxLat = -Infinity
let minLng = Infinity
let maxLng = -Infinity
points.forEach((p) => {
minLat = Math.min(minLat, p.latitude)
maxLat = Math.max(maxLat, p.latitude)
minLng = Math.min(minLng, p.longitude)
maxLng = Math.max(maxLng, p.longitude)
})
return {
latitude: (minLat + maxLat) / 2,
longitude: (minLng + maxLng) / 2
}
}