在我当前的项目中,我从 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 创建谷歌地图标记(当然会使用一些车辆图像)。当我们在五秒后再次调用该函数时,我想检查 _id 是否存在,如果存在则我将检查纬度和经度并相应地移动标记,我有移动功能已经覆盖了。此函数将每五秒调用一次,因此它需要不断检查哪个 _id 已消失以及哪个 _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!)
}
}
最佳答案
我建议使用 Set 而不是 Array。它具有 go substracting
方法,可以轻松处理您的数据。
//: 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)
产生这个:
[__lldb_expr_63.Vehicle(current_location: [90.3727272, 23.8216228], vehicle_type: "BIKE", _id: "5a97f48edf192f6e54725c78"), __lldb_expr_63.Vehicle(current_location: [90.397113300000001, 23.778111200000001], vehicle_type: "BIKE", _id: "5a980243df192f6e54725c85"), __lldb_expr_63.Vehicle(current_location: [90.379659399999994, 23.7221121], vehicle_type: "BIKE", _id: "5a9a66586f27706a7a10783a"), __lldb_expr_63.Vehicle(current_location: [90.400972899999999, 23.872647400000002], vehicle_type: "BIKE", _id: "5a9fff2e31eb895bc79f7ee0"), __lldb_expr_63.Vehicle(current_location: [90.458456400000003, 23.746056500000002], vehicle_type: "BIKE", _id: "5a8fd7e50ed19875687dcf8c")] added>>>>> [__lldb_expr_63.Vehicle(current_location: [90.400972899999999, 23.872647400000002], vehicle_type: "BIKE", _id: "5a9fff2e31eb895bc79f7ee1")] lost>>>> [__lldb_expr_63.Vehicle(current_location: [90.400972899999999, 23.872647400000002], vehicle_type: "BIKE", _id: "5a9fff2e31eb895bc79f7ee0")]
在您的情况下,您可以使用 let previousSet = set
轻松存储以前的数据。
关于swift - 如何根据不同的 ID、Swift 绘制谷歌地图标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50285589/