import { StyleSheet, Linking, StatusBar } from "react-native"; import { View,Image, Text } from "@tarojs/components"; import './map.scss' import { useEffect, useRef, useState } from "react"; import Taro from "@tarojs/taro"; import { LeafletView } from 'react-native-leaflet-maps'; import { useNavigation, useRoute } from "@react-navigation/native"; import { TimeFormatter } from "@/utils/time_format"; import { systemLocation } from "@/services/common"; import { useTranslation } from "react-i18next"; import showAlert from "@/components/basic/Alert"; import { kIsAndroid, kIsIOS } from "@/utils/tools"; import { AtActivityIndicator } from "taro-ui"; // let LeafletView = require('react-native-leaflet-maps').LeafletView let location: any = null export default function map() { // const [location, setLocation] = useState(null) const navigation = useNavigation() const [current, setCurrent] = useState(null) const [centerLocation, setCenterLocation] = useState(null) const { t } = useTranslation() const [show, setShow] = useState(false) const [isLoading, setIsLoading] = useState(false) const router = useRoute() const zoom = (router.params! as any).zoom || 12 useEffect(() => { if (router.params && (router.params! as any).lat) { setCenterLocation({ latitude: (router.params! as any).lat, longitude: (router.params! as any).lng }) } else { setCenterLocation({ latitude: 37.4221, longitude: -122.0853 }) } navigation.setOptions({ headerTitle: t('time_of_day.index.pick_location'), headerRight: () => { return {t('time_of_day.index.done')} // return // {t('time_of_day.index.done')} // } }); // getCurrent() }, []) function confirmChoose() { if ((router.params! as any).source && (router.params! as any).source == 'time_of_day') { if ((router.params! as any).chooseLocation) { (router.params! as any).chooseLocation(location, 'PICK') } navigation.goBack() return; } var today = new Date() var yesterday = new Date(today.getTime() - 24 * 3600 * 1000) var tomorrow = new Date(today.getTime() + 24 * 3600 * 1000) var strYesterday = `${yesterday.getFullYear()}-${TimeFormatter.padZero(yesterday.getMonth() + 1)}-${TimeFormatter.padZero(yesterday.getDate())}` var strTomorrow = `${tomorrow.getFullYear()}-${TimeFormatter.padZero(tomorrow.getMonth() + 1)}-${TimeFormatter.padZero(tomorrow.getDate())}` if (location.lng < -180) { location.lng += 360 } if (location.lng > 180) { location.lng -= 360 } if (location.lat > 90) { location.lat -= 90 } if (location.lat < -90) { location.lat += 90 } systemLocation({ lat: (location as any).lat, lng: (location as any).lng, date_start: strYesterday, date_end: strTomorrow, coordinate_system_standard: process.env.TARO_ENV == 'weapp' ? 'GCJ-02' : 'WGS-84' }).then(data => { global.locationDetail = data Taro.setStorage({ key: 'gps', data: JSON.stringify(data as any) }) if (global.swiperDayNightRefresh) { global.swiperDayNightRefresh() } if (global.updateSwiper) { global.updateSwiper() } global.refreshNight() global.refreshDay() navigation.goBack() }) } function getCurrent() { if (kIsIOS) { requestLocation() return; } var Jto = require('react-native').NativeModules.NativeBridge; Jto.checkSystemLocationService().then(res => { if (res) { requestLocation() } else { showAlert({ title: t('feature.auth_sys.location_title'), content: t('feature.auth_sys.device_location_desc'), showCancel: true, cancelText: t('feature.auth_sys.location_cancel'), confirmText: t('feature.auth_sys.device_confirm'), confirm: () => { Jto.openSystemLocationSettings() } }) } }) } function requestLocation() { setIsLoading(true) Taro.getLocation({ success(res) { // showAlert({ // title: 'location', // content: JSON.stringify(res) // }) setShow(true) setCurrent(res) setCenterLocation(res) location = { lat: (res as any).latitude, lng: (res as any).longitude } setIsLoading(false) }, fail(res) { console.log('location update failed reason', res) if (res.errMsg == 'Permissions denied!') { setIsLoading(false) showAlert({ title: t('feature.auth_sys.location_title'), content: kIsIOS ? t('feature.auth_sys.location_desc') : t('feature.auth_sys.location_android_service_desc'), showCancel: true, cancelText: t('feature.auth_sys.location_cancel'), confirmText: kIsIOS ? t('feature.auth_sys.location_confirm') : t('feature.auth_sys.service_confirm'), confirm: () => { if (kIsIOS) { Linking.openURL('app-settings:') } else { Linking.openSettings() // var Jto = require('react-native').NativeModules.NativeBridge; // Jto.openNotificationSettings() } } }) } else { if (kIsAndroid) { var Jto = require('react-native').NativeModules.NativeBridge; Jto.getLocation().then(result => { var res = JSON.parse(result) setCurrent(res) setCenterLocation(res) location = { lat: (res as any).latitude, lng: (res as any).longitude } setIsLoading(false) // showAlert({ // title: 'location2', // content: JSON.stringify(res) // }) }).catch(e => { setIsLoading(false) // showAlert({ // title: 'location failed', // content: JSON.stringify(e) // }) }) } else { setIsLoading(false) } } } }) } function showCurrent() { getCurrent() } function centerPosition() { if (centerLocation) { return { lat: centerLocation.latitude, lng: centerLocation.longitude } } return null } function mapMarkers() { const icon = '' if (current) { return [{ id: 9527, position: { lat: current.latitude, lng: current.longitude }, icon: `
`, // icon: // require('@/assets/images/current_location.png'), // icon: this.state.user_last_car, size: [32, 32], }] } return [] } return { if (e.event == 'onZoomEnd' && e.payload && e.payload.zoom) { Taro.setStorage({ key: 'zoom', data: (e as any).payload.zoom }) } if (e.payload && e.payload.mapCenterPosition) location = e.payload.mapCenterPosition // if (e.event && e.event == 'onMoveEnd') { // if (e.payload && e.payload.mapCenterPosition) // setLocation(e.payload.mapCenterPosition) // } // setTimeout(() => { // if (e.payload && e.payload.mapCenterPosition) // setLocation(e.payload.mapCenterPosition) // }, 500) // if (e.payload && e.payload.mapCenterPosition) // setLocation(e.payload.mapCenterPosition) }} /> { isLoading ? : } {/* { location && Center位置:lat {(location as any).lat.toFixed(4)} lng {(location as any).lng.toFixed(4)} } */} } const styles = StyleSheet.create({ container: { flex: 1, }, scrollContent: { // 您的滚动内容样式 }, overlayView: { // 覆盖视图样式 position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, alignItems: 'center', justifyContent: 'center' }, center: { width: 35, height: 35, marginBottom: 21 }, location_btn: { position: 'absolute', left: 40, bottom: 80, width: 50, height: 50, alignItems: 'center', justifyContent: 'center', backgroundColor: '#fff', borderRadius: 5 }, location_icon: { width: 30, height: 30 }, footer: { position: 'absolute', left: 50, right: 50, bottom: 30, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', height: 30 } });