如何根据不同的id绘制google地图标记,Swift
How to draw a google map marker depending on different id, Swift
在我当前的项目中,我收到了 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"
}
]
现在我制作了一个模型 class,随后在必要时在不同的 ViewController 中声明了该模型类型的数组。
var nearByFetchedVehicles = [nearByVehicleModel]()
现在,正如您在响应中看到的,我们得到了车辆的 _id。当我遍历 nearByFetchedVehicles 数组时,我想为每个 _id[=31= 创建 google 地图标记(当然会使用一些车辆图像) ].当我们在五秒后再次调用该函数时,我想检查 _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
.
轻松存储以前的数据
两种方式:
- 每次从服务器获得响应时,使用
mapView.clear()
清除 mapView。然后再次绘制标记。
将_id作为用户数据分配给每个maker,如marker.userData = _id
。
将标记存储在一个数组中,并通过 markerArray 循环。
检查标记 _id 是否存在于来自服务器的数组中。如果不存在,请删除标记,如 marker.map = nil
。否则使用
更改标记位置
marker.position = CLLocationCoordinate2D.init(latitude: <#T##CLLocationDegrees#>, longitude: <#T##CLLocationDegrees#>)
如果标记中的 _id 与任何 标记的 userData 都不匹配,则创建一个新标记为:
let marker = GMSMarker.init(position: currentLocation)
marker.map = mapView
marker.userData = nearByFetchedVehicles[i]
希望对您有所帮助。
在我当前的项目中,我收到了 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"
}
]
现在我制作了一个模型 class,随后在必要时在不同的 ViewController 中声明了该模型类型的数组。
var nearByFetchedVehicles = [nearByVehicleModel]()
现在,正如您在响应中看到的,我们得到了车辆的 _id。当我遍历 nearByFetchedVehicles 数组时,我想为每个 _id[=31= 创建 google 地图标记(当然会使用一些车辆图像) ].当我们在五秒后再次调用该函数时,我想检查 _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
.
两种方式:
- 每次从服务器获得响应时,使用
mapView.clear()
清除 mapView。然后再次绘制标记。 将_id作为用户数据分配给每个maker,如
更改标记位置marker.userData = _id
。 将标记存储在一个数组中,并通过 markerArray 循环。 检查标记 _id 是否存在于来自服务器的数组中。如果不存在,请删除标记,如marker.map = nil
。否则使用marker.position = CLLocationCoordinate2D.init(latitude: <#T##CLLocationDegrees#>, longitude: <#T##CLLocationDegrees#>)
如果标记中的 _id 与任何 标记的 userData 都不匹配,则创建一个新标记为:
let marker = GMSMarker.init(position: currentLocation)
marker.map = mapView
marker.userData = nearByFetchedVehicles[i]
希望对您有所帮助。