Skip to content

Commit

Permalink
实现 iOS 逆地理编码
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuxiang committed May 3, 2018
1 parent 0a6e6db commit 68e8916
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 28 deletions.
36 changes: 26 additions & 10 deletions docs/geolocation.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,74 @@
## Methods

### `init({ ios: string, android: string }): Promise<void>`

初始化并申请权限,必须在调用其他方法前调用。

---

### `setOptions(options: Options)`

设置定位参数。

---

### `start()`

开始定位。

---

### `stop()`

停止定位。

---

### `addLocationListener(Location => void): EventSubscription`

添加定位监听函数。

---

### `getLastLocation(): Promise<Location>`

获取最近一次定位结果。

## Types

```typescript
type Options = {
/**
* 最小更新距离,默认 0 米,即只要位置更新就立即返回,仅用于 iOS
*
* 更多请参考 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 // 兴趣点
}
```
```
10 changes: 8 additions & 2 deletions docs/installation.md
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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
```
```
43 changes: 34 additions & 9 deletions lib/ios/RCTGeolocationModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>",
"repository": {
"type": "git",
Expand Down
14 changes: 8 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 68e8916

Please sign in to comment.