WhereAmI
一款简单的 Core Location 库,使用 Swift 编写,只需几行代码即可获取当前位置和当前地址
- 当前位置
- 当前地址
需求
- Swift 4
- Xcode 9
- iOS 8.0+
- watchOS 2.0
- tvOS 9.0
安装
CocoaPods
WhereAmI 通过 CocoaPods 提供。
您可以使用以下命令安装
$ gem install cocoapods
要将 WhereAmI 集成到您的项目中,请将以下行添加到您的 Podfile 中
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'WhereAmI', '~> 4.0'
Carthage
Carthage 是一个去中心化的依赖管理器,可以自动化将框架添加到您的 Cocoa 应用程序的过程。
您可以使用以下命令通过 Homebrew 安装 Carthage
$ brew update
$ brew install carthage
要使用 Carthage 将 WhereAmI 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "lypiut/WhereAmI" >= 4.0
手动安装
只需将位于 Source
文件夹中的 .swift 文件拖放到您的项目中。
如何使用
请求访问位置服务
WhereAmI 自动处理访问位置服务的权限请求。
不要忘记在应用中设置 NSLocationWhenInUseUsageDescription
或 NSLocationAlwaysUsageDescription
键。
默认情况下,WhereAmI 请求 "在使用时" 权限,但您也可以通过更改 locationAuthorization
变量设置 "始终" 权限。
WhereAmI.sharedInstance.locationAuthorization = .AlwaysAuthorization
设置精度配置文件
WhereAmI 提供了 4 个位置配置文件,这些文件会影响位置精度。
Default // Good mix between precision and location speed with an accuracy of ~200m
Low // Low precision and fast location speed with an accuracy of ~2000m
Medium // Medium precision and an accuracy of ~500m
High // High precision and low location speed with an accuracy of ~10m
您也可以使用 LocationProfile
协议创建自己的位置配置文件。
获取当前位置
要获取当前位置,请使用whereAmI
方法。默认情况下,位置请求仅执行一次。如果您想持续更新位置,请将continuousUpdate
设置为true。
//If you want continuous update
WhereAmI.sharedInstance.continuousUpdate = true;
//Request the current location
whereAmI { response in
switch response {
case .locationUpdated(let location):
//location updated
case .locationFail(let error):
//An error occurred
case .unauthorized:
//The location authorization has been refused
}
}
获取当前位置地址
您可以使用whatIsThisPlace
方法检索有关当前位置(街道、城市等)的信息。
whatIsThisPlace { (response) -> Void in
switch response {
case .success(let placemark):
//reverse geocoding succeed
case .placeNotFound:
// no place found
case .failure(let error):
// an error occurred
case .unauthorized:
//The location authorization has been refused
}
}
示例项目
更多示例在Example
文件夹中。
问题 & 贡献
对于任何错误或建议,请打开GitHub上的问题。
许可协议
WhereAmI采用MIT许可协议。有关更多信息,请参阅LICENSE文件。