map.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import { View, StyleSheet, Linking } from "react-native";
  2. import { Image, Text } from "@tarojs/components";
  3. import './map.scss'
  4. import { useEffect, useRef, useState } from "react";
  5. import Taro from "@tarojs/taro";
  6. import { LeafletView } from 'react-native-leaflet-maps';
  7. import { useNavigation } from "@react-navigation/native";
  8. import { TimeFormatter } from "@/utils/time_format";
  9. import { systemLocation } from "@/services/common";
  10. import { useTranslation } from "react-i18next";
  11. import showAlert from "@/components/basic/Alert";
  12. import { kIsAndroid, kIsIOS } from "@/utils/tools";
  13. // let LeafletView = require('react-native-leaflet-maps').LeafletView
  14. let location: any = null
  15. export default function map() {
  16. // const [location, setLocation] = useState<any>(null)
  17. const navigation = useNavigation()
  18. const [current, setCurrent] = useState<any>(null)
  19. const { t } = useTranslation()
  20. const [show, setShow] = useState(false)
  21. const [count, setCount] = useState(0)
  22. useEffect(() => {
  23. navigation.setOptions({
  24. headerTitle: 'Choose Location',
  25. headerRight: () => {
  26. return <View>
  27. <Text onClick={confirmChoose} style={{ color: '#fff', marginRight: 15 }}>Done</Text>
  28. </View>
  29. }
  30. });
  31. getCurrent()
  32. }, [])
  33. function confirmChoose() {
  34. var today = new Date()
  35. var yesterday = new Date(today.getTime() - 24 * 3600 * 1000)
  36. var tomorrow = new Date(today.getTime() + 24 * 3600 * 1000)
  37. var strYesterday = `${yesterday.getFullYear()}-${TimeFormatter.padZero(yesterday.getMonth() + 1)}-${TimeFormatter.padZero(yesterday.getDate())}`
  38. var strTomorrow = `${tomorrow.getFullYear()}-${TimeFormatter.padZero(tomorrow.getMonth() + 1)}-${TimeFormatter.padZero(tomorrow.getDate())}`
  39. systemLocation({
  40. lat: (location as any).lat,
  41. lng: (location as any).lng,
  42. date_start: strYesterday,
  43. date_end: strTomorrow,
  44. coordinate_system_standard: process.env.TARO_ENV == 'weapp' ? 'GCJ-02' : 'WGS-84'
  45. }).then(data => {
  46. global.locationDetail = data
  47. Taro.setStorage({
  48. key: 'gps',
  49. data: JSON.stringify(data as any)
  50. })
  51. if (global.swiperDayNightRefresh) {
  52. global.swiperDayNightRefresh()
  53. }
  54. if (global.updateSwiper) {
  55. global.updateSwiper()
  56. }
  57. global.refreshNight()
  58. global.refreshDay()
  59. navigation.goBack()
  60. })
  61. }
  62. function getCurrent() {
  63. Taro.getLocation({
  64. success(res) {
  65. // showAlert({
  66. // title: 'location',
  67. // content: JSON.stringify(res)
  68. // })
  69. setShow(true)
  70. setCurrent(res)
  71. location = {
  72. lat: (res as any).latitude, lng: (res as any).longitude
  73. }
  74. },
  75. fail(res) {
  76. console.log('location update failed reason', res)
  77. if (res.errMsg == 'Permissions denied!') {
  78. showAlert({
  79. title: t('feature.auth_sys.location_title'),
  80. content: kIsIOS ? t('feature.auth_sys.location_desc') : t('feature.auth_sys.location_android_service_desc'),
  81. showCancel: true,
  82. cancelText: t('feature.auth_sys.location_cancel'),
  83. confirmText: kIsIOS ? t('feature.auth_sys.location_confirm') : t('feature.auth_sys.service_confirm'),
  84. confirm: () => {
  85. if (kIsIOS) {
  86. Linking.openURL('app-settings:')
  87. }
  88. else {
  89. // Linking.openSettings()
  90. var Jto = require('react-native').NativeModules.NativeBridge;
  91. Jto.openNotificationSettings()
  92. }
  93. }
  94. })
  95. }
  96. else {
  97. if (kIsAndroid) {
  98. var Jto = require('react-native').NativeModules.NativeBridge;
  99. Jto.getLocation().then(result => {
  100. var res = JSON.parse(result)
  101. setCurrent(res)
  102. location = {
  103. lat: (res as any).latitude, lng: (res as any).longitude
  104. }
  105. })
  106. }
  107. }
  108. }
  109. })
  110. }
  111. function showCurrent() {
  112. getCurrent()
  113. }
  114. function centerPosition() {
  115. if (current) {
  116. return { lat: current.latitude, lng: current.longitude }
  117. }
  118. return null
  119. }
  120. function mapMarkers() {
  121. const icon = '<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1721280834380" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8231" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M511.999097 511.999097m-486.524133 0a486.524133 486.524133 0 1 0 973.048266 0 486.524133 486.524133 0 1 0-973.048266 0Z" fill="#286FB7" opacity=".2" p-id="8232"></path><path d="M511.999097 511.999097m-307.200542 0a307.200542 307.200542 0 1 0 614.401084 0 307.200542 307.200542 0 1 0-614.401084 0Z" fill="#FFFFFF" p-id="8233"></path><path d="M511.999097 511.999097m-257.688367 0a257.688367 257.688367 0 1 0 515.376734 0 257.688367 257.688367 0 1 0-515.376734 0Z" fill="#4575F6" p-id="8234"></path></svg>'
  122. if (current) {
  123. return [{
  124. id: 9527,
  125. position: {
  126. lat: current.latitude,
  127. lng: current.longitude
  128. },
  129. icon: `<div style="width: 24px; height: 24px; background-color: #286FB752; border-radius: 50%;display:flex;align-items:center;justify-content:center;">
  130. <div style="width: 16px; height: 16px; background-color: #fff;; border-radius: 50%;display:flex;align-items:center;justify-content:center">
  131. <div style="width: 12px; height: 12px; background-color: #4575F6; border-radius: 50%;display:flex;align-items:center;justify-content:center">
  132. </div>
  133. </div>
  134. </div>`,
  135. // icon:
  136. // require('@/assets/images/current_location.png'),
  137. // icon: this.state.user_last_car,
  138. size: [32, 32],
  139. }]
  140. }
  141. return []
  142. }
  143. return <View style={{ flex: 1 }}>
  144. <LeafletView
  145. style={{ flex: 1 }}
  146. zoom={10}
  147. mapCenterPosition={centerPosition()}
  148. mapMarkers={mapMarkers()}
  149. onMessageReceived={e => {
  150. if (e.payload && e.payload.mapCenterPosition)
  151. console.log((e as any).payload.mapCenterPosition)
  152. // console.log(e)
  153. // setCount(count => count + 1)
  154. if (e.payload && e.payload.mapCenterPosition)
  155. location = e.payload.mapCenterPosition
  156. // if (e.event && e.event == 'onMoveEnd') {
  157. // if (e.payload && e.payload.mapCenterPosition)
  158. // setLocation(e.payload.mapCenterPosition)
  159. // }
  160. // setTimeout(() => {
  161. // if (e.payload && e.payload.mapCenterPosition)
  162. // setLocation(e.payload.mapCenterPosition)
  163. // }, 500)
  164. // if (e.payload && e.payload.mapCenterPosition)
  165. // setLocation(e.payload.mapCenterPosition)
  166. }}
  167. />
  168. <View style={styles.overlayView}
  169. pointerEvents="none"
  170. >
  171. <Image src={require('@/assets/images/center.png')} style={styles.center} />
  172. </View>
  173. <Image src={require('@/assets/images/current_location.png')} onClick={showCurrent} style={styles.location_btn} />
  174. {/* <View style={styles.footer}>
  175. {
  176. location && <Text style={{ fontSize: 14, color: '#000' }}>Center位置:lat {(location as any).lat.toFixed(4)} lng {(location as any).lng.toFixed(4)}</Text>
  177. }
  178. </View> */}
  179. </View>
  180. }
  181. const styles = StyleSheet.create({
  182. container: {
  183. flex: 1,
  184. },
  185. scrollContent: {
  186. // 您的滚动内容样式
  187. },
  188. overlayView: {
  189. // 覆盖视图样式
  190. position: 'absolute',
  191. top: 0,
  192. left: 0,
  193. right: 0,
  194. bottom: 0,
  195. alignItems: 'center',
  196. justifyContent: 'center'
  197. },
  198. center: {
  199. width: 40,
  200. height: 40,
  201. marginBottom: 25
  202. },
  203. location_btn: {
  204. position: 'absolute',
  205. left: 40,
  206. bottom: 80,
  207. width: 40,
  208. height: 40
  209. },
  210. footer: {
  211. position: 'absolute',
  212. left: 50,
  213. right: 50,
  214. bottom: 30,
  215. backgroundColor: '#fff',
  216. alignItems: 'center',
  217. justifyContent: 'center',
  218. height: 30
  219. }
  220. });