Git Product home page Git Product logo

react-native-gps-state's Introduction

React Native GPS State

React Native Listener for GPS status changes

This library will emit an event whenever there is a change in the GPS status, like when the permission was rejected or when the user disables Location Service in the phone settings.

Instalation

  1. Add library to project
    • yarn add react-native-gps-state
    • OR npm install --save react-native-gps-state
  2. Link library to project
    • react-native link react-native-gps-state

If the link fails, do a manual setup:

Android

android/settings.gradle

include ':react-native-gps-state'
project(':react-native-gps-state').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-gps-state/android')

android/app/build.gradle

dependencies {
   ...
   implementation project(':react-native-gps-state')
}

MainApplication.java

On top, where imports are:

import br.com.dopaminamob.gpsstate.GPSStatePackage;

Add the new GPSStatePackage() class to your list of exported packages.

@Override
protected List<ReactPackage> getPackages() {
    return Arrays.asList(
            new MainReactPackage(),
            new GPSStatePackage(),
    );
}

Usage

Constants

Platform Status Code Constant Description
IOS/Android 0 NOT_DETERMINED The user has not yet made a choice regarding whether this app can use location services.
IOS/Android 1 RESTRICTED This app is not authorized to use location services.
IOS/Android 2 DENIED The user explicitly denied the use of location services for this app or location services are currently disabled in Settings.
IOS/Android 3 AUTHORIZED This app is authorized to use location services.
IOS 3 AUTHORIZED_ALWAYS This app is authorized to start location services at any time.
IOS 4 AUTHORIZED_WHENINUSE This app is authorized to start most location services while running in the foreground

Methods

//Open a system dialog requesting permission
//authType could be one of `AUTHORIZED_ALWAYS` or `AUTHORIZED_WHENINUSE`
GPSState.requestAuthorization(authType)
//Open the system Settings to enable user to toggle Location on.
GPSState.openLocationSettings()
//Open the system Settings in app details, so the user could manage all permissions in the `Permissions` tab
//in Android bellow M will fallback to `openLocationSettings()`
GPSState.openAppDetails()
//ANDROID ONLY
//return true if system version is Marshmallow or above
GPSState.isMarshmallowOrAbove()
//return true if the location permission is granted
GPSState.isAuthorized()
//return true if the location permission is denied
GPSState.isDenied()
//Get the current GPS state
GPSState.getStatus().then((status)=>{

})

Listeners

import GPSState from 'react-native-gps-state'
...
componentWillMount(){
	GPSState.addListener((status)=>{
		switch(status){
			case GPSState.NOT_DETERMINED:
				alert('Please, allow the location, for us to do amazing things for you!')
			break;

			case GPSState.RESTRICTED:
				GPSState.openLocationSettings()
			break;

			case GPSState.DENIED:
				alert('It`s a shame that you do not allowed us to use location :(')
			break;

			case GPSState.AUTHORIZED_ALWAYS:
				//TODO do something amazing with you app
			break;

			case GPSState.AUTHORIZED_WHENINUSE:
				//TODO do something amazing with you app
			break;
		}
	})
	GPSState.requestAuthorization(GPSState.AUTHORIZED_WHENINUSE)
}

componentWillUnmount(){
	GPSState.removeListener()
}

react-native-gps-state's People

Contributors

codlab avatar francofasano avatar friederbluemle avatar henghonglee avatar kenmistry avatar mtebele avatar mujeebrahaman avatar neuberoliveira avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.