当前位置: 首页 > 软件库 > 手机/移动开发 > >

react-native-geolocation-service

授权协议 MIT License
开发语言 Java
所属分类 手机/移动开发
软件类型 开源软件
地区 不详
投 递 者 越嘉石
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

react-native-geolocation-service

React native geolocation service for iOS and android.

Why ?

This library is created in an attempt to fix the location timeout issue on android with the react-native's current implementation of Geolocation API. This library tries to solve the issue by using Google Play Service's new FusedLocationProviderClient API, which Google strongly recommends over android's default framework location API. It automatically decides which provider to use based on your request configuration and also prompts you to change the location mode if it doesn't satisfy your current request configuration.

NOTE: Location request can still timeout since many android devices have GPS issue in the hardware/system level. Check the FAQ for more details.

Installation

yarn

yarn add react-native-geolocation-service

npm

npm install react-native-geolocation-service

Compatibility

RN Version Package Version
>=0.60 >=3.0.0
<0.60 2.0.0
<0.57 1.1.0

Setup

Usage

Since this library was meant to be a drop-in replacement for the RN's Geolocation API, the usage is pretty straight forward, with some extra error cases to handle.

One thing to note, for android this library assumes that location permission is already granted by the user, so you have to use PermissionsAndroid to request for permission before making the location request.

...
import Geolocation from 'react-native-geolocation-service';
...

componentDidMount() {
  if (hasLocationPermission) {
    Geolocation.getCurrentPosition(
        (position) => {
          console.log(position);
        },
        (error) => {
          // See error code charts below.
          console.log(error.code, error.message);
        },
        { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
    );
  }
}

API

async requestAuthorization(authorizationLevel) (iOS only)

Request location permission based on the authorizationLevel parameter. Can be either "whenInUse" or "always". You have to configure the plist keys during setup.

When promise resolves, returns the status of the authorization.

  • disabled - Location service is disabled
  • granted - Permission granted
  • denied - Permission denied
  • restricted - Permission restricted

getCurrentPosition(successCallback, ?errorCallback, ?options)

  • successCallback: Invoked with latest location info.

  • errorCallback: Invoked whenever an error is encountered.

  • options:

    Name Type Default Description
    timeout ms INFINITY Request timeout
    maximumAge ms INFINITY How long previous location will be cached
    accuracy object -- {
       android: Link,
       ios: Link
    }

    If not provided or provided with invalid value, falls back to use enableHighAccuracy
    enableHighAccuracy bool false Use high accuracy mode
    distanceFilter m 100 Minimum displacement in meters
    showLocationDialog bool true Whether to ask to enable location in Android (android only)
    forceRequestLocation bool false Force request location even after denying improve accuracy dialog (android only)
    forceLocationManager bool false If set to true, will use android's default LocationManager API (android only)

watchPosition(successCallback, ?errorCallback, ?options)

  • successCallback: Invoked with latest location info.

  • errorCallback: Invoked whenever an error is encountered.

  • options:

    Name Type Default Description
    accuracy object -- {
       android: Link,
       ios: Link
    }

    If not provided or provided with invalid value, falls back to use enableHighAccuracy
    enableHighAccuracy bool false Use high accuracy mode
    distanceFilter m 100 Minimum displacement between location updates in meters
    interval ms 10000 Interval for active location updates (android only)
    fastestInterval ms 5000 Fastest rate at which your application will receive location updates, which might be faster than interval in some situations (for example, if other applications are triggering location updates) (android only)
    showLocationDialog bool true whether to ask to enable location in Android (android only)
    forceRequestLocation bool false Force request location even after denying improve accuracy dialog (android only)
    forceLocationManager bool false If set to true, will use android's default LocationManager API (android only)
    useSignificantChanges bool false Uses the battery-efficient native significant changes APIs to return locations. Locations will only be returned when the device detects a significant distance has been breached (iOS only)
    showsBackgroundLocationIndicator bool false This setting enables a blue bar or a blue pill in the status bar on iOS. When the app moves to the background, the system uses this property to determine whether to change the status bar appearance to indicate that location services are in use. Users can tap the indicator to return to your app. (iOS only)

clearWatch(watchId)

  • watchId (id returned by watchPosition)

stopObserving()

Stops observing for device location changes. In addition, it removes all listeners previously registered.

Error Codes

Name Code Description
PERMISSION_DENIED 1 Location permission is not granted
POSITION_UNAVAILABLE 2 Location provider not available
TIMEOUT 3 Location request timed out
PLAY_SERVICE_NOT_AVAILABLE 4 Google play service is not installed or has an older version (android only)
SETTINGS_NOT_SATISFIED 5 Location service is not enabled or location mode is not appropriate for the current request (android only)
INTERNAL_ERROR -1 Library crashed for some reason or the getCurrentActivity() returned null (android only)

FAQ

  1. Location timeout still happening ?

    Try the following steps: (Taken from here)

    • Turn phone off/on
    • Turn GPS off/on
    • Disable any battery saver settings, including Power Saving Mode, Battery Management or any third party apps
    • Perform an "AGPS reset": Install the App GPS Status & Toolbox, then in that app, go to Menu > Tools > Manage A-GPS State > Reset

    Adjusting battery saver settings on different devices:

    • HTC: Access your phone settings > battery > power saving mode > battery optimization > select your app > don't optimize > save
    • Huawei: Turn Energy Settings to Normal and add your app to "Protected Apps"
    • LG If you're running Android 6 or higher: Settings > battery & power saving > battery usage > ignore optimizations > turn ON for your app
    • Motorola If you're running Android 6 or higher: Battery > select the menu in the upper right-hand corner > battery optimization > not optimized > all apps > select your app > don't optimize
    • OnePlus (using OxygenOS Settings): Battery > battery optimization > switch to 'all apps' > select your app > don't optimize
    • Samsung: Access battery settings > app power saving > details > your app > disabled
    • Sony If you're running Android 6 or higher: Battery > from the menu in the upper right-hand corner > battery optimization > apps > your app
    • Xiaomi (MIUI OS) If you're running Android 6 or higher: Access your phone settings > additional settings > battery and performance > manage battery usage > apps > your app
  • @react-native-async-storage/async-storage 类似浏览器的storage功能 npm地址:npm i @react-native-async-storage/async-storage github地址:https://github.com/react-native-async-storage/async-storage @react-native-comm

  • React Native 提供了 Geolocation API,让我们可以很方便的获取当前的位置信息,或者监听位置的变化。下面通过样例演示如何使用。 导入 API var Geolocation = require('Geolocation'); 一、获取当前的定位信息 1.getCurrentPosition()方法介绍 static getCurrentPosition(geo_succe

  • 1、Execution failed for task ':react-native-orientation:verifyReleaseResources' //verifyReleaseResources前显示哪个模块报错,就在node_modules找到哪个模块, //打开android-build.gradle,修改sdk版本如下,该错误经常见于0.57版本: compileSdkVers

  • reactNative 获取经纬度踩坑 navigator.geolocation.getCurrentPosition() 百度地图定位不准的问题 最近在做rn的项目要获取经纬度,用到了navigator.geolocation.getCurrentPosition()方法,默认获取的是百度地图坐标系**(经纬度属于WGS84坐标,需要做转换)**,但是后端之前录入的数据是通过高德录入,此时会存

  • RN的定位官方提供了API Gecolocation ,通过这个api我们可以拿到当前的经纬度,代码如下: componentDidMount(){ navigator.geolocation.watchPosition( (position) => { let longitude = JSON.stringify(posi

  • RN 文档上的定位功能需要谷歌框架支持,无疑带来了一些麻烦。github 上也有一些开源库,react-native-geolocation-service等。但是这里还有一个更简便的位置获取 API。 使用内置对象navigator: navigator.geolocation.getCurrentPosition( res => { // conso

  • geolocation API扩展了geolocation的web规范。在android中,地理位置使用android.location. api。 以下替代库用于在React Native中包含新的位置服务API。 React-native-geolocation-service React-native-location 要请求位置访问,需要在应用程序的AndroidManifest.xml文

  • yarn add @react-native-community/geolocation --save 或 npm install @react-native-community/geolocation --save import geolocation from "@react-native-community/geolocation" // 然后使用 navigator.geolocat

  • 我们来看下react-native的geolocation 来看一下这个包 GitHub - Agontuk/react-native-geolocation-service: React native geolocation service for iOS and android 看下文档吧 看下示例代码 ... import Geolocation from 'react-native-geo

 相关资料
  • 本文向大家介绍react-native 启动React Native Packager,包括了react-native 启动React Native Packager的使用技巧和注意事项,需要的朋友参考一下 示例 在最新版本的React Native上,无需运行打包程序。它将自动运行。 默认情况下,这将在端口8081上启动服务器。要指定服务器所在的端口            

  • 百度移动统计SDK支持使用react native框架的H5页面统计,封装好的插件已经在github上开源,相关用法具体请参考:https://github.com/BaiduMobileAnalysis/baidumobstat-react-native。

  • The React Native environment has a lot of little quirks, so this documentation is aimed at helping smooth those over. Please feel free to create issues on GitHub for recommendations and additions to t

  • React Native 可以基于目前大热的开源JavaScript库React.js来开发iOS和Android原生App。而且React Native已经用于生产环境——Facebook Groups iOS 应用就是基于它开发的。 React Native的原理是在JavaScript中用React抽象操作系统原生的UI组件,代替DOM元素来渲染,比如以<View>取代<div>,以<Ima

  • 本文向大家介绍react-native setState,包括了react-native setState的使用技巧和注意事项,需要的朋友参考一下 示例 要在应用程序中更改视图,可以使用setState-这将重新渲染您的组件及其任何子组件。setState在新状态和先前状态之间执行浅表合并,并触发组件的重新呈现。 setState 接受键值对象或返回键值对象的函数 键值对象 功能 使用函数对于基于

  • 诸葛io移动统计支持React Native插件,以下为集成方法。 1. 环境准备 1.1. iOS环境 iOS 8.0+ 代码支持iOS8.0的系统 pod 1.0+ iOS系统的集成依赖于cocoaPod工具 1.2. Android环境 Android SDK 16+ 代码支持Android 16+ 1.3. React Native环境 react-native 0.50+ react-n