Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift3 在swift 3.0中将坐标数组转换为geojson字符串_Swift3_Mapbox_Geojson - Fatal编程技术网

Swift3 在swift 3.0中将坐标数组转换为geojson字符串

Swift3 在swift 3.0中将坐标数组转换为geojson字符串,swift3,mapbox,geojson,Swift3,Mapbox,Geojson,是否有一种方法可以轻松地将坐标数组转换为geojson字符串,以便在mapbox ios map中轻松绘制 id: 1, lat: 37.54365964, lon: -122.36820328, date: 1483607009.98252 id: 2, lat: 37.63721702, lon: -122.43994642, date: 1483607387.95026 id: 3, lat: 37.64117709, lon: -122.44319877, date: 148360740

是否有一种方法可以轻松地将坐标数组转换为geojson字符串,以便在mapbox ios map中轻松绘制

id: 1, lat: 37.54365964, lon: -122.36820328, date: 1483607009.98252
id: 2, lat: 37.63721702, lon: -122.43994642, date: 1483607387.95026
id: 3, lat: 37.64117709, lon: -122.44319877, date: 1483607403.98835
id: 4, lat: 37.64425086, lon: -122.44768593, date: 1483607419.95671
id: 5, lat: 37.64803999, lon: -122.45107063, date: 1483607435.98718
id: 6, lat: 37.65210789, lon: -122.45382878, date: 1483607451.98202
id: 7, lat: 37.65538199, lon: -122.45796869, date: 1483607467.99542
id: 8, lat: 37.65874435, lon: -122.46226986, date: 1483607483.98868
id: 9, lat: 37.66271988, lon: -122.46545406, date: 1483607500.00689
id: 10, lat: 37.66725462, lon: -122.46555909, date: 1483607515.00965
id: 11, lat: 37.67171694, lon: -122.46680447, date: 1483607530.01121
id: 12, lat: 37.67577633, lon: -122.46992933, date: 1483607545.96997
id: 13, lat: 37.68035365, lon: -122.47161149, date: 1483607563.00284
id: 14, lat: 37.68506394, lon: -122.47132223, date: 1483607579.00677
id: 15, lat: 37.68971271, lon: -122.47030274, date: 1483607595.01726
id: 16, lat: 37.69433345, lon: -122.47028874, date: 1483607611.01769
id: 17, lat: 37.69887099, lon: -122.4712903, date: 1483607627.04018
id: 18, lat: 37.70344977, lon: -122.47131242, date: 1483607642.97182
id: 19, lat: 37.7078706, lon: -122.46905887, date: 1483607659.93988
id: 20, lat: 37.71039615, lon: -122.46428839, date: 1483607675.97476
以下是mapbox ios中绘制的geojson的示例图像:

下面是我如何将lat-lon放入数组的

            var coordinates = [Any]()

        for location in try db.prepare(locations) {
            print("id: \(location[id]), lat: \(location[lat]), lon: \(location[lon]), date: \(location[date])")

            var waypoints = [Any]()
            waypoints.append("\(location[lat])")
            waypoints.append("\(location[lon])")
            coordinates.append(waypoints)
        }
这是我想要实现的示例代码格式

    func convertLocationArrayToGeoJson(locations: [Any],_: ()) -> String {

    // convert the array of lat lon here to geojson string

    return "geojson string"
}
输出结果应如下所示:

{
"type": "FeatureCollection",
"features": [{
    "type": "Feature",
    "properties": {
        "name": "geojson-1"
    },
    "geometry": {
        "type": "LineString",
        "coordinates": [
            [
                longitude1-value,
                latitude-value1
            ],
            [
                longitude2-value,
                latitude2-value
            ]

        ]
    }
}]
}

首先,像这样更改
坐标
数组创建,并首先附加
long
,然后再附加
lat
,如您所需
JSON
格式

var coordinates = [Any]()
for location in try db.prepare(locations) {

    var waypoints = [Any]()
    waypoints.append("\(location[lon])")
    waypoints.append("\(location[lat])")
    coordinates.append(waypoints)
}
现在创建类似于本文json的字典

let jsonDic: [String: Any] = [
                                 "type": "FeatureCollection",
                                 "features": [
                                      [
                                          "type": "Feature",
                                          "properties": ["name": "geojson-1"],
                                          "geometry": [
                                               "type": "LineString",
                                               "coordinates": coordinate
                                          ]
                                      ]
                                  ]
                              ]
现在,如果想要
JSON
数据或字符串,可以使用
JSONSerialization.data(withJSONObject:options:

if let data = try? JSONSerialization.data(withJSONObject: jsonDic, options: .prettyPrinted) {
    //For JSON String
    let jsonStr = String(bytes: data, encoding: .utf8)
}

你能显示你的自定义类结构吗。@nirav-d,我已经用代码格式和预期结果更新了问题。你能帮忙吗?我说的是你需要向我们展示你是如何在数组中存储这个lat long值的?编辑你的问题,记住一件事永远不要在注释中添加代码,在你的问题中添加此代码。我已经添加了现在问题来了。@MarkAngeloHernandez欢迎伙伴:)