//@ts-nocheck import React from 'react'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, Image, TouchableOpacity, View, DeviceEventEmitter } from 'react-native'; import { adaptLocationPoint, colors, getCurrentDateNs, getLocalTime, isEmpty, onePx, px2dp, toastShort } from './../../utils'; import GPS from '../../utils/GPS'; import { BjTypeLocation } from './../../utils/types'; import { navigate } from '../../NavigationService'; interface Props { locationKeep?: boolean onLocationResult?: Function size?: 'full' | 'mini' | 'headless' row?: any researchInstance?: any customKey?: string } interface State { currentLocation: BjTypeLocation listnerActive: boolean locationFailue: boolean hasCustomLocation: boolean handelLocation: boolean } interface LocationResult { } export default class KitAreaLocaionMsg extends React.Component { constructor(props: Props) { super(props); }; public delayTimer = null; public onLocationListner = null; public onCustomLocationConfirmListener = null; public currentKey = this.props.customKey ? this.props.customKey : getCurrentDateNs() + Math.random(); public geoPickerListener = null; public handleRefreshLocationListener = null; public readonly state: Readonly = { currentLocation: null, listnerActive: true, handelLocation: false, locationFailue: false, hasCustomLocation: false, customLocation: null, showLocationText: "定位中...", //定位的展示信息 }; componentDidMount() { this.onLocationListner = DeviceEventEmitter.addListener('current-location-update', (a) => { //未判定定位失败时,都可接收 if (!this.state.hasCustomLocation) { //进入后台定位时,若定位失败,则不忽略此次定时 if (a.location && a.location._longitude && a.location._latitude && a.location.longitude && a.location._latitude) { let showLocationText = adaptLocationPoint(a.location._longitude.toFixed(6), a.location._latitude.toFixed(6)); this.setState({ showLocationText }) this.onLocation(a.location) } } }); this.onCustomLocationConfirmListener = DeviceEventEmitter.addListener('custom-location-update', (a) => { if (a.currentKey = this.currentKey) { const { currentLocation } = this.state; let { location } = a; if(currentLocation && currentLocation.province){ if (currentLocation.province) { location.province = currentLocation.province; } if (currentLocation.city) { location.city = currentLocation.city; } if (currentLocation.district) { location.district = currentLocation.district; } if (currentLocation.poiName) { location.poiName = currentLocation.poiName; } } this.setState({ showLocationText: a.showLocationText, hasCustomLocation: true }) this.onLocation(location) } }) this.delayTimer = setTimeout(() => { //如果10s内未得到地理信息更新,判定此次定位失败 if (isEmpty(this.state.currentLocation)) { this.setState({ locationFailue: true }) } }, 1000 * 120); this.geoPickerListener = DeviceEventEmitter.addListener('on-geo-picker', (a) => { const { currentLocation, showLocationText, currentKey } = a; if (currentKey == this.currentKey) { console.log("on-geo-picker", currentLocation, showLocationText) this.setState({ currentLocation, showLocationText, hasCustomLocation: true }) } }) this.handleRefreshLocationListener = DeviceEventEmitter.addListener('on-refresh-location-status', (a) => { const { currentKey } = a; if (this.currentKey == currentKey) { this.setState({ currentLocation: null, showLocationText: "", hasCustomLocation: false, }); } }) }; componentWillUnmount() { this.delayTimer && clearTimeout(this.delayTimer); this.onLocationListner && this.onLocationListner?.remove(); this.geoPickerListener && this.geoPickerListener?.remove(); this.onCustomLocationConfirmListener && this.onCustomLocationConfirmListener?.remove(); this.handleRefreshLocationListener && this.handleRefreshLocationListener?.remove(); this.setState = (state, callback) => { return } } getLocation() { try { let { currentLocation } = this.state; currentLocation.altitude = Number(currentLocation.altitude) currentLocation.longitude = Number(currentLocation.longitude) currentLocation.latitude = Number(currentLocation.latitude) currentLocation.poiName = this.getResearchAddress() return currentLocation; } catch (e) { console.log("获取实时地理信息失败", e) let result = { altitude: "", longitude: "", latitude: "" } return result; } } onLocation(location: BjTypeLocation) { if (location.latitude && location.longitude) { const { onLocationResult, locationKeep = true } = this.props; const { listnerActive } = this.state; location.altitude = Number(location.altitude).toFixed(2); location.longitude = this.adaptLocationNumber(location.longitude); location.latitude = this.adaptLocationNumber(location.latitude); if (!locationKeep) { this.setState({ listnerActive: false }) } this.setState({ currentLocation: location }) listnerActive && onLocationResult && onLocationResult(location); } }; adaptLocationNumber(locationPoint: string) { locationPoint = locationPoint + ''; let firstDotIndex = locationPoint.indexOf('.'); let firstStr = locationPoint.substr(0, firstDotIndex); let lastStr = locationPoint.substr(firstDotIndex + 1, 6); return firstStr + '.' + lastStr; }; handleShowLocationToast() { DeviceEventEmitter.emit('show-custom-location-toast') }; onPressLocationKit() { const { hasCustomLocation, locationFailue } = this.state; if (locationFailue && hasCustomLocation) { this.handleShowLocationToast(); } }; getResearchAddress() { const { currentLocation } = this.state; if (!currentLocation || !currentLocation.latitude) { return "定位中..." } try { let resultAddress = ""; if (currentLocation.province) { resultAddress += currentLocation.province; } if (currentLocation.city) { resultAddress += currentLocation.city; } if (currentLocation.district) { resultAddress += currentLocation.district; } // if(currentLocation.street) { // resultAddress += currentLocation.street; // } return resultAddress; // { // currentLocation ? // currentLocation.district ? // currentLocation.district : // currentLocation.city ? currentLocation.city : '' // : '定位中...' // } } catch (e) { const { currentLocation } = this.state; return currentLocation.poiName } } adptDataToShow(): { showLocationText: string researchArea: string altitude: string createDate?: string } { try { let result = { showLocationText: "", researchArea: "", altitude: "", createDate: "", } const { row, researchInstance } = this.props; // console.log("位置KIT接收到的researchIntance", researchInstance) if (row && row.latitude && row.id) { const { latitude, longitude, altitude, createDate } = row; if (latitude && longitude) { const newLocation = GPS.gcj_decrypt_exact(Number(latitude), Number(longitude)) result.showLocationText = adaptLocationPoint(newLocation['lon'].toFixed(6), newLocation['lat'].toFixed(6)) } if (!isEmpty(altitude)) { result.altitude = altitude; } if (!isEmpty(createDate)) { result.createDate = getLocalTime('YY:MM:DD HH:MM', createDate); } } else if (researchInstance && researchInstance.id) { const { altitudeStart, longitudeStart, latitudeStart, poiAddress, latitude, longitude, altitude, createDate } = researchInstance; if (longitudeStart && latitudeStart) { const newLocation = GPS.gcj_decrypt_exact(Number(latitudeStart), Number(longitudeStart)) result.showLocationText = adaptLocationPoint(newLocation['lon'].toFixed(6), newLocation['lat'].toFixed(6)) } else if (latitude && longitude) { const newLocation = GPS.gcj_decrypt_exact(Number(latitude), Number(longitude)) result.showLocationText = adaptLocationPoint(newLocation['lon'].toFixed(6), newLocation['lat'].toFixed(6)) } if (!isEmpty(altitudeStart)) { result.altitude = altitudeStart; } if (!isEmpty(altitude)) { result.altitude = altitude; } if (!isEmpty(createDate)) { result.createDate = getLocalTime('YY:MM:DD HH:MM', createDate); } if (!isEmpty(poiAddress)) { result.researchArea = poiAddress; } } return result } catch (e) { console.log("adptDataToShow-err", e) return { showLocationText: "", } } }; jumpToGeoPicker() { const { currentLocation } = this.state; navigate('GeoPicker', { currentKey: this.currentKey, initLocation: currentLocation }) } handleRefreshLocation() { DeviceEventEmitter.emit("on-refresh-location-status", { currentKey: this.currentKey }) // toastShort("手动刷新坐标成功") }; handleInputLocation() { let { currentLocation } = this.state; DeviceEventEmitter.emit('show-custom-location-dialog', { currentLocation, currentKey: this.currentKey, }) }; render() { const { size = 'full' } = this.props; const { currentLocation, locationFailue, hasCustomLocation, showLocationText, } = this.state; if (size == 'headless') { return } // if (locationFailue && !hasCustomLocation) { // return ( // // 定位失败,您可点击此处手动录入地理信息 // {/* */} // // ) // } const data = this.adptDataToShow(); return ( <> { data && data.showLocationText ? 初始坐标: { data.showLocationText ? data.showLocationText : '无坐标信息' } 海拔: {data && !isEmpty(data.altitude) ? data.altitude + 'm' : '0m'} { size == 'full' ? 初始地域: {data.researchArea ? data.researchArea : '无'} : null } { data.createDate ? 创建时间:{data.createDate} : null } : { hasCustomLocation ? 自定坐标: : 当前坐标: } { showLocationText ? showLocationText : '定位中...' } 海拔: {currentLocation && !isEmpty(currentLocation.altitude) ? currentLocation.altitude + 'm' : '定位中...'} { size == 'full' ? 所属地域: {this.getResearchAddress()} : null } 刷新定位 手动输入 地图选点 } ) } } const styles = StyleSheet.create({ container: { width: px2dp(350), backgroundColor: '#fff', borderWidth: onePx, borderColor: '#e9e9e9', borderBottomLeftRadius: px2dp(6), borderBottomRightRadius: px2dp(6), justifyContent: 'center', alignItems: 'flex-start', paddingHorizontal: px2dp(10), paddingVertical: px2dp(10), // elevation: 2 }, top: { width: '100%', flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }, locationText: { fontSize: 12, color: colors.blackColor }, toolLine: { width: '100%', flexDirection: 'row', justifyContent: 'flex-end', alignItems: 'center', paddingTop: px2dp(8) }, toolBtn: { height: px2dp(24), flexDirection: 'row', justifyContent: 'center', alignItems: 'center', backgroundColor: colors.themeColor, paddingHorizontal: px2dp(5), marginLeft: px2dp(8), borderRadius: px2dp(2), paddingBottom: px2dp(1) }, iconContainer: { width: px2dp(14), height: px2dp(14), marginRight: px2dp(2), justifyContent: 'center', alignItems: 'center' }, toolBtnIcon: { width: px2dp(14), height: px2dp(14), tintColor: '#fff', }, toolBtnText: { fontSize: 12, color: '#fff', } })