我正在使用一个API,该API返回包含坐标的String
。如何转换此响应:
“ route_pins”: “#30.0539983,30.943465#30.0539033,30.9434167#30.05379,30.9434467#30.0536117,30.943865#30.0535133,30.9439867#30.0534633,30.9440967#30.05353,30.94428#30.053675,30.944525#30.0539933,30.9449667#30.0541517,30.9455455.34545 30.0544667,30.945725#30.05471,30.9460733#30.0548667,30.94631#30.0550417,30.9465683#30.0553733,30.9471133#30.0557133,30.9476383#30.0558667,30.947905133#30.0560083,30.9481767#30.0562517,30.94872#30.0564917,30.94929673494365 30.9501883“
插入Array
中的CLLocationCoordinate2D
中,以使用MapKit
绘制一条线,
let polyLine = MKPolyline(coordinates: Locations , count: Locations.count)
busMapView.add(polyLine)
答案 0 :(得分:2)
您可以结合使用components(separatedBy
和map
#
分割字符串,并删除第一个(空)项。CLLocationCoordinate2D
,将字符串转换为CLLocationCoordinate2D
并创建坐标。let routePins = "#30.0539983,30.943465#30.0539033,30.9434167#30.05379,30.9434467#30.0536117,30.943865#30.0535133,30.9439867#30.0534633,30.9440967#30.05353,30.94428#30.053675,30.944525#30.0539933,30.9449667#30.0541517,30.9452317#30.0542917,30.9454717#30.054365,30.9455717#30.0544667,30.945725#30.05471,30.9460733#30.0548667,30.94631#30.0550417,30.9465683#30.0553733,30.9471133#30.0557133,30.9476383#30.0558667,30.947905#30.0560083,30.9481767#30.0562517,30.94872#30.0564917,30.9492217#30.0565783,30.9494567#30.056645,30.9496883#30.0566167,30.9501883"
let coordinates = routePins.components(separatedBy: "#").dropFirst().map { (pin) -> CLLocationCoordinate2D in
let latLng = pin.components(separatedBy: ",").map{ CLLocationDegrees($0)! }
return CLLocationCoordinate2D(latitude: latLng[0], longitude: latLng[1])
}
结果为[CLLocationCoordinate2D]
。
答案 1 :(得分:0)
let dic:Dictionary<String,Any> = ["route_pins": "#30.0539983,30.943465#30.0539033,30.9434167#30.05379,30.9434467#30.0536117,30.943865#30.0535133,30.9439867#30.0534633,30.9440967#30.05353,30.94428#30.053675,30.944525#30.0539933,30.9449667#30.0541517,30.9452317#30.0542917,30.9454717#30.054365,30.9455717#30.0544667,30.945725#30.05471,30.9460733#30.0548667,30.94631#30.0550417,30.9465683#30.0553733,30.9471133#30.0557133,30.9476383#30.0558667,30.947905#30.0560083,30.9481767#30.0562517,30.94872#30.0564917,30.9492217#30.0565783,30.9494567#30.056645,30.9496883#30.0566167,30.9501883"]
let strLatLng = dic["route_pins"] as! String
let arrayOflatLng = strLatLng.components(separatedBy: "#")
var testcoords = [CLLocationCoordinate2D]()
for latLngStr in arrayOflatLng {
if let strLat = latLngStr.components(separatedBy: ",") as? [String], strLat.count == 2 {
testcoords.append(CLLocationCoordinate2D(latitude: CLLocationDegrees(strLat[0])!, longitude: CLLocationDegrees(strLat[1])!))
}
}
print("testcoords \(testcoords.count)")
let polyLine = MKPolyline(coordinates: testcoords , count: testcoords.count)
busMapView.add(polyLine)
答案 2 :(得分:0)
使用2个层次的解析来解析API字符串,首先我们将API字符串拆分为location字符串,然后使用for循环将每个位置拆分为其坐标,并将坐标保存到CLLocationCoordinate2D并将其附加到结果数组中
let routePins = "#30.0539983,30.943465#30.0539033,30.9434167#30.05379,30.9434467#30.0536117,30.943865#30.0535133,30.9439867#30.0534633,30.9440967#30.05353,30.94428#30.053675,30.944525#30.0539933,30.9449667#30.0541517,30.9452317#30.0542917,30.9454717#30.054365,30.9455717#30.0544667,30.945725#30.05471,30.9460733#30.0548667,30.94631#30.0550417,30.9465683#30.0553733,30.9471133#30.0557133,30.9476383#30.0558667,30.947905#30.0560083,30.9481767#30.0562517,30.94872#30.0564917,30.9492217#30.0565783,30.9494567#30.056645,30.9496883#30.0566167,30.9501883"
let pins = routePins.components(separatedBy: "#")//first step is splitting the fetched array to pins array
var LocationsArray = [CLLocationCoordinate2D]()//this is the result array
for location in pins {
if(location.contains(",")){//checking that the location is containing lat ,long separtor
let coordinates = location.components(separatedBy: ",")//splitting the location to lat ,long
//safe parsing the string value to double vale for coordinates
let lat = Double(coordinates[0]) ?? 0.0
let long = Double( coordinates[1]) ?? 0.0
LocationsArray.append(CLLocationCoordinate2D(latitude: lat
,longitude: long))// appending new coordinates to locations array
}
}