How to catch the current Geolocation of the cell phone using React Native?

Asked

Viewed 373 times

-1

I’m trying to get my latitude and longitude, so I used the following code:

import { View, Text, TextInput, StyleSheet, TouchableOpacity } from 'react-native';
import MapView, { PROVIDER_GOOGLE } from 'react-native-maps';
import { Marker } from 'react-native-maps';
import Geolocation from '@react-native-community/geolocation';

export default function Map({ navigation }) {
    
    const [latitude, setLatitude]=useState(0)
    Geolocation.getCurrentPosition(data=>{
        setLatitude(data.coords.latitude)
    });

    const [longitude, setLongitude]=useState(0)
    Geolocation.getCurrentPosition(data=>{
        setLongitude(data.coords.longitude)
    });

    return (
        <MapView
            provider={PROVIDER_GOOGLE}
            region={{
                latitude: latitude,
                longitude: longitude,
                latitudeDelta: 0.0042,
                longitudeDelta: 0.0031,
            }}
            style={styles.mapView}
            rotateEnabled={false}
        >
            <Marker
                coordinate={{
                    latitude: latitude,
                    longitude: longitude
                }}
            />
        </MapView>
    );
}
const styles = StyleSheet.create({
    mapView: {
        flex: 1,
    }
})

It returns me a latitude and longitude, but they are not of my current position, it is of another location (another city in the USA). I need latitude and longitude to store in a comic book I already own.

I’m using Android Simulator and when I use Google Maps it gets my location right.

PS: I already added permissions on Android Manifest.

  • Can you test on a real device? This is likely to be emulator stuff (it’s just a guess)

1 answer

0

import Geolocation from '@react-native-community/geolocation';

const [CurrentRegion, setCurrentRegion] = useState(null);

async function loadInitialPosition(){
    await Geolocation.getCurrentPosition(
        (position) => {
            const {coords} = position
            const {latitude, longitude} = coords
            setCurrentRegion({
                latitude,
                longitude,
                latitudeDelta: 0,
                longitudeDelta: 0.005,
            })
        },
    );
}

useEffect(() => {
    loadInitialPosition();
},[]);


function handleRegionChange(region) {
    console.log("regiao", region)
    setCurrentRegion(region);
}

if (!getCurrentRegion) {
    return null;
}


<MapsView
    onRegionChangeComplete={handleRegionChange}
    style={{ flex: 1 }}
    initialRegion={getCurrentRegion}
    showsUserLocation
    loadingEnabled
/>

Run and tell me if it worked.

  • I tried to apply here in my app your code but I’m still a little lost of how to apply it within my <Mapview>

  • This code may be a solution to the question, but your answer might be better if you include an explanation of the key points of the code. The goal is not only to help those who asked the question, but the next visitors as well. Read more on Code-only answers - What to do?.

  • Thanks Rafael, I already helped him and we discovered some mistakes and already fixed, but it was good you have given me this alert, in my next answer I will make some notes of what each line does, so anyone who enters already leave with the answer in hand

Browser other questions tagged

You are not signed in. Login or sign up in order to post.