将字符串转换为cllocationcoordinate2d swift的数组

将字符串转换为cllocationcoordinate2d swift的数组,swift,Swift,我使用的API返回一个包含坐标的字符串。如何转换此响应: “线路引脚”: "#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#3

我使用的API返回一个包含坐标的
字符串。如何转换此响应:

“线路引脚”: "#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.949494567#30.056645,30.9496883#30.0566167 30.9501883”

进入
CLLocationCoordinate2D的
数组
,使用
MapKit
绘制一条线,使用:

let polyLine = MKPolyline(coordinates: Locations , count: Locations.count)
busMapView.add(polyLine)

您可以通过组合
组件(由
map

  • 按字符
    #
    拆分字符串,并删除第一个(空)项
  • 通过用逗号拆分字符串,将每个项目映射到
    CLLocationCoordinate2D
    ,将字符串转换为
    CLLocationCoordinate2D
    ,并创建坐标

结果是
[CLLocationCoordinate2D]
让dic:Dictionary=[“线路针脚”:"#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 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)
将strLatLng=dic[“布线引脚”]设为!字符串 设ArrayFlatlng=strLatLng.组件(以“#”分隔) var testcoords=[CLLocationCoordinate2D]() 用于阵列中的latLngStr{ 如果将strLat=latLngStr.components(以“,”分隔)设为?[String],则strLat.count==2{ 追加(CLLocationCoordinate2D(纬度:CLLocationDegrees(strLat[0])!,经度:CLLocationDegrees(strLat[1])!) } } 打印(“testcoords\(testcoords.count)”) 设polyLine=MKPolyline(坐标:testcoords,计数:testcoords.count) busMapView.add(多段线)
API字符串使用两级解析进行解析,首先我们将API字符串拆分为位置字符串,然后使用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
        }
        }

这正是我所需要的。非常感谢您,瓦迪安先生,您刚刚救了我,非常感谢您
让latLng=pin.components(以“,”分隔)。compactMap(CLLocationDegrees.init)
尝试为您的代码添加一些上下文,以帮助未来的读者更好地理解其含义。
  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
        }
        }