Obtener coordenadas exactas del gps

  • Autor de la entrada:
  • Categoría de la entrada:React Native

import React from ‘react’;
import { StyleSheet, View, Text, Dimensions, ScrollView,TouchableOpacity,Platform } from ‘react-native’;
import MapView, { Marker, ProviderPropType,AnimatedRegion, MAP_TYPES } from ‘react-native-maps’;
import * as Location from ‘expo-location’;
import * as Permissions from ‘expo-permissions’;
import { Component } from ‘react’;
import Constants from ‘expo-constants’;

export default class App extends React.Component {
state = {
location: null,
errorMessage: null,
};

componentWillMount() {
if (Platform.OS === ‘android’ && !Constants.isDevice) {
this.setState({
errorMessage: ‘Oops, this will not work on Sketch in an Android emulator. Try it on your device!’,
});
} else {
this._getLocationAsync();
}
}

_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== ‘granted’) {
this.setState({
errorMessage: ‘Permission to access location was denied’,
});
}

let location = await Location.getCurrentPositionAsync({});
this.setState({ location });
};

render() {
let text = ‘Waiting..’;
if (this.state.errorMessage) {
text = this.state.errorMessage;
} else if (this.state.location) {
text = JSON.stringify(this.state.location.coords.latitude);
}

return (
<View style={styles.container}>
<Text style={styles.paragraph}>{text}</Text>
</View>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: ‘center’,
justifyContent: ‘center’,
paddingTop: Constants.statusBarHeight,
backgroundColor: ‘#ecf0f1’,
},
paragraph: {
margin: 24,
fontSize: 18,
textAlign: ‘center’,
},
});