MIT License

项目介绍

YsnKsy/react-native-location-enabler 是一个托管在 GitHub 上的开源项目,主要使用 TypeScript 编写,采用 MIT License 许可证,目前获得 164 个 Star、18 次 Fork。

仓库全名
YsnKsy/react-native-location-enabler
开源许可证
MIT License
Star 数
164
Fork 数
18
未关闭 Issue
7

README 摘要

react-native-location-enabler This package makes it easy for an React Native App to ensure that the Android device's system settings are properly configured for the app's location needs. If your app needs to request location, the device needs to enable the appropriate system settings, such as GPS or Wi-Fi scanning. Rather than directly enabling services such as the device's GPS, your app specifies the…

完整内容见 GitHub 原始 README 或本页 。

常见问题

react-native-location-enabler 用什么语言开发?

主要使用 TypeScript 编写,采用 MIT License 许可证,目前有 164 个 Star。

react-native-location-enabler 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 react-native-location-enabler 的源码?

可以直接克隆仓库:git clone https://github.com/YsnKsy/react-native-location-enabler.git,默认分支为 master;安装与运行步骤见仓库 README。

react-native-location-enabler 最近还在维护吗?

仓库最近一次更新为 2023年5月14日,当前有 7 个未关闭 Issue;Star 数为 164,可作为社区活跃度的参考。

关于

GitHub 原始描述(英文)

This package makes it easy for an React Native App to ensure that the Android device's system settings are properly configured for the app's location needs. If your app needs to request location, the device needs to enable the appropriate system settings, such as GPS or Wi-Fi scanning. Rather than directly enabling services such as the device's GPS, your app specifies the required level of accuracy/power consumption, and the device automatically makes the appropriate changes to system settings.

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
201
价值分
103
活跃状态
活跃

语言占比

Java
JavaScript
Kotlin
TypeScript

Release

README

react-native-location-enabler

GitHub version npm version Last Release Pull Request GitHub issues PRs Welcome

Platform - Android styled with Prettier semantic-release GitHub license

This package makes it easy for an React Native App to ensure that the Android device's system settings are properly configured for the app's location needs. If your app needs to request location, the device needs to enable the appropriate system settings, such as GPS or Wi-Fi scanning. Rather than directly enabling services such as the device's GPS, your app specifies the required level of accuracy/power consumption, and the device automatically makes the appropriate changes to system settings.


Installation

yarn add react-native-location-enabler

Usage

Example using Hook (React Hooks API) :

import LocationEnabler from 'react-native-location-enabler';

const {
  PRIORITIES: { HIGH_ACCURACY },
  useLocationSettings,
} = LocationEnabler;

// React Component
const App = () => {
  const [enabled, requestResolution] = useLocationSettings(
    {
      priority: HIGH_ACCURACY, // default BALANCED_POWER_ACCURACY
      alwaysShow: true, // default false
      needBle: true, // default false
    },
    false /* optional: default undefined */
  );

  return (
    <View>
      {!enabled && (
        <Button
          onPress={requestResolution}
          title="Request Resolution Location Settings"
        />
      )}
    </View>
  );
};

Example using Listener :

import LocationEnabler from "react-native-location-enabler"

const {
  PRIORITIES: { HIGH_ACCURACY },
  addListener,
  checkSettings,
  requestResolutionSettings
} = LocationEnabler

// Adds a listener to be invoked when location settings checked using
// [checkSettings] or changed using [requestResolutionSettings]
const listener = addListener(({ locationEnabled }) =>
  console.log(`Location are ${ locationEnabled ? 'enabled' : 'disabled' }`);
);

// Define configuration
const config = {
  priority: HIGH_ACCURACY, // default BALANCED_POWER_ACCURACY
  alwaysShow: true, // default false
  needBle: false, // default false
};

// Check if location is enabled or not
checkSettings(config);

// If location is disabled, prompt the user to turn on device location
requestResolutionSettings(config);

// ...
// Removes this subscription
listener.remove();

Example React Native App :

Clone the repo

git clone https://github.com/YsnKsy/react-native-location-enabler.git && cd react-native-location-enabler

Install npm dependencies

yarn

Start Metro ( javascript bundler )

yarn example start

Install and launch example app on the device

yarn example android


API

Properties

PRIORITIES

import LocationEnabler from 'react-native-location-enabler';

const {
  HIGH_ACCURACY,
  BALANCED_POWER_ACCURACY,
  LOW_POWER,
  NO_POWER,
} = LocationEnabler.PRIORITIES;

Static object contain a list quality of service for location updates. If your application wants high accuracy location it should set prioprity to ‘HIGH_ACCURACY’. If you want negligible power impact, but to still receive location updates when available, then set priority to ‘NO_POWER’.


Methods

useLocationSettings({ priority, alwaysShow, needBle }, initialStatus?)

import LocationEnabler from 'react-native-location-enabler';

const {
  useLocationSettings,
  PRIORITIES: { HIGH_ACCURACY },
} = LocationEnabler;

const [enabled, requestResolution] = useLocationSettings({
  priority: HIGH_ACCURACY, // optional: default BALANCED_POWER_ACCURACY
  alwaysShow: true, // optional: default false
  needBle: true, // optional: default false
});

console.log(`Location are ${enabled ? 'enabled' : 'disabled'}`);

// ...
if (!enabled) {
  requestResolution();
}

Hook let you check the user’s device location status ‘on’ / ‘off’ and method let you display an activity where they can turn location ‘on’.


checkSettings({ priority, alwaysShow, needBle })

import LocationEnabler from 'react-native-location-enabler';

const {
  checkSettings,
  PRIORITIES: { HIGH_ACCURACY },
} = LocationEnabler;

checkSettings({
  priority: HIGH_ACCURACY, // optional: default BALANCED_POWER_ACCURACY
  alwaysShow: true, // optional: default false
  needBle: true, // optional: default false
});

Checking if the user’s device location is turned on / off.


requestResolutionSettings({ priority, alwaysShow, needBle })

import LocationEnabler from 'react-native-location-enabler';

const {
  requestResolutionSettings,
  PRIORITIES: { HIGH_ACCURACY },
} = LocationEnabler;

requestResolutionSettings({
  priority: HIGH_ACCURACY, // optional: default BALANCED_POWER_ACCURACY
  alwaysShow: true, // optional: default false
  needBle: true, // optional: default false
});

Display an activity where they can turn location ‘on’ using a location request.


addListener(callback, context?)

import LocationEnabler from 'react-native-location-enabler';

let listener = null;

function cb(result) {
  const { locationEnabled } = result;

  console.log(`Location are ${locationEnabled ? 'enabled' : 'disabled'}`);

  if (listener !== null) {
    // remove listener when you finish
    listener.remove();
  }
}

listener = LocationEnabler.addListener(cb);

Adds a listener to be invoked when onChangeLocationSettings are emitted. An optional calling context may be provided. The data arguments emitted will be passed to the listener function.


Contributing

Press the STAR button 😀 and see the contributing guide to learn how to contribute to the repository and the development workflow.

Code of Conduct

See the code of conduct guide.

License

See the MIT License

评论

暂无评论