我可以在React Native上检查设备位置是打开还是关闭

时间:2016-07-20 03:24:32

标签: geolocation react-native

在使用geolocation之前,我可以检查设备位置是打开还是关闭? 我希望在设备位置关闭时显示App need to run your location, please open your devices location之类的提醒消息。

我可以导航到IOS和Android的原生位置吗?

例如 -

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)
     );
  }
}

5 个答案:

答案 0 :(得分:6)

我认为你可以使用



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




要从您的应用中打开设置,我认为您必须亲自使用本机代码:D

答案 1 :(得分:1)

<强>更新

search.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { ArrayList<KodePos> kodePos = new ArrayList<>(); if (tempData!=null){ for (KodePos kodePosSatu: data ){ String dat = s.toString().toLowerCase(); if (kodePosSatu.getPosCamat().toLowerCase().contains(dat)) { kodePos.add(kodePosSatu); } } tempData = kodePos; recyclerView.setAdapter(new KodePosAdapter(tempData)); } } @Override public void afterTextChanged(Editable s) { } }); 未启用error GPSerror时,会引发code 无位置提供商可用 。(如果你想确定,请记录2。)

error.message or code

答案 2 :(得分:0)

尝试一下:

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);


  }

答案 3 :(得分:0)

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

检查状态

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

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

您还可以订阅更新

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
    }
})

答案 4 :(得分:0)

此软件包可让您无需启用地理位置即可启用/禁用位置服务的状态。 https://github.com/rmrs/react-native-settings