2017-02-16 49 views
0

我收到此JSON:轉換單個字符串座標成CLLocationCoordinate2D陣列和使用該陣列在MapView生成多邊形

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")...] 

我現在需要做的是使用String的rutaItemArray.coor每個索引內部產生MKPolygonObject,所以首先我需要的長字符串轉換成4 CLLocationCoordinate2D對象並將這4個座標對象放入數組中,然後使用數組索引爲不同區域生成多邊形。

有人可以幫我解決這個問題嗎?

+0

''「coor」'數組來自哪裏?它應該是一個JSON數組對象包含雙打,而不是像這樣的字符串 – Alexander

+0

它應該但它不,我不是Web服務的程序員,我只是iOS開發人員,我必須與我一起工作不幸的是。如果Android開發者能夠做到這一點,那麼在iOS中也是如此。 –

回答

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

我必須承認,在大多數情況下,我不是'components(separatedBy:)'的粉絲。如果Web服務開發人員決定在座標對''之間插入空格'('或者刪除x和y座標之間的空格,那麼您必須修改代碼。 –

+0

@Alexander我學到了很多答案,謝謝分享知識! –

+1

@MartinR雖然我認爲'NSScanner'也是一個選項。 – Alexander