unl-core 0.0.9

unl-core 0.0.9

UNL Core 维护。



unl-core 0.0.9

  • UNL 全球

UNL Core Swift

这个库可以用来将 UNL locationId 转换为经纬度点,同时包含一些辅助函数,如检索 UNL 单元的边界或给定边框的 UNL 网格线(这些可以用来绘制 UNL 单元或 UNL 网格)。

要集成 core-swift 库

  1. 在您的项目 Podfile 中添加此依赖项
pod 'unl-core'
  1. 运行 pod install

有关使用 CocoaPods 的更多信息,请参阅这里

要运行 core-swift 项目

  1. 克隆仓库
  2. 在项目目录中打开终端
  3. 运行 pod install
  4. 打开 unl-core.xcworkspace 并运行项目

public class Point: Equatable, Decodable {
    public let lat: Double;
    public let lon: Double;
    
    public init(lat: Double, lon: Double){
        self.lat = lat;
        self.lon = lon;
    }
}

边界

public class Bounds: Equatable, Decodable {
    public let n: Double;
    public let e: Double;
    public let s: Double;
    public let w: Double;
    
    public init(n: Double, e: Double, s: Double, w: Double){
        self.n = n;
        self.e = e;
        self.s = s;
        self.w = w;
    }
}

高程

public class Elevation: Equatable {
    public let elevation: Int;
    public let elevationType: String;
    
    public init(elevation: Int, elevationType: String){
        self.elevation = elevation;
        self.elevationType = elevationType;
    }
}

具有高程的点

public class PointWithElevation: Equatable {
    public let coordinates: Point;
    public let elevation: Elevation;
    public let bounds: Bounds;
    public init(coordinates: Point, elevation: Elevation, bounds: Bounds){
        self.coordinates = coordinates;
        self.elevation = elevation;
        self.bounds = bounds;
    }
}

具有高程的位置标识符

public class LocationIdWithElevation: Equatable {
    public let locationId: String;
    public let elevation: Elevation;
    public init(locationId: String, elevation: Elevation){
        self.locationId = locationId;
        self.elevation = elevation;
    }
}

邻居

public class Neighbours: Equatable {
    public let n: String;
    public let ne: String;
    public let e: String;
    public let se: String;
    public let s: String;
    public let sw: String;
    public let w: String;
    public let nw: String;
    
    public init(n: String, ne: String, e: String, se: String,s: String, sw: String, w: String, nw: String){
        self.n = n;
        self.ne = ne;
        self.e = e;
        self.se = se;
        self.s = s;
        self.sw = sw;
        self.w = w;
        self.nw = nw;
    }
}

位置

public class Location: Equatable, Decodable {
    public let point: Point;
    public let elevation: Elevation;
    public let bounds: Bounds;
    public let geohash: String;
    public let words: String;
    
    public init(point: Point, elevation: Elevation, bounds: Bounds, geohash: String, words: String){
        self.point = point;
        self.elevation = elevation;
        self.bounds = bounds;
        self.geohash = geohash;
        self.words = words;
    }
}

UnlCore 方法

您可以将 UnlCore 类导入您的文件中,以调用以下描述的任何方法

import unl-core;

encode

将纬度/经度坐标编码为位置标识符。encode 方法有多种签名。

编码到指定的精度。最后一个参数用于指定高程信息:数字和类型(楼层 | "heightincm")。

public static func encode(lat: Double, lon: Double, precision: Int, elevation: Elevation) throws -> String

示例

UnlCore.encode(lat: 57.648, lon: 10.41, 6, elevation: Elevation(elevation: 87, elevationType: "heightincm"));

返回

"u4pruy#87"

精度和/或高程可以从参数中省略。在这种情况下,将使用默认值

public static let defaultPrecision = 9;
public static let defaultElevation = Elevation(elevation: 0, elevationType: "floor");
public static func encode(lat: Double, lon: Double, precision: Int) throws -> String

示例

UnlCore.encode(lat: 57.648, lon: 10.41, precision: 6);

返回

"u4pruy"
public static func encode(lat: Double, lon: Double, elevation: Elevation) throws -> String

示例

UnlCore.encode(lat: 52.37686, lon: 4.90065, elevation: Elevation(-2));

返回

"u173zwbt3@-2"
public static func encode(lat: Double, lon: Double) throws -> String

示例

UnlCore.encode(lat: 52.37686, lon: 4.90065);

返回

"u173zwbt3"

编码

public static func decode(locationId: String) throws -> PointWithElevation

将locationId解码为纬度/经度(location是locationId单元格的近似中心,以合理的精度为准)。

示例

UnlCore.decode(locationId: "u173zwbt3");

返回一个带有高度的点对象。以下是返回对象的JSON表示形式:

{
   "coordinates":{
      "lat":52.376869,
      "lon":4.900653
   },
   "elevation":{
      "elevation":0,
      "elevationType":"floor"
   },
   "bounds":{
      "n":52.37689018249512,
      "e":4.900674819946289,
      "s":52.37684726715088,
      "w":4.900631904602051
   }
}

边界

返回指定locationId单元格的n、e、s、w纬度/经度边界,以及高度信息。

public static func bounds(locationId: String) throws -> Bounds

示例

UnlCore.bounds(locationId: "u173zwbt3");

返回一个带有高度的边界对象。以下是返回对象的JSON表示形式:

{
   "n":52.37689018249512,
   "e":4.900674819946289,
   "s":52.37684726715088,
   "w":4.900631904602051
}

网格线

返回可以用于在指定的n、e、s、w纬度/经度边界和精度处绘制UNL网格的垂直和水平线。每条线用两个坐标组成的数组表示:[[起始经度,起始纬度],[结束经度,结束纬度]]。

public static func gridLines(bounds: Bounds, precision: Int) throws -> [[[Double]]]

如果不传递精度参数,将使用默认精度:9。

示例

let bounds: Bounds = Bounds(n: 46.77227194246396, e: 23.59560827603795, s: 46.77210936378606, w: 23.595436614661565);
UnlCore.gridLines(bounds: bounds, precision: 12);

将返回一个长度为1481的ArrayList,包含线条数组。

[[startLon, startLat], [endLon, endLat]]
...

相邻

确定给定方向的相邻单元格:“N” | “S” | “E” | “W”。

public static func adjacent(locationId: String, direction: String) throws -> String

示例

UnlCore.adjacent(locationId: "ezzz@5", direction: "N");

返回一个字符串

"gbpb@5"

相邻单元格

返回指定locationId的所有8个相邻单元格。

public static func neighbours(locationId: String) throws -> Neighbours

示例

UnlCore.neighbours(locationId: "ezzz");

返回一个相邻对象,包含指定locationId的所有8个相邻单元格。以下是该对象的JSON表示形式:

{
   "n":"gbpb",
   "ne":"u000",
   "e":"spbp",
   "se":"spbn",
   "s":"ezzy",
   "sw":"ezzw",
   "w":"ezzx",
   "nw":"gbp8"
}

excludeElevation

返回一个包含locationId和海拔属性的LocationIdWithElevation实例。它主要用于内部函数。

public static func excludeElevation(locationIdWithElevation: String) throws -> LocationIdWithElevation

示例

UnlCore.excludeElevation(locationIdWithElevation: "6gkzwgjz@5");

返回一个LocationIdWithElevation对象。其JSON表示形式为

{
   "locationId":"6gkzwgjz",
   "elevation":{
      "elevation":5,
      "elevationType":"floor"
   }
}

appendElevation

将海拔字符和海拔值添加到locationId中。它主要用于内部函数。

public static func appendElevation(locationIdWithoutElevation: String, elevation: Elevation) throws -> String

示例

let elevation: Elevation = Elevation(elevation: 5, elevationType: "floor"):
UnlCore.appendElevation(locationIdWithoutElevation: "6gkzwgjz", elevation: elevation);

返回一个字符串

"6gkzwgjz@5"

toWords

需要提供用于访问位置API的API密钥。在成功的情况下,返回封装对应于位置字符串(id或经纬度坐标)的经度、海拔、边界、geohash和单词的位置对象。如果请求位置API失败,则调用onFailure回调。

public static func toWords(location: String, apiKey: String, onSuccess: @escaping (Location) -> (), onFailure: @escaping(Error) -> ())

words

需要提供用于访问位置API的API密钥。在成功的情况下,返回封装对应于单词字符串的经度、海拔、边界、geohash和单词的位置对象。如果请求位置API失败,则调用onFailure回调。

public static func words(words: String, apiKey: String, onSuccess: @escaping (Location) -> (), onFailure: @escaping(Error) -> ())

要生成apiKey并访问位置API,您需要在map.unl.global上创建一个开发者账户。您可以在https://developer.unl.global/docs/authentication上了解更多关于认证和API密钥的信息。

贡献

欢迎Pull请求。

请确保根据需要更新测试。

许可

遵循 Apache 许可证2.0版本 许可。