index.jsx 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import { useState, useEffect, useRef } from 'react'
  2. import Taro from '@tarojs/taro'
  3. import withLayout from '@/layout'
  4. import { ScrollView, Map } from '@tarojs/components'
  5. import { fetch } from '@/utils/request'
  6. import { API_ITEMS_DETAIL } from '@/constants/api'
  7. import '@/assets/css/iconfont.css'
  8. import './index.scss'
  9. export default withLayout((props) => {
  10. const { router, city } = props
  11. const { id } = router.params
  12. const [DetailInfo, setDetailInfo] = useState({})
  13. const [loc, setLoc] = useState([])
  14. const [NavList, setNavList] = useState([])
  15. const [CurrentNavId, setCurrentNavId] = useState(0)
  16. const [List, setList] = useState([])
  17. const [OtherList, setOtherList] = useState([])
  18. const [markers, setMarkers] = useState([])
  19. const mapCtx = useRef()
  20. const CutNav = (inx) => {
  21. return () => {
  22. setCurrentNavId(inx)
  23. }
  24. }
  25. const locationTo = () => {
  26. if (loc.length) {
  27. Taro.openLocation({
  28. longitude: loc[0],
  29. latitude: loc[1],
  30. name: DetailInfo.buildingName,
  31. address: DetailInfo.address,
  32. })
  33. }
  34. }
  35. const handlePoi = (poi) => {
  36. const [longitude, latitude] = poi.location.split(',').map(x => x ? x - 0 : undefined)
  37. const marker = {
  38. id: 1,
  39. longitude,
  40. latitude,
  41. // iconPath: '',
  42. width: 18,
  43. height: 27,
  44. callout: {
  45. content: poi.name,
  46. color: '#333333',
  47. padding: 6,
  48. display: 'ALWAYS',
  49. }
  50. }
  51. const center = markers[0]
  52. setMarkers([center, marker])
  53. if (mapCtx.current) {
  54. // 缩放地图,显示所有 marker
  55. const points = [
  56. { longitude: center.longitude, latitude: center.latitude },
  57. { longitude: marker.longitude, latitude: marker.latitude }
  58. ]
  59. mapCtx.current.includePoints({ points, padding: [64] })
  60. }
  61. }
  62. // 获取地图 Context
  63. useEffect(() => {
  64. Taro.nextTick(() => {
  65. mapCtx.current = Taro.createMapContext('around-map')
  66. })
  67. }, [])
  68. useEffect(() => {
  69. // 获取楼盘信息
  70. fetch({ url: `${API_ITEMS_DETAIL}/${id}`, spin: true }).then((res) => {
  71. if (res?.coordinate) {
  72. // 地图中心
  73. const [longitude, latitude] = res.coordinate.split(',')
  74. setLoc([longitude - 0, latitude - 0])
  75. // 中心点标记
  76. setMarkers([{
  77. id: -1,
  78. longitude,
  79. latitude,
  80. width: 18,
  81. height: 27,
  82. callout: {
  83. content: res.buildingName,
  84. color: '#333333',
  85. padding: 6,
  86. display: 'ALWAYS',
  87. }
  88. }])
  89. setDetailInfo(res || {})
  90. if (res.mapJson) {
  91. const pois = JSON.parse(res.mapJson).map(poi => ({ ...poi, data: JSON.parse(poi.data) }))
  92. pois.map((item) => {
  93. let target = []
  94. if(item.key === 'Transport') {
  95. target = (res.buildingTransport || '').split(',').map((subItem) => {
  96. return { name: subItem }
  97. })
  98. } else if(item.key === 'Mall') {
  99. target = (res.buildingMall || '').split(',').map((subItem) => {
  100. return { name: subItem }
  101. })
  102. } else if(item.key === 'Edu') {
  103. target = (res.buildingEdu || '').split(',').map((subItem) => {
  104. return { name: subItem }
  105. })
  106. } else if(item.key === 'Hospital') {
  107. target = (res.buildingHospital || '').split(',').map((subItem) => {
  108. return { name: subItem }
  109. })
  110. } else if(item.key === 'Bank') {
  111. target = (res.buildingBank || '').split(',').map((subItem) => {
  112. return { name: subItem }
  113. })
  114. } else if(item.key === 'Restaurant') {
  115. target = (res.buildingRestaurant || '').split(',').map((subItem) => {
  116. return { name: subItem }
  117. })
  118. }
  119. item.data = item.data.concat(target)
  120. })
  121. console.log(`pois is`, pois)
  122. console.log(res)
  123. setNavList(pois)
  124. }
  125. } else {
  126. Taro.showToast({
  127. title: '当前楼盘未设置位置信息',
  128. icon: 'none',
  129. })
  130. }
  131. }).catch((err) => {
  132. console.error(err)
  133. })
  134. }, [id])
  135. useEffect(() => {
  136. if (NavList && NavList.length) {
  137. const { key, data } = NavList[CurrentNavId]
  138. setList(data)
  139. const tags = DetailInfo[key] || ''
  140. setOtherList(tags.split(',').filter(Boolean))
  141. }
  142. }, [NavList, CurrentNavId, DetailInfo])
  143. const TypeCalc = (key) => {
  144. switch (key) {
  145. case 'Transport':
  146. return '交通'
  147. case 'Mall':
  148. return '商业'
  149. case 'Edu':
  150. return '学校'
  151. case 'Hospital':
  152. return '医院'
  153. case 'Bank':
  154. return '银行'
  155. case 'Restaurant':
  156. return '餐饮'
  157. default:
  158. return ''
  159. }
  160. }
  161. return (
  162. <view className='Page buildingAround'>
  163. <view className='MapContainer'>
  164. <view>
  165. <Map
  166. id='around-map'
  167. showLocation
  168. longitude={loc[0]}
  169. latitude={loc[1]}
  170. markers={markers}
  171. />
  172. </view>
  173. </view>
  174. <view className='TabContainer'>
  175. <view>
  176. <view className='Title flex-h'>
  177. <view className='flex-item'>
  178. <text>{DetailInfo.buildingName}</text>
  179. </view>
  180. <view className='Go' onClick={locationTo}>
  181. <text>前往</text>
  182. <text className='iconfont icon-qianwang'></text>
  183. </view>
  184. </view>
  185. <view className='Address flex-h'>
  186. <text className='iconfont icon-dingwei'></text>
  187. <view className='flex-item'>
  188. <text>{DetailInfo.address}</text>
  189. </view>
  190. </view>
  191. <view className='Nav flex-h'>
  192. {
  193. NavList.map((item, inx) => (
  194. <view className={inx === CurrentNavId ? 'flex-item active' : 'flex-item'} key={item.key}>
  195. <text onClick={CutNav(inx)}>{`${TypeCalc(item.key)}`}</text>
  196. </view>
  197. ))
  198. }
  199. </view>
  200. <view className='CutLine'></view>
  201. <view className='ListContainer'>
  202. <ScrollView scroll-y>
  203. <view className='ScrollContent'>
  204. <view className='List'>
  205. {
  206. List.map((item, index) => (
  207. <view className='flex-h' key={`ListItem-${index}`} onClick={() => handlePoi(item)}>
  208. <text>{index + 1}、</text>
  209. <view className='flex-item'>
  210. <text>{item.name}</text>
  211. </view>
  212. <text className='iconfont icon-dingwei'></text>
  213. <text className='distance'>{`${item.distance || '-'}m`}</text>
  214. </view>
  215. ))
  216. }
  217. </view>
  218. {
  219. OtherList.length > 0 &&
  220. <text>其他</text>
  221. }
  222. <view className='List'>
  223. {
  224. OtherList.map((item, index) => (
  225. <view className='flex-h' key={`ListItem-${index}`}>
  226. <view className='flex-item'>
  227. <text>{index + 1}、南京地铁1号线</text>
  228. </view>
  229. </view>
  230. ))
  231. }
  232. </view>
  233. </view>
  234. </ScrollView>
  235. </view>
  236. </view>
  237. </view>
  238. </view>
  239. )
  240. })