Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

undefined is not an object (evaluating 'location.coords.latitude'). trying to put the coordinates inside the map

$
0
0

This is a piece of the code:

 import MapView, {  Callout,  Marker,  PROVIDER_GOOGLE,  Region,} from "react-native-maps";     const wait = (timeout) => {  return new Promise(resolve => setTimeout(resolve, timeout));} export default function App() {  const [location, setLocation] = useState(null); const [errorMsg, setErrorMsg] = useState(null); const [regiao, setRegiao] = useState(null); const [refreshing, setRefreshing] = React.useState(false); const onRefresh = React.useCallback(() => {setRefreshing(true);  wait(2000).then(() => setRefreshing(false)); }, []); useEffect(() => {if (Platform.OS === 'android'&& !Constants.isDevice) {  setErrorMsg('Oops, this will not work on Sketch in an Android emulator. Try it on your device!'  );} else {  (async () => {    let { status } = await Location.requestForegroundPermissionsAsync();    if (status !== 'granted') {             setErrorMsg('Permission to access location was denied');               }              let location = await Location.getCurrentPositionAsync({});         setLocation([location.coords.latitude, location.coords.longitude ]);    console.log('My Latitude');    console.log(location.coords.latitude);    console.log('My Longitude');    console.log(location.coords.longitude);  }   )();}    }, []);    let coord = 'Loanding...';  if (errorMsg) {    coord = errorMsg; } else if (location) {coord = JSON.stringify(location); }  const region = {latitude: location.coords.latitude,  longitude:location.coords.longitude,   latitudeDelta: 100,    longitudeDelta: 100,  }; return (<View style={styles.container}><View style={styles.container}><Text  style={styles.sectionHeader}  >{coord}</Text><MapView    style={{ alignSelf: 'stretch', height: '100%' }}    region={region}    provider={PROVIDER_GOOGLE}  /></View></View>  );}

Hi! I'm trying to put the user's coordinates on the map, but it's not working returns this problem: "undefined is not an object (evaluating 'location.coords.latitude')".Does anyone know what I can do to solve this problem, it seems silly but I'm really stuck on it. Being that when I put a value in latitude and longitude it works! example:

const initialRegion = {  latitude: 49.2576508,  longitude: -123.2639868,  latitudeDelta: 100,  longitudeDelta: 100, };

can someone help me please i really need this working


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>