import { useEffect, useRef, useState } from 'react' import Taro from '@tarojs/taro' import withLayout from '@/layout' import { Image, Map } from '@tarojs/components' import { getItemList } from '@/services/item' import { getImgURL } from '@/utils/image' import '@/assets/css/iconfont.css' import './index.scss' export default withLayout((props) => { const { city } = props const [ShowInfo, setShowInfo] = useState(false) const [list, setList] = useState([]) const [markers, setMarkers] = useState([]) const mapCtx = useRef() const [current, setCurrent] = useState({}) const changeCity = () => { Taro.navigateTo({ url: '/pages/index/location/index' }) } const handleMarker = (e) => { const { markerId } = e.detail const building = list[markerId - 1]; setCurrent(building || {}); setShowInfo(true) } const gotoDetail = () => { if (current.buildingId) { Taro.navigateTo({ url: `/pages/index/buildingDetail/index?id=${current.buildingId}` }) } } const locationTo = () => { if (current.buildingId) { const loc = current.coordinate.split(',') Taro.openLocation({ longitude: loc[0] - 0, latitude: loc[1] - 0, name: current.buildingName, address: current.address, scale: 12, }) } } useEffect(() => { Taro.nextTick(() => { mapCtx.current = Taro.createMapContext('map-buildings') }) }, []) useEffect(() => { if (city?.id) { // 先移除页面的 markers if (mapCtx.current && markers.length) { mapCtx.current.removeMarkers({ markerIds: markers.map(x => x.id) }) setMarkers([]) } getItemList({ cityId: city.id, pageSize: 100, }).then((res) => { const { records } = res setList((records || []).filter(x => x.coordinate)) }) } }, [city?.id]) useEffect(() => { if (!list.length) return; const mks = list.map((item, index) => { const loc = item.coordinate.split(',') return { id: index + 1, longitude: loc[0] - 0, latitude: loc[1] - 0, iconPath: '', width: 18, height: 27, callout: { content: item.buildingName, color: '#333333', fontSize: 14, display: 'ALWAYS', padding: 6, borderRadius: 2, borderColor: 'rgba(0,0,0, .1)', } } }) const points = mks.map(it => ({ longitude: it.longitude, latitude: it.latitude })) setMarkers(mks) const t = setInterval(() => { if (mapCtx.current) { mapCtx.current.includePoints({ points, padding: [32] }) clearInterval(t) } }, 300) return () => clearInterval(t) }, [list]) return ( {/* 地图 */} setShowInfo(false)} /> {/* 定位 */} {city.name} {/* 分享 */} {/* 附近楼盘 */} 立即 前往 {/* 附近楼盘 */} { !!current.buildingName && { current?.panoramaList.length > 0 && } { current?.videoUrl !== null && } } {current.buildingName} {current.price} {current.address} { (current.buildingTag || []).map((item, index) => ( {item.tagName} )) } {`${199 + (current.shareNum || 0)}次分享`} { (current.uvList?.records || []).slice(0, 3).map((item, index) => ( )) } {(current.uvList?.records || []).length > 0 ? '...' : ''}{`${199 + (current.pvNum || 0)}人围观`} ) })