//@ts-nocheck import React from 'react'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, Image, TouchableOpacity, View, DeviceEventEmitter } from 'react-native'; import { navigate } from '../../NavigationService'; import { adaptLocationPoint, colors, getCurrentDateNs, getLocalTime, isEmpty, onePx, px2dp } from '../../utils'; import GPS from '../../utils/GPS'; import { Location, TypeBirdResearchLineV1 } from '../../utils/types'; interface Props { locationKeep?: boolean onLocationResult?: Function createDate?: any endDate?: any hideResearchLineDistance?: boolean, researchInstance: TypeBirdResearchLineV1 } interface State { currentLocation: Location listnerActive: boolean locationFailue: boolean hasCustomLocation: boolean handelLocation: boolean } interface LocationResult { } export default class KitResearchAreaLocation extends React.Component { constructor(props: Props) { super(props); }; public delayTimer = null; public onLocationListner = null; public onCustomLocationConfirmListener = null; public currentKey = getCurrentDateNs(); public geoPickerListener = 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 * 2); this.geoPickerListener = DeviceEventEmitter.addListener('on-geo-picker', (a) => { const { currentLocation, showLocationText, currentKey } = a; if (currentKey == this.currentKey) { this.setState({ currentLocation, showLocationText, hasCustomLocation: true }) } }) }; componentWillUnmount() { this.delayTimer && clearTimeout(this.delayTimer); this.onLocationListner && this.onLocationListner?.remove(); this.geoPickerListener && this.geoPickerListener?.remove(); this.onCustomLocationConfirmListener && this.onCustomLocationConfirmListener?.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: Location) { 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) { console.log("定位失败----->", e) const { currentLocation } = this.state; return currentLocation.poiName } } jumpToGeoPicker() { const { currentLocation } = this.state; navigate('GeoPicker', { currentKey: this.currentKey, initLocation: currentLocation }) } handleRefreshLocation() { this.setState({ currentLocation: null, showLocationText: "", hasCustomLocation: false, }); // toastShort("手动刷新坐标成功") }; handleInputLocation() { let { currentLocation } = this.state; DeviceEventEmitter.emit('show-custom-location-dialog', { currentLocation, currentKey: this.currentKey, }) }; render() { const { createDate, endDate, hideResearchLineDistance = false, researchInstance } = this.props; const { currentLocation, locationFailue, hasCustomLocation, showLocationText } = this.state; if (locationFailue && !hasCustomLocation) { return ( 定位失败,您可点击此处手动录入地理信息 ) } return ( // onPress={this.onPressLocationKit.bind(this)} {'起\n点'} { researchInstance && researchInstance.id ? 经纬度:  { researchInstance.latitudeStart ? adaptLocationPoint(researchInstance.longitudeStart, researchInstance.latitudeStart) : '无定位' } 海拔m:  {researchInstance && !isEmpty(researchInstance.altitudeStart) ? researchInstance.altitudeStart + 'm' : '定位中...'} 时间: {getLocalTime('YY:MM:DD HH:MM', researchInstance .createDate)} : 经纬度:  { showLocationText ? showLocationText : '定位中...' } 海拔m:  {currentLocation && !isEmpty(currentLocation.altitude) ? currentLocation.altitude + 'm' : '定位中...'} 时间: {getLocalTime('YY:MM:DD HH:MM', new Date().getTime())} } { researchInstance && researchInstance.id ? null : <> 刷新定位 手动输入 地图选点 } { researchInstance && researchInstance.finishDate ? {'终\n点'} 经纬度:  { researchInstance.longitudeEnd ? adaptLocationPoint(researchInstance.longitudeEnd, researchInstance.latitudeEnd) : '定位中...' } 海拔m:  {!isEmpty(researchInstance.altitudeEnd) ? researchInstance.altitudeEnd + 'm' : '定位中...'} 时间: {researchInstance.finishDate ? getLocalTime('YY:MM:DD HH:MM', researchInstance.finishDate) : '自动获取'} : {'终\n点'} 经纬度: 调查结束自动获取 海拔m: 自动获取 时间: {endDate ? getLocalTime('YY:MM:DD HH:MM', endDate) : '自动获取'} } { !hideResearchLineDistance ? <> { researchInstance && researchInstance.finishDate ? 样线长度: {researchInstance.researchLineDistance ? Number(researchInstance.researchLineDistance).toFixed(2) : '0'}米 : 样线长度: 调查结束自动结算 } : } { researchInstance && researchInstance.researchArea ? 调查区域:  {researchInstance.researchArea} : 调查区域:  {this.getResearchAddress()} } ) } } 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), }, top: { width: '100%', flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', }, locationText: { fontSize: 12, color: '#606366' }, blockLeft: { width: px2dp(18), backgroundColor: '#f2f2f2', justifyContent: 'center', alignItems: 'center', borderRadius: px2dp(3), paddingVertical: px2dp(2) }, blockText: { fontSize: 12, color: '#303133', textAlign: 'center' }, blockRight: { flex: 1, justifyContent: 'center', alignItems: 'flex-start', paddingLeft: px2dp(8), // backgroundColor: 'red' }, border: { width: px2dp(330), height: 1, borderBottomColor: colors.borderColor, borderBottomWidth: onePx, marginVertical: px2dp(6) }, timeText: { width: px2dp(132), textAlign: 'left' }, 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', } })