2017-02-16 68 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