【问题标题】:Can I check devices location is open or close on React Native我可以在 React Native 上检查设备位置是打开还是关闭
【发布时间】:2016-11-23 03:09:17
【问题描述】:

我可以在使用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)
     );
  }
}

【问题讨论】:

  • 你的意思是设备上的位置权限吗?
  • 您能澄清一下您的问题吗?很难理解你在问什么。
  • @VladimirNul 我会尽快更新!
  • @AakashSigdel 是的,只检查设备位置是否打开或关闭。请再次检查我更新的问题。
  • @VladimirNul ,我更新了我的坏问题。请再次检查。

标签: geolocation react-native


【解决方案1】:

我认为你可以使用

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

对于从您的应用程序打开设置,我认为您将不得不使用本机代码:D

【讨论】:

  • 在使用geolocation之前如何查看位置条件?
  • @LwinKyawMyat 您可以在 componentDidMount() 和 onSuccess 中检查它,您可以使用 geoLocation 获取位置。
【解决方案2】:

更新:

error 没有可用的位置提供程序在未启用 GPS 时抛出,其 error code 值为 2。(如果需要,请记录 error.message or code当然)。

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

【讨论】:

    【解决方案3】:

    试试这个:

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

    【讨论】:

    • 这将适用于 Android,iOS 呢?我需要使用另一个包吗?
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      此软件包允许您在不使用地理定位的情况下获取启用/禁用位置服务的状态。 https://github.com/rmrs/react-native-settings

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-09
        • 1970-01-01
        • 2011-10-20
        • 2013-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多