修复已知问题
This commit is contained in:
@@ -2,9 +2,8 @@ import {useEffect, useState} from "react";
|
||||
import {Map} from '@tarojs/components'
|
||||
import {Search} from '@nutui/icons-react-taro'
|
||||
import {Button, Input} from '@nutui/nutui-react-taro'
|
||||
import Taro from '@tarojs/taro'
|
||||
import {useRouter} from '@tarojs/taro'
|
||||
import {getHjmCar, pageHjmCar} from "@/api/hjm/hjmCar";
|
||||
import {getHjmCar, getHjmCarByCode} from "@/api/hjm/hjmCar";
|
||||
import {HjmCar} from "@/api/hjm/hjmCar/model";
|
||||
import './location.scss'
|
||||
|
||||
@@ -19,55 +18,57 @@ const Location = () => {
|
||||
const [longitude, setLongitude] = useState<any>()
|
||||
const [latitude, setLatitude] = useState<any>()
|
||||
const [scale, setScale] = useState<any>(16)
|
||||
const [showCircles, setShowCircles] = useState<boolean>(false)
|
||||
const [points, setPoints] = useState<any[]>([])
|
||||
|
||||
const onKeywords = (keywords: string) => {
|
||||
setKeywords(keywords)
|
||||
}
|
||||
|
||||
// 获取当前位置
|
||||
const getLocation = async () => {
|
||||
// 通用的坐标字符串转数组函数
|
||||
const parseCoordinateString = (coordStr: string) => {
|
||||
if (!coordStr) return { points: [] };
|
||||
|
||||
try {
|
||||
const res = await Taro.getLocation({
|
||||
type: 'gcj02' //返回可以用于wx.openLocation的经纬度
|
||||
})
|
||||
if (res.latitude) {
|
||||
setLatitude(res.latitude)
|
||||
}
|
||||
if (res.longitude) {
|
||||
setLongitude(res.longitude)
|
||||
}
|
||||
console.log('当前位置:', res.latitude, res.longitude);
|
||||
return res;
|
||||
} catch (err) {
|
||||
console.error('获取位置失败:', err);
|
||||
// 分割坐标点
|
||||
const coordPairs = coordStr.split(';');
|
||||
|
||||
// 转为多边形点数组
|
||||
const points = coordPairs.map(coord => {
|
||||
const [lat, lng] = coord.split(',');
|
||||
return {
|
||||
latitude: parseFloat(lat),
|
||||
longitude: parseFloat(lng)
|
||||
}
|
||||
});
|
||||
|
||||
return { points };
|
||||
} catch (error) {
|
||||
console.error('解析坐标字符串失败:', error);
|
||||
return { points: [] };
|
||||
}
|
||||
}
|
||||
|
||||
// 打开地图选择位置
|
||||
// const chooseLocation = async () => {
|
||||
// try {
|
||||
// const res = await Taro.chooseLocation({
|
||||
// latitude, // 默认纬度
|
||||
// longitude // 默认经度
|
||||
// })
|
||||
// console.log('选择的位置:', res);
|
||||
// } catch (err) {
|
||||
// console.error('选择位置失败:', err);
|
||||
// }
|
||||
// }
|
||||
const reload = () => {
|
||||
const id = Number(params.id);
|
||||
setScale(16)
|
||||
getLocation()
|
||||
setScale(14)
|
||||
// 执行搜索
|
||||
if (keywords) {
|
||||
pageHjmCar({keywords}).then(res => {
|
||||
if (res?.list && res?.list?.length > 0) {
|
||||
const data = res?.list[0];
|
||||
setItem(data)
|
||||
setLatitude(data.latitude)
|
||||
setLongitude(data.longitude)
|
||||
setKeywords(data.code)
|
||||
getHjmCarByCode(keywords).then(data => {
|
||||
console.log('执行搜索', data)
|
||||
setItem(data)
|
||||
setLatitude(data.latitude)
|
||||
setLongitude(data.longitude)
|
||||
setKeywords(data.code)
|
||||
if (data.fence) {
|
||||
// 方法2:使用实际的 fence 数据(如果是字符串格式)
|
||||
const coordStr = data.fence.points || '';
|
||||
|
||||
// 使用通用函数解析坐标字符串
|
||||
const { points } = parseCoordinateString(coordStr);
|
||||
console.log('解析结果 - 多边形点:', points);
|
||||
setPoints(points);
|
||||
setShowCircles(true)
|
||||
}
|
||||
})
|
||||
return false;
|
||||
@@ -79,6 +80,16 @@ const Location = () => {
|
||||
setLatitude(data.latitude)
|
||||
setLongitude(data.longitude)
|
||||
setKeywords(data.code)
|
||||
if (data.fence) {
|
||||
// 方法2:使用实际的 fence 数据(如果是字符串格式)
|
||||
const coordStr = data.fence.points || '';
|
||||
|
||||
// 使用通用函数解析坐标字符串
|
||||
const { points } = parseCoordinateString(coordStr);
|
||||
console.log('解析结果 - 多边形点:', points);
|
||||
setPoints(points);
|
||||
setShowCircles(true)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -87,6 +98,9 @@ const Location = () => {
|
||||
reload()
|
||||
}, [])
|
||||
|
||||
// 监听圈圈数据变化
|
||||
useEffect(() => {
|
||||
}, [showCircles])
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -126,7 +140,7 @@ const Location = () => {
|
||||
<div className={'car-info-item-content'}>
|
||||
操作员:{item?.driver}
|
||||
</div>
|
||||
<div className={'car-info-item-content'}>
|
||||
<div className={'car-info-item-content pr-6'}>
|
||||
位置信息:{item?.address}
|
||||
</div>
|
||||
</div>
|
||||
@@ -145,11 +159,20 @@ const Location = () => {
|
||||
// @ts-ignore
|
||||
name: '位置'
|
||||
}]}
|
||||
polygons={points.length > 0 ? [
|
||||
{
|
||||
points: points,
|
||||
color: '#ff0000',
|
||||
fillColor: '#ffcccc',
|
||||
strokeWidth: 2
|
||||
}
|
||||
] : []}
|
||||
onTap={() => {
|
||||
console.log('map tap')
|
||||
}}
|
||||
style={{width: '100%', height: '100vh'}}
|
||||
/>
|
||||
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user