适用于 Swift 和 Objective-C 的开放地址码 (Open Location Code)

Build Status Carthage compatible

在十进制经纬度坐标和开放地址码之间进行转换。缩短并恢复给定参考位置的开放地址码。

此存储库是开放地址码的 Swift 实现。它支持 iOS、macOS、tvOS 和 watchOS 上的 Swift 和 Objective-C 项目,以及 Linux 上的 Swift 项目。主存储库 提供对多种其他语言的开放地址码支持。

关于开放地址码

开放地址码是短的、10-11 个字符的代码,可以用来代替街道地址。 这些代码可以离线生成和解码,并使用简化的字符集,最大限度地减少代码包含单词的机会。

代码可以相对于附近的位置进行缩短。 这意味着在许多情况下,只需要代码的四到七个字符。 要恢复原始代码,不需要相同的位置,只要提供附近的位置即可。

代码代表矩形区域而不是点,代码越长,区域越小。 一个 10 个字符的代码代表一个 13.5x13.5 米的区域(在赤道)。 一个 11 个字符的代码代表大约一个 2.8x3.5 米的区域。

使用两种编码算法。 前 10 个字符是字符对,一个用于纬度,一个用于经度,使用 20 进制。 每对都会将代码的面积缩小 400 倍。 只有偶数长度的代码才有意义,因为奇数长度的代码的边长比率为 20:1。 在第 11 位,算法发生变化,因此每个字符从 4x5 网格中选择一个位置。 这允许单字符细化。

支持的环境

此库以 Swift 和 Objective-C Cocoa 框架的形式提供,用于 iOS、macOS、tvOS 和 watchOS,并以纯 Swift 模块的形式提供,用于 macOS 和 Linux。

Swift 版本

构建

Cocoa 框架

要构建框架

xcodebuild -project OpenLocationCode.xcodeproj -scheme OpenLocationCode_iOS -configuration Release
xcodebuild -project OpenLocationCode.xcodeproj -scheme OpenLocationCode_macOS -configuration Release
xcodebuild -project OpenLocationCode.xcodeproj -scheme OpenLocationCode_tvOS -configuration Release
xcodebuild -project OpenLocationCode.xcodeproj -scheme OpenLocationCode_watchOS -configuration Release

或者,如果您已安装 Carthage

carthage build --no-skip-current

测试框架

xcodebuild test -project OpenLocationCode.xcodeproj -scheme OpenLocationCode_macOS -destination 'platform=OS X,arch=x86_64'

Swift 模块

要构建纯 Swift 模块

swift build

测试纯 Swift 模块

swift test

包含一个 Dockerfile,用于在 Linux 容器中构建和运行纯 Swift 模块

docker build .

Swift 代码示例

import OpenLocationCode

// Encode a location with default code length.
if let code = OpenLocationCode.encode(latitude: 37.421908,
                                      longitude: -122.084681) {
  print("Open Location Code: \(code)")
}

// Encode a location with specific code length.
if let code10Digit = OpenLocationCode.encode(latitude: 37.421908,
                                             longitude: -122.084681,
                                             codeLength: 10) {
  print("Open Location Code: \(code10Digit)")
}

// Decode a full code:
if let coord = OpenLocationCode.decode("849VCWC8+Q48") {
  print("Center is \(coord.latitudeCenter), \(coord.longitudeCenter)")
}

// Attempt to trim the first characters from a code:
if let shortCode = OpenLocationCode.shorten(code: "849VCWC8+Q48",
                                            latitude: 37.4,
                                            longitude: -122.0) {
  print("Short code: \(shortCode)")
}

// Recover the full code from a short code:
if let fullCode = OpenLocationCode.recoverNearest(shortcode: "CWC8+Q48",
                                                  referenceLatitude: 37.4,
                                                  referenceLongitude: -122.0) {
  print("Recovered full code: \(fullCode)")
}

Objective-C 代码示例

@import OpenLocationCode;

// ...

// Encode a location with default code length.
NSString *code = [OLCConverter encodeLatitude:37.421908
                                    longitude:-122.084681];
NSLog(@"Open Location Code: %@", code);

// Encode a location with specific code length.
NSString *code10Digit = [OLCConverter encodeLatitude:37.421908
                                           longitude:-122.084681
                                          codeLength:10];
NSLog(@"Open Location Code: %@", code10Digit);

// Decode a full code:
OLCArea *coord = [OLCConverter decode:@"849VCWC8+Q48"];
NSLog(@"Center is %.6f, %.6f", coord.latitudeCenter, coord.longitudeCenter);

// Attempt to trim the first characters from a code:
NSString *shortCode = [OLCConverter shortenCode:@"849VCWC8+Q48"
                                       latitude:37.4
                                      longitude:-122.0];
NSLog(@"Short Code: %@", shortCode);

// Recover the full code from a short code:
NSString *recoveredCode = [OLCConverter recoverNearestWithShortcode:@"CWC8+Q48"
                                                  referenceLatitude:37.4
                                                 referenceLongitude:-122.1];
NSLog(@"Recovered Full Code: %@", recoveredCode);

在本地测试 CI

要在本地运行 Travis CI 测试,请安装

gem install wwtd
gem install xcpretty

并运行

wwtd

您的 Xcode 和 macOS 版本需要与 Travis osx_image 配置相匹配。