import React, { Component } from 'react' import { View, Text, StyleSheet, TouchableHighlight, TextInput, DeviceEventEmitter, Animated, Easing, KeyboardAvoidingView } from 'react-native' import { px2dp, onePx, colors, toastShort, isEmpty, filterSqlRows, handleDismissKeyboard } from '../../utils'; import GPS from '../../utils/GPS'; import { BjTypeLocation } from '../../utils/types'; interface Props { }; interface State { opacity: any latDegree: string latMin: string latSec: string lngDegree: string lngMin: string lngSec: string altitude: string } export default class CustomLocationDialog extends Component { constructor(props) { super(props); this.show = this.show.bind(this) } public active = false; public delayTimer = null; public delayTimer1 = null; public delayHideTimer = null; public inputRef = null; public toast = null; public tempKey = null; public showDialogListener = null; componentDidMount() { this.showDialogListener = DeviceEventEmitter.addListener('show-custom-location-dialog', (a) => { console.log("接收到=======", a) const { currentKey, currentLocation } = a; this.tempKey = currentKey; this.show(); }) } public readonly state: Readonly = { opacity: new Animated.Value(0), latDegree: "", latMin: "", latSec: "", lngDegree: "", lngMin: "", lngSec: "", altitude: "", }; componentWillUnmount() { this.delayHideTimer && clearTimeout(this.delayHideTimer); this.showDialogListener && this.showDialogListener?.remove(); this.delayTimer && clearTimeout(this.delayTimer); this.setState = (state, callback) => { return; } } show = () => { this.toast.setNativeProps({ style: { width: '100%', height: '100%', zIndex: 999 } }) Animated.timing( this.state.opacity, { toValue: 0.32, duration: 250, //@ts-ignore easing: Easing.in(), useNativeDriver: true } ).start(); this.delayTimer = setTimeout(() => { // this.inputRef && this.inputRef.focus(); }, 200); } hide = () => { this.active = false; handleDismissKeyboard(); Animated.timing( this.state.opacity, { toValue: 0, duration: 100, //@ts-ignore easing: Easing.in(), useNativeDriver: true } ).start(); this.delayHideTimer = setTimeout(() => { this.toast.setNativeProps({ style: { width: 0, height: 0, zIndex: -1 } }) }, 100); }; handleClose() { const { latDegree, latMin, latSec, lngDegree, lngMin, lngSec, altitude } = this.state; if (isEmpty(latDegree)) { return toastShort("请输入经度:度") } if (isEmpty(latDegree)) { return toastShort("请输入经度:分") } if (isEmpty(latDegree)) { return toastShort("请输入经度:秒") } if (isEmpty(latDegree)) { return toastShort("请输入纬度:度") } if (isEmpty(latDegree)) { return toastShort("请输入纬度:分") } if (isEmpty(latDegree)) { return toastShort("请输入纬度:秒") } if (isEmpty(latDegree)) { return toastShort("请输入海拔信息") } let _latitude = GPS.ToDigital(latDegree, latMin, latSec, 6); let _longitude = GPS.ToDigital(lngDegree, lngMin, lngSec, 6); //以上是w84 //w84 to gc_j02 const newLocation = GPS.gcj_encrypt(_latitude, _longitude); let location: BjTypeLocation = { _latitude, _longitude, latitude: newLocation['lat'], longitude: newLocation['lon'], //@ts-ignore altitude, poiName: "", address: "", } let showLocationText = ""; showLocationText = `${lngDegree}°${lngMin}′${lngSec}″E ${latDegree}°${latMin}′${latSec}″N` DeviceEventEmitter.emit("custom-location-update", { location, showLocationText, currentKet: this.tempKey }) this.hide(); } onLatDegreeValueChange(text) { const { latDegree, latMin, latSec, lngDegree, lngMin, lngSec, altitude } = this.state; if (isEmpty(text) || text == 0) { return this.setState({ latDegree: "" }) } text = Number(text); var patrn = /^[0-9]*$/; if (!patrn.test(text)) { toastShort("请输入纯数字") this.setState({ latDegree }) } else { if (text > 180) { toastShort("最大可输入180") return this.setState({ latDegree: '180' }) } this.setState({ latDegree: text + "" }) } }; onLatMinValueChange(text) { const { latDegree, latMin, latSec, lngDegree, lngMin, lngSec, altitude } = this.state; if (isEmpty(text) || text == 0) { return this.setState({ latMin: "" }) } text = Number(text); var patrn = /^[0-9]*$/; if (!patrn.test(text)) { toastShort("请输入纯数字") this.setState({ latMin }) } else { if (text > 60) { toastShort("最大不超过60") return this.setState({ latMin: '59' }) } this.setState({ latMin: text + "" }) } }; onLatSecValueChange(text) { const { latDegree, latMin, latSec, lngDegree, lngMin, lngSec, altitude } = this.state; if (isEmpty(text) || text == '0') { return this.setState({ latSec: "" }) } text = text.replace(/[^\d^\.]+/g, ''); let arr = text.split('.'); if (text[0] && text[0] == '.') { return this.setState({ latSec }) } if (arr.length > 2) { toastShort("只能输入一个小数点"); } else { if (Number(text) > 60) { toastShort("最大不超过60"); return this.setState({ latSec: '59' }) } if (arr[1] && arr[1].length > 5) { this.setState({ latSec }) return toastShort("最多可保留小数点后5位数"); } else { this.setState({ latSec: text }) } }; }; onLngDegreeValueChange(text) { const { latDegree, latMin, latSec, lngDegree, lngMin, lngSec, altitude } = this.state; if (isEmpty(text) || text == 0) { return this.setState({ lngDegree: "" }) } text = Number(text); var patrn = /^[0-9]*$/; if (!patrn.test(text)) { toastShort("请输入纯数字") this.setState({ lngDegree }) } else { if (text > 180) { toastShort("最大可输入90") return this.setState({ lngDegree: '90' }) } this.setState({ lngDegree: text + "" }) } }; onLngMinValueChange(text) { const { latDegree, latMin, latSec, lngDegree, lngMin, lngSec, altitude } = this.state; if (isEmpty(text) || text == 0) { return this.setState({ lngMin: "" }) } text = Number(text); var patrn = /^[0-9]*$/; if (!patrn.test(text)) { toastShort("请输入纯数字") this.setState({ lngMin }) } else { if (text > 60) { toastShort("最大不超过60") return this.setState({ lngMin: '59' }) } this.setState({ lngMin: text + "" }) } }; onLngSecValueChange(text) { const { latDegree, latMin, latSec, lngDegree, lngMin, lngSec, altitude } = this.state; if (isEmpty(text) || text == '0') { return this.setState({ lngSec: "" }) } text = text.replace(/[^\d^\.]+/g, ''); let arr = text.split('.'); if (text[0] && text[0] == '.') { return this.setState({ lngSec }) } if (arr.length > 2) { toastShort("只能输入一个小数点"); } else { if (Number(text) > 60) { toastShort("最大不超过60"); return this.setState({ lngSec: '59' }) } if (arr[1] && arr[1].length > 5) { this.setState({ lngSec }) return toastShort("最多可保留小数点后5位数"); } else { this.setState({ lngSec: text }) } }; }; onAltitudeChange(text) { const { latDegree, latMin, latSec, lngDegree, lngMin, lngSec, altitude } = this.state; if (isEmpty(text) || text == '0') { return this.setState({ altitude: "" }) } text = text.replace(/[^\d^\.]+/g, ''); let arr = text.split('.'); if (text[0] && text[0] == '.') { return this.setState({ altitude }) } if (arr.length > 2) { toastShort("只能输入一个小数点"); } else { if (arr[1] && arr[1].length > 5) { this.setState({ altitude }) return toastShort("只能输入一个小数点后5位数"); } else { this.setState({ altitude: text }) } }; }; render() { const { latDegree, latMin, latSec, lngDegree, lngMin, lngSec, altitude } = this.state; return ( this.toast = c} style={styles.alertContainer}> GPS 请确保录入的经纬度严格遵循度/分/秒格式 经度: this.inputRef = c} style={styles.alertBodyInput} placeholder={""} keyboardType={'numeric'} maxLength={3} value={lngDegree} onChangeText={this.onLngDegreeValueChange.bind(this)} /> °   ′ ″E 纬度: °   ′ ″N 海拔:  m this.hide()} > 取消 this.handleClose()} > 确定 ) } } const styles = StyleSheet.create({ alertContainer: { position: 'absolute', width: '0%', height: '0%', justifyContent: 'center', alignItems: 'center', overflow: 'hidden', zIndex: -1, }, alertOverlay: { position: 'absolute', top: 0, left: 0, right: 0, bottom: 0, width: '100%', height: '100%', backgroundColor: '#000' }, alertContent: { width: px2dp(320), backgroundColor: '#fff', borderRadius: px2dp(8), justifyContent: 'space-between', alignItems: 'center', overflow: "hidden" }, alertHeader: { width: '100%', justifyContent: 'center', alignItems: 'center', // backgroundColor: '#999', paddingTop: px2dp(18) }, alertBody: { width: '100%', justifyContent: 'flex-start', alignItems: 'center', paddingHorizontal: px2dp(18), paddingTop: px2dp(16), }, alertBodyTop: { width: '100%', flexDirection: 'column', justifyContent: 'flex-start', alignItems: 'flex-start' }, alertBodyTopRight: { flex: 1, height: px2dp(24), flexDirection: 'row', justifyContent: 'flex-end', alignItems: 'center' }, alertBodyCenter: { flexDirection: 'column', justifyContent: 'center', alignItems: 'center', paddingVertical: px2dp(10) }, alertBodyCenterRow: { width: px2dp(290), flexDirection: 'row', justifyContent: 'flex-start', alignItems: 'center', marginVertical: px2dp(6), // backgroundColor: 'red' }, alertBodyInput: { flex: 1, height: px2dp(30), borderWidth: 1, borderColor: '#e9e9e9', borderRadius: px2dp(4), paddingHorizontal: px2dp(6), paddingVertical: px2dp(0), fontSize: 14, color: colors.blackColor }, alertBodyInput1: { width: px2dp(120), height: px2dp(30), borderWidth: 1, borderColor: '#e9e9e9', borderRadius: px2dp(4), paddingHorizontal: px2dp(6), paddingVertical: px2dp(0), fontSize: 14, color: colors.blackColor }, title: { fontSize: 16, color: '#000', fontWeight: '600', lineHeight: 25, textAlign: 'center', letterSpacing: 0.8 }, alertFooter: { width: px2dp(320), height: px2dp(52), justifyContent: 'center', alignItems: 'center', flexDirection: 'row', }, boderTop: { width: px2dp(280), height: 1, borderTopColor: '#d2d2d2', borderTopWidth: onePx, }, btn: { flex: 1, height: '100%', justifyContent: 'center', alignItems: 'center' }, border: { width: 1, height: px2dp(32), borderRightColor: '#d2d2d2', borderRightWidth: onePx }, decText: { width: '100%', fontSize: 12, color: '#aaa', marginTop: px2dp(-8), marginBottom: px2dp(18) }, })