Get current location, latitude and longitude in ReactNative using react-native-maps

后端 未结 3 823
轮回少年
轮回少年 2020-12-23 18:16

I am developing a map location. When I click in some particular place I get the latitude and longitude, but not the current location, latitude and longitude.

I don\'

3条回答
  •  一向
    一向 (楼主)
    2020-12-23 18:57

    Look for location permission using the following code:

    try {
        const granted = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
        )
        if (granted === PermissionsAndroid.RESULTS.GRANTED) {
            alert("You can use the location")
        }
        else {
            alert("Location permission denied")
        }
    }
    catch (err) {
        console.warn(err)
    }
    

    Fetch the current location latitude and longitude using the following code:

    this.watchID = navigator.geolocation.watchPosition((position) => {
        let region = {
            latitude:       position.coords.latitude,
            longitude:      position.coords.longitude,
            latitudeDelta:  0.00922*1.5,
            longitudeDelta: 0.00421*1.5
        }
    }
    

提交回复
热议问题