From 68e89166b98f5f7d8b74e2dbaab8b0cc96849830 Mon Sep 17 00:00:00 2001 From: 7c00 Date: Fri, 4 May 2018 00:00:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20iOS=20=E9=80=86=E5=9C=B0?= =?UTF-8?q?=E7=90=86=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/geolocation.md | 36 ++++++++++++++++++++-------- docs/installation.md | 10 ++++++-- lib/ios/RCTGeolocationModule.m | 43 +++++++++++++++++++++++++++------- package.json | 2 +- readme.md | 14 ++++++----- 5 files changed, 77 insertions(+), 28 deletions(-) diff --git a/docs/geolocation.md b/docs/geolocation.md index 73ae0be..e5e53d9 100644 --- a/docs/geolocation.md +++ b/docs/geolocation.md @@ -3,34 +3,41 @@ ## Methods ### `init({ ios: string, android: string }): Promise` + 初始化并申请权限,必须在调用其他方法前调用。 --- ### `setOptions(options: Options)` + 设置定位参数。 --- ### `start()` + 开始定位。 --- ### `stop()` + 停止定位。 --- ### `addLocationListener(Location => void): EventSubscription` + 添加定位监听函数。 --- ### `getLastLocation(): Promise` + 获取最近一次定位结果。 ## Types + ```typescript type Options = { /** @@ -38,23 +45,32 @@ type Options = { * * 更多请参考 https://bit.ly/2vPTXY7 */ - distanceFilter: number, + distanceFilter: number /** * 定位请求间隔,默认 2000 毫秒,仅用于 Android * * 更多请参考 https://bit.ly/2KhmCbu */ - interval: number, + interval: number } type Location = { - accuracy: number, // 定位精度 (m) - latitude: number, // 经度 - longitude: number, // 纬度 - altitude: number, // 海拔 (m),需要 GPS - speed: number, // 速度 (m/s),需要 GPS - direction: number, // 移动方向,需要 GPS - timestamp: number, // 定位时间 + accuracy: number // 定位精度 (m) + latitude: number // 经度 + longitude: number // 纬度 + altitude: number // 海拔 (m),需要 GPS + speed: number // 速度 (m/s),需要 GPS + direction: number // 移动方向,需要 GPS + timestamp: number // 定位时间 + address?: string // 详细地址 + country?: string // 国家 + province?: string // 省份 + city?: string // 城市 + cityCode?: string // 城市编码 + district?: string // 区 + street?: string // 街道 + streetNumber?: string // 门牌号 + poiName?: string // 兴趣点 } -``` \ No newline at end of file +``` diff --git a/docs/installation.md b/docs/installation.md index e337af9..f396aba 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -1,17 +1,21 @@ ## 安装 + ```shell npm i react-native-amap-geolocation ``` ### Android + ```shell react-native link react-native-amap-geolocation ``` ### iOS + 为了简化配置过程,暂时只提供 CocoaPods 支持。 在 `ios` 目录下新建文件 `Podfile`: + ```ruby platform :ios, '8.0' @@ -41,9 +45,11 @@ target 'Your Target' do pod 'react-native-amap-geolocation', path: '../node_modules/react-native-amap-geolocation/lib/ios' end ``` -*以上配置针对 RN v0.55,v0.53 及其他版本请参考: https://facebook.github.io/react-native/docs/0.53/integration-with-existing-apps.html* + +_以上配置针对 RN v0.55,v0.53 及其他版本请参考: https://facebook.github.io/react-native/docs/0.53/integration-with-existing-apps.html_ 然后运行: + ```shell pod install -``` \ No newline at end of file +``` diff --git a/lib/ios/RCTGeolocationModule.m b/lib/ios/RCTGeolocationModule.m index 06901ed..c838ad2 100644 --- a/lib/ios/RCTGeolocationModule.m +++ b/lib/ios/RCTGeolocationModule.m @@ -15,6 +15,9 @@ @implementation RCTLocationModule { if (options[@"distanceFilter"]) { _manager.distanceFilter = [options[@"distanceFilter"] doubleValue]; } + if (options[@"reGeocode"]) { + _manager.locatingWithReGeocode = [options[@"reGeocode"] boolValue]; + } } RCT_REMAP_METHOD(init, key:(NSString *)key resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) { @@ -42,15 +45,37 @@ @implementation RCTLocationModule { } - (id)json:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode { - return @{ - @"accuracy": @(location.horizontalAccuracy), - @"latitude": @(location.coordinate.latitude), - @"longitude": @(location.coordinate.longitude), - @"altitude": @(location.altitude), - @"speed": @(location.speed), - @"direction": @(location.course), - @"timestamp": @(location.timestamp.timeIntervalSince1970 * 1000), - }; + if (reGeocode && reGeocode.formattedAddress.length) { + return @{ + @"accuracy": @(location.horizontalAccuracy), + @"latitude": @(location.coordinate.latitude), + @"longitude": @(location.coordinate.longitude), + @"altitude": @(location.altitude), + @"speed": @(location.speed), + @"direction": @(location.course), + @"timestamp": @(location.timestamp.timeIntervalSince1970 * 1000), + @"address": reGeocode.formattedAddress, + @"poiName": reGeocode.POIName, + @"country": reGeocode.country, + @"province": reGeocode.province, + @"city": reGeocode.city, + @"cityCode": reGeocode.citycode, + @"district": reGeocode.district, + @"street": reGeocode.street, + @"streetNumber": reGeocode.number, + @"adCode": reGeocode.adcode, + }; + } else { + return @{ + @"accuracy": @(location.horizontalAccuracy), + @"latitude": @(location.coordinate.latitude), + @"longitude": @(location.coordinate.longitude), + @"altitude": @(location.altitude), + @"speed": @(location.speed), + @"direction": @(location.course), + @"timestamp": @(location.timestamp.timeIntervalSince1970 * 1000), + }; + } } - (void)amapLocationManager:(AMapLocationManager *)manager diff --git a/package.json b/package.json index 19b5d03..79f04cb 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "react-native-amap-geolocation", "description": "React Native geolocation module for Android + iOS", "keywords": ["react-native", "amap", "geolocation"], - "version": "0.2.1", + "version": "0.3.0", "author": "7c00 ", "repository": { "type": "git", diff --git a/readme.md b/readme.md index 71ff013..aec6e52 100644 --- a/readme.md +++ b/readme.md @@ -6,10 +6,10 @@ React Native 高德地图定位模块,支持 Android + iOS。 ## 使用 -1. [安装](docs/installation.md) -2. 获取 Key: - - [Android](http://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key) - - [iOS](http://lbs.amap.com/api/ios-location-sdk/guide/create-project/get-key) +1. [安装](docs/installation.md) +2. 获取 Key: + * [Android](http://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key) + * [iOS](http://lbs.amap.com/api/ios-location-sdk/guide/create-project/get-key) ```javascript import { Geolocation } from "react-native-amap-geolocation" @@ -29,10 +29,12 @@ Geolocation.start() ``` ## 文档 -- [Geolocation](docs/geolocation.md) + +* [Geolocation](docs/geolocation.md) ## 示例 -你可以直接下载安装 [example.apk](https://github.com/qiuxiang/react-native-amap-geolocation/releases/download/v0.1.0/example.apk),或者按照以下步骤运行项目示例: + +你可以直接下载安装 [example.apk](https://github.com/qiuxiang/react-native-amap-geolocation/releases/download/v0.3.0/example.apk),或者按照以下步骤运行项目示例: ```shell yarn