swift - 将带有坐标的单个字符串转换为 CLLocationCoordinate2D 数组,并使用该数组在 mapView 中生成多边形

标签 swift string mapkit cllocationcoordinate2d mkpolygon

我收到这个 JSON:

JSON: {
  "status_code" : 200,
  "status" : "ok",
  "data" : [
    {
      "zona" : "Narvarte",
      "hora" : "",
      "id_zona" : 1423,
      "proxdia" : "Lunes 20 de Febrero, 2017",
      "coor" : "(19.452187074041884, -99.1457748413086),(19.443769985032485, -99.14852142333984),(19.443446242121073, -99.13787841796875),(19.450244707639662, -99.13822174072266)",
      "dias" : "Lunes"
    }, ...]

我存储在这个结构中:

struct RutaItem {
var idZona: Int
var dias: String
var proxDia: String
var hora: String
var coor: String
var zona: String
}

然后我创建了一个 [RutaItem] 数组,用于存储结构

var rutaItemArray = [RutaItem]()

数据存储后,rutaItemArray 中的结构如下所示:

[pixan.RutaItem(idZona: 1423, dias: "Lunes", proxDia: "Lunes 20 de Febrero, 2017", hora: "", coor: "(19.452187074041884, -99.1457748413086),(19.443769985032485, -99.14852142333984),(19.443446242121073, -99.13787841796875),(19.450244707639662, -99.13822174072266)", zona: "Narvarte")...]

我现在需要做的是使用 rutaItemArray.coor 的每个索引中的字符串生成一个 MKPolygonObject,所以首先我需要将长字符串转换为 4 个 CLLocationCoordinate2D 对象并将它们放入每个项目在一个数组中放置 4 个坐标对象,然后使用数组索引生成不同区域的多边形。

谁能帮我解决这个问题?

最佳答案

您可以使用正则表达式模式匹配。 内联解释:

let coordString = "(19.452187074041884, -99.1457748413086), (19.443769985032485, -99.14852142333984),(19.443446242121073, -99.13787841796875),(19.450244707639662, -99.13822174072266)"

// Regular expression pattern for "( ... , ... )"
let pattern = "\\((.+?),(.+?)\\)"
let regex = try! NSRegularExpression(pattern: pattern)

// We need an NSString, compare http://stackoverflow.com/a/27880748/1187415
let nsString = coordString as NSString

// Enumerate all matches and create an array: 
let coords = regex.matches(in: coordString, range: NSRange(location: 0, length: nsString.length))
    .flatMap { match -> CLLocationCoordinate2D? in
        // This closure is called for each match.

        // Extract x and y coordinate from match, remove leading and trailing whitespace:
        let xString = nsString.substring(with: match.rangeAt(1)).trimmingCharacters(in: .whitespaces)
        let yString = nsString.substring(with: match.rangeAt(2)).trimmingCharacters(in: .whitespaces)

        // Convert to floating point numbers, skip invalid entries:
        guard let x = Double(xString), let y = Double(yString) else { return nil }

        // Return CLLocationCoordinate2D:
        return CLLocationCoordinate2D(latitude: x, longitude: y)
}

关于swift - 将带有坐标的单个字符串转换为 CLLocationCoordinate2D 数组,并使用该数组在 mapView 中生成多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42285506/

相关文章:

ios - map 套件的用户体验

网络连接较差时iOS远程通知角标(Badge)计数策略

iOS Swift & Parse - getDataInBackground 不工作?

java - 当 TextField 中的字符串超过 5 个字符时启用按钮

java - getNodeValue() 截断 org.w3c.dom.Node 中的属性内容

ios - 在 iOS MapKit 中通过经纬度坐标判断国家

swift - Swift 中的 MacOS 菜单

swift - 如果 .type() 方法不起作用,如何更好地处理 XCUIApplication.keyboards.keys[String].tap() ?

java - IsNan() 方法可用性

ios - MKPolyline 未在 map 上绘制。我该如何解决这个问题?