Giter Site home page Giter Site logo

reactnativ / react-native-android-location-enabler Goto Github PK

View Code? Open in Web Editor NEW

This project forked from richou/react-native-android-location-enabler

0.0 0.0 0.0 3.94 MB

Display a GoogleMap like android popup to ask for user to enable location services if disabled

License: MIT License

JavaScript 11.29% Ruby 0.84% Java 38.27% Kotlin 35.45% TypeScript 14.14%

react-native-android-location-enabler's Introduction

react-native-android-location-enabler

Allow to display a GoogleMap like android popup to ask for user to enable location services if disabled

See in action

Getting started

ReactNative >= 0.60.0

Installation

$ npm install react-native-android-location-enabler --save

Since ReactNative 0.60.0 and ReactNative Cli 2.0.0, no action is needed to add a native module.

ReactNative < 0.60.0

Installation

$ npm install [email protected] --save

Configuration

$ react-native link react-native-android-location-enabler

Manual Configuration

  1. Open up android/app/src/main/java/[...]/MainApplication.java
  • Add import com.heanoria.library.reactnative.locationenabler.RNAndroidLocationEnablerPackage; to the imports at the top of the file
  • Add new RNAndroidLocationEnablerPackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
    include ':react-native-android-location-enabler'
    project(':react-native-android-location-enabler').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-android-location-enabler/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
      compile project(':react-native-android-location-enabler')
    

Usage

Check if location is enabled

import { isLocationEnabled } from 'react-native-android-location-enabler';
import { Platform } from 'react-native';

async function handleCheckPressed() {
  if (Platform.OS === 'android') {
    const checkEnabled: boolean = await isLocationEnabled();
    console.log('checkEnabled', checkEnabled)
  }
}

Show a modal to ask user to enable location

import { promptForEnableLocationIfNeeded } from 'react-native-android-location-enabler';
import { Platform } from 'react-native';

async function handleEnabledPressed() {
  if (Platform.OS === 'android') {
    try {
      const enableResult = await promptForEnableLocationIfNeeded();
      console.log('enableResult', enableResult);
      // The user has accepted to enable the location services
      // data can be :
      //  - "already-enabled" if the location services has been already enabled
      //  - "enabled" if user has clicked on OK button in the popup
    } catch (error: unknown) {
      if (error instanceof Error) {
        console.error(error.message);
        // The user has not accepted to enable the location services or something went wrong during the process
        // "err" : { "code" : "ERR00|ERR01|ERR02|ERR03", "message" : "message"}
        // codes :
        //  - ERR00 : The user has clicked on Cancel button in the popup
        //  - ERR01 : If the Settings change are unavailable
        //  - ERR02 : If the popup has failed to open
        //  - ERR03 : Internal error
      }
    }
  }
}

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library

react-native-android-location-enabler's People

Contributors

reactnativ avatar richou 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.