lwinkyawmyat
lwinkyawmyat

Reputation: 1242

Can I check devices location is open or close on React Native

Can I check devices location is open or close before using geolocation? I want to show alert message like App need to run your location, please open your devices location, when devices location is close.

Can I navigate to go native location setting both IOS and Android?

Like example -

componentDidMount() {
  // Check condition device location is open or close.
  if( DevicesLocation === 'close' ) {
     alert('App need to run your location, please open your devices location');
     // other process 
  } else {
     navigator.geolocation.getCurrentPosition(
        (position) => {
           var initialPosition = JSON.stringify(position);
           this.setState({ initialPosition});
        },
        (error) => console.log(error.message)
     );
  }
}

Upvotes: 8

Views: 19343

Answers (5)

Hosam Abdelnaser
Hosam Abdelnaser

Reputation: 147

This package allows you to get the status of location service enabled/disabled without using geolocation. https://github.com/rmrs/react-native-settings

Upvotes: 0

ismael oliva
ismael oliva

Reputation: 97

https://github.com/nearit/react-native-connectivity-status

Check Status

import ConnectivityManager from 'react-native-connectivity-status'

// Check if Location Services are enabled
const locationServicesAvailable = await ConnectivityManager.areLocationServicesEnabled()

You can also Subscribe to updates

import ConnectivityManager from 'react-native-connectivity-status'

const connectivityStatusSubscription = ConnectivityManager.addStatusListener(({ eventType, status }) => {
    switch (eventType) {
        case 'bluetooth':
                    console.log(`Bluetooth is ${status ? 'ON' : 'OFF'}`)
                break
        case 'location':
                    console.log(`Location Services are ${status ? 'AVAILABLE' : 'NOT available'}`)
                break
    }
})

Upvotes: 0

Sully
Sully

Reputation: 417

try this:

export async function request_device_location_runtime_permission() {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      {
        'title': 'ReactNativeCode Location Permission',
        'message': 'ReactNativeCode App needs access to your location '
      }
    )
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {

       Alert.alert("location active");
    }
    else {

      Alert.alert("location de active");

    }
  } catch (err) {
    console.warn(err)
  }
}


 async componentDidMount() {

    if (Platform.OS === 'android') {

      await request_device_location_runtime_permission();

    }

    this.getLongLat = navigator.geolocation.watchPosition(
      (position) => {
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
          error: null,
        });
      },
      (error) => this.setState({ error: error.message }),
      { enableHighAccuracy: true, timeout: 2000, maximumAge: 100, distanceFilter: 10 },
    );

  }

  componentWillUnmount() {

    navigator.geolocation.clearWatch(this.getLongLat);


  }

Upvotes: 1

Amal p
Amal p

Reputation: 3042

Update:

The error no location provider is available is thrown when GPS is not enabled its error code value is 2.(log the error.message or code if you want to be sure).

const NO_LOCATION_PROVIDER_AVAILABLE = 2;

navigator.geolocation.getCurrentPosition(
  (position) => {
    this.setState({ position });
  },
  (error) => {
    if (error.code === NO_LOCATION_PROVIDER_AVAILABLE) {
         //Show alert or something here that GPS need to turned on.
      }
  },
  {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);

Upvotes: 4

Aakash Sigdel
Aakash Sigdel

Reputation: 9300

I think you can use

navigator.geolocation.getCurrentPosition(
  (position) => {
   //do stuff with location
  },
  (error) => {
    this.setState({locationEnabled: false}),
  },
  {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);

For opening settings from your app, I think you will have to get your hands dirty with native code :D

Upvotes: 8

Related Questions