LocationTextView.js 1.37 KB
import React, {Component} from 'react';
import {
    View,
    Text,
    TouchableOpacity,
    NativeModules
} from 'react-native';

export default class LocationTextView extends Component {

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

    getAddress() {
        let _this = this;
        NativeModules.system.getLocation().then(data => {
            console.log('address', data);
            if (!!JSON.parse(data).cityCode) {
                let addressData = JSON.parse(data).adCode;
                _this.setState({
                    address: addressData.fromAddress
                });
            } else {
                return;
            }
        });
    }

    render() {
        return (
            <View style={{width: '100%', backgroundColor: 'white', paddingHorizontal: 5, paddingVertical: 10, flexDirection: 'row', alignItems: 'center'}}>
                <Text style={{fontSize: 16, color: 'rgba(0, 0, 0, 1)'}}>当前地点</Text>
                <Text style={{fontSize: 16, color: 'rgba(0, 0, 0, 1)', flex: 1, marginLeft: 30, marginRight: 10}}>{this.state.address}</Text>
                <Text style={{fontSize: 16, color: global.homeColor}} onPress={()=>this.getAddress()}>
                    {(this.state.address && this.state.address.length > 0)? '刷新':'获取'}</Text>
            </View>
        );
    }
}