| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- /**
- * 给经纬度数组增加 0~5cm 的随机偏移
- * @param {Array<Array<number>>} points [[lng, lat], ...]
- * @param {number} maxOffsetCm 最大偏移(厘米),默认 5cm
- * @returns {Array<Array<number>>}
- */
- export function offsetGeoPoints(points, maxOffsetCm = 5) {
- return points.map(([lng, lat]) => {
- // cm -> meter
- const maxOffsetMeter = maxOffsetCm / 100
- // 随机偏移(-max ~ +max)
- const offsetMeterLng = (Math.random() * 2 - 1) * maxOffsetMeter
- const offsetMeterLat = (Math.random() * 2 - 1) * maxOffsetMeter
- // 米 -> 度
- const latOffset = offsetMeterLat / 111320
- const lngOffset = offsetMeterLng / (111320 * Math.cos(lat * Math.PI / 180))
- return [
- lng + lngOffset,
- lat + latOffset
- ]
- })
- }
- /**
- * 经纬度数组转字符串
- * 输入格式:[[lng, lat], ...]
- * 输出格式:lat,lnglat,lng
- * @param {Array<Array<number>>} points
- * @param {number} fixed 保留小数位数(可选)
- * @returns {string}
- */
- export function geoPointsToString(points, fixed = 6) {
- return points
- .map(([lng, lat]) =>
- `${lat.toFixed(fixed)},${lng.toFixed(fixed)}`
- )
- .join(';') + ';'
- }
- /**
- * 将 startTime 加上指定秒数,返回同样格式的 endTime
- * @param {string} startTime - 格式: "YYYY-MM-DD HH:mm:ss"
- * @param {number} secondsToAdd - 要加的秒数
- * @returns {string} - 格式: "YYYY-MM-DD HH:mm:ss"
- */
- export function addSeconds(startTime, secondsToAdd) {
- const [datePart, timePart] = startTime.split(' ')
- let [year, month, day] = datePart.split('-').map(Number)
- let [hour, minute, second] = timePart.split(':').map(Number)
- // 转换成总秒数
- let totalSeconds = hour * 3600 + minute * 60 + second + secondsToAdd
- // 处理天数进位
- let extraDays = Math.floor(totalSeconds / 86400)
- totalSeconds %= 86400
- hour = Math.floor(totalSeconds / 3600)
- minute = Math.floor((totalSeconds % 3600) / 60)
- second = totalSeconds % 60
- // 处理日期进位
- const daysInMonth = (y, m) => new Date(y, m, 0).getDate()
- day += extraDays
- while (day > daysInMonth(year, month)) {
- day -= daysInMonth(year, month)
- month += 1
- if (month > 12) {
- month = 1
- year += 1
- }
- }
- const pad = (n) => n.toString().padStart(2, '0')
- return `${year}-${pad(month)}-${pad(day)} ${pad(hour)}:${pad(minute)}:${pad(second)}`
- }
- /** 获取当前时间,格式 "YYYY-MM-DD HH:mm:ss"
- * @returns {string}
- */
- export function getCurrentTime() {
- const now = new Date()
- const pad = n => String(n).padStart(2, '0')
- const year = now.getFullYear()
- const month = pad(now.getMonth() + 1) // 月份从0开始
- const day = pad(now.getDate())
- const hour = pad(now.getHours())
- const minute = pad(now.getMinutes())
- const second = pad(now.getSeconds())
- return `${year}-${month}-${day} ${hour}:${minute}:${second}`
- }
|