I want to create a react native expo cli geolocation tracking system, i have got the location coordinates via expo location(lib).I have stored these co-ordinates into a variable(text) as a string. Now i want to display these coordinates in a map view in real time. How should i proceed?
Here is the code:
import React, { useState, useEffect } from 'react'; //hooks for using react features without class
import { Platform, Text, View, StyleSheet } from 'react-native'; import Device from 'expo-device';
import * as Location from 'expo-location'; //expo lib to get location coordinates
export default function App() { const [location, setLocation] = useState(null);
const [errorMsg, setErrorMsg] = useState(null);
useEffect(() => {
(async () => {
if (Platform.OS === 'android' && !Device.isDevice) {
setErrorMsg(
'Try it on your device!'
);
return;
}
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
})(); }, []);
let text = 'Waiting..';
if (errorMsg) {
text = errorMsg; }
else if (location) {
text = JSON.stringify(location); }
return (
<View style={styles.container}>
<Text style={styles.paragraph}>{text}</Text>
</View> ); }
const styles = StyleSheet.create({ container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
padding: 20, }, paragraph: {
fontSize: 18,
textAlign: 'center', }, });