import React from 'react'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, Image, TouchableOpacity, View, DeviceEventEmitter } from 'react-native'; import { adaptLocationPoint, colors, isEmpty, px2dp, statusBarHeight } from './../../utils'; import { BjTypeLocation } from './../../utils/types'; interface Props { } interface State { currentLocation: BjTypeLocation locationFailue: boolean, showLocationText: string } export default class GpsLocationShowKit extends React.Component { constructor(props: Props) { super(props); }; public delayTimer = null; public onLocationListner = null; public onCustomLocationConfirmListener = null; public readonly state: Readonly = { currentLocation: null, locationFailue: false, showLocationText: "定位中...", }; componentDidMount() { this.onLocationListner = DeviceEventEmitter.addListener('current-location-update', (a) => { try { //未判定定位失败时,都可接收 //进入后台定位时,若定位失败,则不忽略此次定时 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)); if (a.location && a.location.accuracy) { if (a.location.accuracy > 80) { a.location.accuracy = a.location.accuracy / 2; } if (a.location.accuracy < 20) { a.location.accuracy = a.location.accuracy + 10; } a.location.accuracy = Number((a.location.accuracy - (Math.random() * 10))).toFixed(1) } this.setState({ showLocationText }) this.onLocation(a.location) } } catch (e) { } }); this.delayTimer = setTimeout(() => { //如果10s内未得到地理信息更新,判定此次定位失败 if (isEmpty(this.state.currentLocation)) { this.setState({ locationFailue: true }) } }, 1000 * 2); }; componentWillUnmount() { this.delayTimer && clearTimeout(this.delayTimer); this.onLocationListner && this.onLocationListner?.remove(); this.onCustomLocationConfirmListener && this.onCustomLocationConfirmListener?.remove(); this.setState = (state, callback) => { return } } onLocation(location: BjTypeLocation) { if (location.latitude && location.longitude) { //@ts-ignore location.altitude = Number(location.altitude).toFixed(2); //@ts-ignore location.longitude = this.adaptLocationNumber(location.longitude); //@ts-ignore location.latitude = this.adaptLocationNumber(location.latitude); this.setState({ currentLocation: 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; }; render() { const { showLocationText, currentLocation } = this.state; // console.log("currentLocation---->", currentLocation) return ( WGS-84 {showLocationText} 当前海拔:  {currentLocation && !isEmpty(currentLocation.altitude) ? currentLocation.altitude + 'm' : 'N/A'} ) } } const styles = StyleSheet.create({ container: { position: 'absolute', top: 0, left: 0, zIndex: 999, width: '100%', height: statusBarHeight + px2dp(24), backgroundColor: colors.themeColor, paddingTop: statusBarHeight, flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', paddingHorizontal: px2dp(13), elevation: 12 }, locationType: { height: px2dp(16), // width: px2dp(32), // backgroundColor: '#7f7f7f', backgroundColor: colors.yellowColor, justifyContent: 'center', alignItems: 'center', paddingHorizontal: px2dp(4), borderRadius: px2dp(2), marginRight: px2dp(4), marginTop: px2dp(-1) } })