LocationTextView.js 2.43 KB
import React, {Component} from 'react';
import {
    View,
    Text,
    TouchableOpacity,
    NativeModules
} from 'react-native';
import AutoExpandingInput from "./AutoExpandingInput";
const defaultHeight = (62);
const HPading = (16);
const VPading = (6);
export default class LocationTextView extends Component {

    constructor(props) {
        super(props);
        this.state = {
            address: ''
        }
        this.attrData = this.props.attrData;
    }

    getAddress() {
        let _this = this;
        NativeModules.system.getLocation().then(data => {
            console.log('address', data);
            if (!!JSON.parse(data).cityCode) {
                let addressData = JSON.parse(data);
                _this.setState({
                    address: addressData.formattedAddress
                });
                let result = {latitude: addressData.latitude, longitude: addressData.longitude, ip: '', name: addressData.formattedAddress}
                this.attrData.value = JSON.stringify(result);
            } else {
                return;
            }
        });
    }

    render() {
        let {name, code, type, description, isRequired, isPreview} = this.attrData;
        let {placeholder, buttonText} = JSON.parse(this.attrData.data);

        return (
            <TouchableOpacity activeOpacity={1} onPress={()=>this.getAddress()} style={{flex:1,height:defaultHeight,paddingVertical:VPading,paddingHorizontal:HPading,justifyContent:'space-around',alignItems:'center',backgroundColor:'#fff', flexDirection: 'row'}}>
                <View style={{flexDirection: 'row',width:50}}>
                    {isRequired&&<Text style={{ color: "#FF3030" }}>* {" "}</Text>}
                    {!isRequired&&<Text style={{ color: "#fff" }}>* {" "}</Text>}
                    <Text style={{fontSize: 16, color: 'rgba(0, 0, 0, 1)', marginBottom: 5}}>{name||""}</Text>
                </View>
                <Text style={{fontSize: 14,flex:1, color: 'black',backgroundColor: 'white', textAlign: 'left', marginLeft:4,marginRight:4}}
                numberOfLines={2}>
                    {(this.state.address && this.state.address.length > 0) ? this.state.address:''}
                </Text>
                <Text style={{fontSize: 16, color: global.homeColor,marginRight:0,width:60,textAlign:'right'}}>
                    {(this.state.address && this.state.address.length > 0) ? '刷新':'获取'}</Text>
            </TouchableOpacity>
        );
    }
}