在我当前的项目中,我从API得到如下响应:

[
{
  "current_location" : [
    90.458456400000003,
    23.746056500000002
  ],
  "vehicle_type" : "BIKE",
  "_id" : "5a8fd7e50ed19875687dcf8c"
},
{
  "current_location" : [
    90.3727272,
    23.8216228
  ],
  "vehicle_type" : "BIKE",
  "_id" : "5a97f48edf192f6e54725c78"
},
{
  "current_location" : [
    90.397113300000001,
    23.778111200000001
  ],
  "vehicle_type" : "BIKE",
  "_id" : "5a980243df192f6e54725c85"
},
{
  "current_location" : [
    90.379659399999994,
    23.7221121
  ],
  "vehicle_type" : "BIKE",
  "_id" : "5a9a66586f27706a7a10783a"
},
{
  "current_location" : [
    90.400972899999999,
    23.872647400000002
  ],
  "vehicle_type" : "BIKE",
  "_id" : "5a9fff2e31eb895bc79f7ee0"
 }
]

现在我创建了一个模型类,后来在不同的viewController中声明了该模型类型的数组(如果需要)。
var nearByFetchedVehicles = [nearByVehicleModel]()

现在,正如您在响应中看到的,我们得到了车辆的ID。当我循环访问NearbyFetchedVehicles数组时,我想为每个ID创建谷歌地图标记(当然会使用一些车辆图像)。当我们在说5秒钟后再次调用该函数时,我想检查该ID是否存在,如果存在,那么我将检查纬度和经度并移动相应的标记,我已经介绍了移动功能。此函数将每五秒钟调用一次,因此它需要不断检查哪个ID已消失,哪个ID是新的。如果之前存在的任何标识消失,则将删除该标识;如果新标识可用,则将创建新标识。我需要帮助创建标记并删除与_id有关的标记。
 if let fetchedVehicle = result.value {

            if fetchedVehicle.isEmpty {
                print("No vehicle nearby")
            }

            self.nearByFetchedVehicles = fetchedVehicle
            print("nearByFetchedVehicles are:\(self.nearByFetchedVehicles)")
            self.mapView?.clear()

            for i in self.nearByFetchedVehicles  {

                // Need to create or delete marker here

                self.nearbyVehicleMovement(lat: i.lat!, lon: i.lon!, vehicleType: i.vehicleType!, vehicleID: i.vehicleID!)
            }

        }

最佳答案

我建议使用集合而不是数组。它有gosubstracting方法,可以很容易地处理您的数据。

//: Playground - noun: a place where people can play

import UIKit

var previousStr = """
[
{
"current_location" : [
90.458456400000003,
23.746056500000002
],
"vehicle_type" : "BIKE",
"_id" : "5a8fd7e50ed19875687dcf8c"
},
{
"current_location" : [
90.3727272,
23.8216228
],
"vehicle_type" : "BIKE",
"_id" : "5a97f48edf192f6e54725c78"
},
{
"current_location" : [
90.397113300000001,
23.778111200000001
],
"vehicle_type" : "BIKE",
"_id" : "5a980243df192f6e54725c85"
},
{
"current_location" : [
90.379659399999994,
23.7221121
],
"vehicle_type" : "BIKE",
"_id" : "5a9a66586f27706a7a10783a"
},
{
"current_location" : [
90.400972899999999,
23.872647400000002
],
"vehicle_type" : "BIKE",
"_id" : "5a9fff2e31eb895bc79f7ee0"
}
]
"""

var str = """
[
{
"current_location" : [
90.458456400000003,
23.746056500000002
],
"vehicle_type" : "BIKE",
"_id" : "5a8fd7e50ed19875687dcf8c"
},
{
"current_location" : [
90.3727272,
23.8216228
],
"vehicle_type" : "BIKE",
"_id" : "5a97f48edf192f6e54725c78"
},
{
"current_location" : [
90.397113300000001,
23.778111200000001
],
"vehicle_type" : "BIKE",
"_id" : "5a980243df192f6e54725c85"
},
{
"current_location" : [
90.379659399999994,
23.7221121
],
"vehicle_type" : "BIKE",
"_id" : "5a9a66586f27706a7a10783a"
},
{
"current_location" : [
90.400972899999999,
23.872647400000002
],
"vehicle_type" : "BIKE",
"_id" : "5a9fff2e31eb895bc79f7ee0"
}
]
"""

struct Vehicle: Decodable, Hashable {
    let current_location: [Double]
    let vehicle_type: String
    let _id: String

    var hashValue: Int {
        return _id.hashValue
    }
}
let array: [Vehicle] = try! JSONDecoder().decode([Vehicle].self, from: str.data(using: .utf8)!)
let set = Set<Vehicle>(array)

let previousArray: [Vehicle] = try! JSONDecoder().decode([Vehicle].self, from: previousStr.data(using: .utf8)!)
let previousSet = Set<Vehicle>(previousArray)

print(previousSet)

let added = set.subtracting(previousSet)
let lost = previousSet.subtracting(set)

print(added)
print(lost)

得出以下结论:
[\ulldb_expr_63.车辆(当前位置:[90.3727272,23.8216228],
车辆类型:“自行车”,ID:“5A97F48EDF192F6E54725C78”),
_ lldb_expr_63.车辆(当前_位置:【90.397113300000001,23.778111200000001】,车辆_类型:“自行车”,_ID:“5A980243DF192F6E54725C85”),ulldb_expr_63.车辆(当前_位置:
[90.37965939999994,23.7221121],车辆类型:“自行车”,识别号:
“5A9A66586F27706A7A10783A”),uuldb_expr_63.车辆(当前位置:
[90.400972899999999,23.87264740000002],车辆类型:“自行车”,识别号:
“5A9fff2e31eb895bc79f7ee0”),uulldb_expr_63.车辆(当前位置:
[90.458456400000003,23.746056500000002],车辆类型:“自行车”,识别号:
“5A8FD7E50ED19875687DCF8C”)]已添加>>>>>>[uulldb_expr_63.车辆(当前位置:
[90.400972899999999,23.87264740000002],车辆类型:“自行车”,识别号:
“5A9fff2e31eb895bc79f7ee1”)]丢失>>>>[\ulldb\u expr\u 63.车辆(当前位置:
[90.400972899999999,23.87264740000002],车辆类型:“自行车”,识别号:
“5A9fff2e31eb895bc79f7ee0”)]
在您的情况下,您可以使用let previousSet = set轻松存储以前的数据。

关于swift - 如何根据不同的身份,Swift绘制谷歌 map 标记,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50285589/

10-11 00:45
查看更多