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

时间:2017-02-16 22:00:33

标签: 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的每个索引中使用String来生成MKPolygonObject,所以首先我需要将long String转换为4个CLLocationCoordinate2D对象并将这4个坐标对象放入每个项目的数组,然后使用数组索引为不同的区域生成多边形。

有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

这是我想出的。您可以根据自己的结构进行调整。

import Foundation

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

// Remove leading `(` and trailing `)`
let trimmedInput = String(input.characters.dropLast().dropFirst())

let coordStrings = trimmedInput.components(separatedBy: "),(")

let coords: [CLLocationCoordinate2D] = coordStrings.map{ coordsString in
    let coords = coordsString.components(separatedBy: ", ")
    precondition(coords.count == 2, "There should be exactly 2 coords.")
    guard let lat = Double(coords[0]),
          let long = Double(coords[1]) else {
        fatalError("One of the coords isn't a valid Double: \(coords)")
    }

    return CLLocationCoordinate2D(latitude: lat, longitude: long)
}

print(coords)

答案 1 :(得分:2)

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

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)
}