import { useState, useEffect, useRef } from 'react' import Taro from '@tarojs/taro' import withLayout from '@/layout' import { ScrollView, Map } from '@tarojs/components' import { fetch } from '@/utils/request' import { API_ITEMS_DETAIL } from '@/constants/api' import '@/assets/css/iconfont.css' import './index.scss' export default withLayout((props) => { const { router, city } = props const { id } = router.params const [DetailInfo, setDetailInfo] = useState({}) const [loc, setLoc] = useState([]) const [NavList, setNavList] = useState([]) const [CurrentNavId, setCurrentNavId] = useState(0) const [List, setList] = useState([]) const [OtherList, setOtherList] = useState([]) const [markers, setMarkers] = useState([]) const mapCtx = useRef() const CutNav = (inx) => { return () => { setCurrentNavId(inx) } } const locationTo = () => { if (loc.length) { Taro.openLocation({ longitude: loc[0], latitude: loc[1], name: DetailInfo.buildingName, address: DetailInfo.address, }) } } const handlePoi = (poi) => { const [longitude, latitude] = poi.location.split(',').map(x => x ? x - 0 : undefined) const marker = { id: 1, longitude, latitude, // iconPath: '', width: 18, height: 27, callout: { content: poi.name, color: '#333333', padding: 6, display: 'ALWAYS', } } const center = markers[0] setMarkers([center, marker]) if (mapCtx.current) { // 缩放地图,显示所有 marker const points = [ { longitude: center.longitude, latitude: center.latitude }, { longitude: marker.longitude, latitude: marker.latitude } ] mapCtx.current.includePoints({ points, padding: [64] }) } } // 获取地图 Context useEffect(() => { Taro.nextTick(() => { mapCtx.current = Taro.createMapContext('around-map') }) }, []) useEffect(() => { // 获取楼盘信息 fetch({ url: `${API_ITEMS_DETAIL}/${id}`, spin: true }).then((res) => { if (res?.coordinate) { // 地图中心 const [longitude, latitude] = res.coordinate.split(',') setLoc([longitude - 0, latitude - 0]) // 中心点标记 setMarkers([{ id: -1, longitude, latitude, width: 18, height: 27, callout: { content: res.buildingName, color: '#333333', padding: 6, display: 'ALWAYS', } }]) setDetailInfo(res || {}) if (res.mapJson) { const pois = JSON.parse(res.mapJson).map(poi => ({ ...poi, data: JSON.parse(poi.data) })) pois.map((item) => { let target = [] if(item.key === 'Transport') { target = (res.buildingTransport || '').split(',').map((subItem) => { return { name: subItem } }) } else if(item.key === 'Mall') { target = (res.buildingMall || '').split(',').map((subItem) => { return { name: subItem } }) } else if(item.key === 'Edu') { target = (res.buildingEdu || '').split(',').map((subItem) => { return { name: subItem } }) } else if(item.key === 'Hospital') { target = (res.buildingHospital || '').split(',').map((subItem) => { return { name: subItem } }) } else if(item.key === 'Bank') { target = (res.buildingBank || '').split(',').map((subItem) => { return { name: subItem } }) } else if(item.key === 'Restaurant') { target = (res.buildingRestaurant || '').split(',').map((subItem) => { return { name: subItem } }) } item.data = item.data.concat(target) }) console.log(`pois is`, pois) console.log(res) setNavList(pois) } } else { Taro.showToast({ title: '当前楼盘未设置位置信息', icon: 'none', }) } }).catch((err) => { console.error(err) }) }, [id]) useEffect(() => { if (NavList && NavList.length) { const { key, data } = NavList[CurrentNavId] setList(data) const tags = DetailInfo[key] || '' setOtherList(tags.split(',').filter(Boolean)) } }, [NavList, CurrentNavId, DetailInfo]) const TypeCalc = (key) => { switch (key) { case 'Transport': return '交通' case 'Mall': return '商业' case 'Edu': return '学校' case 'Hospital': return '医院' case 'Bank': return '银行' case 'Restaurant': return '餐饮' default: return '' } } return ( {DetailInfo.buildingName} 前往 {DetailInfo.address} { NavList.map((item, inx) => ( {`${TypeCalc(item.key)}`} )) } { List.map((item, index) => ( handlePoi(item)}> {index + 1}、 {item.name} {`${item.distance || '-'}m`} )) } { OtherList.length > 0 && 其他 } { OtherList.map((item, index) => ( {index + 1}、南京地铁1号线 )) } ) })