import React from 'react'; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, Image, TouchableOpacity, View, DeviceEventEmitter } from 'react-native'; import { adaptLocationPoint } from '../../utils'; import { BjTypeLocation } from './../../utils/types'; interface Props { onLocationResult?: (locationResult: LocationResult) => void showLocation?: boolean locationKeep?: boolean } interface State { currentLocation: BjTypeLocation listnerActive: boolean } interface LocationResult { } export default class KitRecordLocation extends React.Component { constructor(props: Props) { super(props); }; public readonly state: Readonly = { currentLocation: null, listnerActive: true, }; public onLocationListner = null; componentDidMount() { this.onLocationListner = DeviceEventEmitter.addListener('current-location-update', (a) => this.onLocation(a.location)) }; componentWillUnmount() { this.onLocationListner && this.onLocationListner.remove(); this.setState = (state, callback) => { return } } onLocation(location: BjTypeLocation) { if (location.latitude && location.longitude) { const { onLocationResult, locationKeep = false } = this.props; const { listnerActive } = this.state; location.longitude = Number(Number(location.longitude).toFixed(6)); location.latitude = Number(Number(location.latitude).toFixed(6)) if (!locationKeep) { this.setState({ listnerActive: false }) } listnerActive && onLocationResult && onLocationResult(location); } } render() { const { showLocation = false, locationKeep = false } = this.props; const { currentLocation } = this.state; return ( { showLocation && currentLocation ? {adaptLocationPoint(currentLocation.longitude, currentLocation.latitude)} {currentLocation.address} : null } ) } }