https://github.com/ybfacc/react-native-position
react-native Location library, supporting Android and Huawei HMS positioning.
https://github.com/ybfacc/react-native-position
geolocation hms-location location react-native
Last synced: 6 months ago
JSON representation
react-native Location library, supporting Android and Huawei HMS positioning.
- Host: GitHub
- URL: https://github.com/ybfacc/react-native-position
- Owner: YBFACC
- License: mit
- Created: 2024-10-18T16:29:02.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2024-11-16T10:30:58.000Z (11 months ago)
- Last Synced: 2025-03-17T06:51:41.453Z (7 months ago)
- Topics: geolocation, hms-location, location, react-native
- Language: Kotlin
- Homepage:
- Size: 1.41 MB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# react-native-position
通过 `Android’s Location API` 和 `HMS Location REST API` 获取位置信息。
解决 `Google’s Location Services API` 无法使用时,也能获取位置信息。
## 优势
- 不需要集成第三方 SDK, 只需要调用 Https 请求
- 由于 HMS 不需要商业授权, 只需要按量付费([HMS 位置服务](https://developer.huawei.com/consumer/cn/doc/HMSCore-References/web-network-location-0000001051602603))([安卓定位方案的价格对比](http://www.likehide.com/blogs/android/location_server/))## 实现原理
1. 可获取附近的 `wifi` 信息时, 通过 `HMS Location REST API` 请求位置
2. 无 `wifi` 信息时, 尝试 `Android’s Location API` 请求
3. 保底情况, 通过 `cell` 信息 `HMS Location REST API` 请求位置## 必要准备
1. [注册华为开发者账号](https://developer.huawei.com/consumer/cn/doc/start/registration-and-verification-0000001053628148)
2. [创建项目](https://developer.huawei.com/consumer/cn/doc/app/agc-help-createproject-0000001100334664)和[创建应用](https://developer.huawei.com/consumer/cn/doc/app/agc-help-createapp-0000001146718717)
3. 获取到 `apiKey`
4. `GNSS` 需要``和``权限
5. `wifi` 需要 `` 和 ``权限
6. `cell` 需要 ``权限## 使用
```sh
npm install react-native-position
``````ts
import { PermissionsAndroid } from 'react-native'
import { getCurrentPosition } from 'react-native-position'const getLocation = async () => {
const granteds = await PermissionsAndroid.requestMultiple([
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
PermissionsAndroid.PERMISSIONS.READ_PHONE_STATE,
])
if (
granteds['android.permission.ACCESS_FINE_LOCATION'] !== 'granted' ||
granteds['android.permission.ACCESS_COARSE_LOCATION'] !== 'granted' ||
granteds['android.permission.READ_PHONE_STATE'] !== 'granted'
) {
console.log('权限拒绝')
return
}return await getCurrentPosition({
hmsKey: 'your hms key',
GNSStimeout: 5000,
})
}
```## 注意事项
1. 使用 `HMS` 服务中需要遵守[关于华为开发者联盟与隐私的声明](https://developer.huawei.com/consumer/cn/devservice/term)
2. 尽量保证 `Android 10 (API 29)` 以上
3. `mnc` < 10 时, 需要设置为 0(HMS 奇怪的配置)## TODO
- [ ] ios 实现
- [x] 附近 wifi 或基站信息获取, 加强网络定位## Contributing
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
## License
MIT
---
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)