GeocoreKit
这是一个非常早期的版本。
GeocoreKit 是一个用于访问 Geocore API 服务器的纯 Swift 框架。
安装
GeocoreKit 可通过 CocoaPods 或 Carthage 获取。要使用 CocoaPods 安装,将以下行添加到 Podfile 中
pod "GeocoreKit"
要使用 Carthage 安装,将以下行添加到 Cartfile 中
github "geocore/geocore-swift"
基本用法
在开始使用库之前,可以通过将以下密钥添加到 Info.plist
文件中来轻松设置连接
键名 | 值 |
---|---|
GeocoreBaseURL | Geocore API 的基础 URL |
GeocoreProjectId | MapMotion 提供的项目 ID |
通过导入 GeocoreKit
,可以使用 sharedInstance
静态成员访问库的主要单例实例,如下所示
import GeocoreKit
// ....
let geocore = Geocore.sharedInstance
配置连接后,登录Geocore最简单的方式是使用来自Geocore单例对象的loginWithDefaultUser
。Geocore提供的大部分函数都返回由PromiseKit提供的Promise
对象。
Geocore.sharedInstance.loginWithDefaultUser().then { accessToken -> Void in
println("Logged in to Geocore successfully, with access token = \(accessToken)")
}
代码片段
以下是一个基本的例子,展示了如何连接使用Promises。
- 初始化框架。
- 登录到Geocore。
- 获取用户对象。
- 获取坐标附近的一些地点。
import PromiseKit
import GeocoreKit
Geocore.sharedInstance
.setup(baseURL: GEOCORE_BASEURL, projectId: GEOCORE_PROJECTID)
.login(userId: GEOCORE_USERID, password: GEOCORE_USERPASSWORD)
.then { accessToken -> Promise<GeocoreUser> in
print("Access Token = \(accessToken)")
return GeocoreUser.get(GEOCORE_USERID)
}
.then { user -> Promise<[GeocorePlace]> in
print("--- The user as promised:")
print("Id = \(user.id!), Name = \(user.name!)")
return GeocorePlaceQuery()
.withCenter(latitude: 35.666, longitude: 139.7126)
.nearest()
}
.then { places -> Void in
print("--- Some places as promised:")
for place in places {
print("Id = \(place.id!), Name = \(place.name!), Point = (\(place.point!.latitude!), \(place.point!.longitude!))")
}
}
.catch { error in
print("--- Cannot fulfill promise because of : \(error)")
}
以下示例展示了如何获取指定矩形内的地点
GeocorePlaceQuery()
.withRectangle(
minimumLatitude: 35.66617440081799,
minimumLongitude: 139.7126117348629,
maximumLatitude: 35.67753978462231,
maximumLongitude: 139.72917705773887)
.withinRectangle()
.then { places -> Void in
for place in places {
print("Id = \(place.id), Name = \(place.name), Point = (\(place.point?.latitude), \(place.point?.longitude))")
}
}
.catch { error in
print("--- Cannot fulfill promise because of : \(error)")
}
备注
- 该框架初始结构是基于Swift,框架和CocoaPods。
- 该框架使用Alamofire进行HTTP网络通信,SwiftyJSON进行JSON处理,以及PromiseKit进行Promises。