swiftui - 如何使用 SwiftUI 在 GoogleMaps 中创建多个标记?
问题描述
我需要在我的 MapView 中添加多个标记。如何使用 swiftui 在同一个地图视图中添加多个标记?
这是我的代码:
import SwiftUI
import UIKit
import GoogleMaps
struct MapView: UIViewRepresentable {
let coordinate: CLLocationCoordinate2D?
let marker : GMSMarker = GMSMarker()
func makeUIView(context: Self.Context) -> GMSMapView {
let camera = GMSCameraPosition.camera(withLatitude: coordinate.latitude, longitude: coordinate.longitude, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
return mapView
}
func updateUIView(_ mapView: GMSMapView, context: Self.Context) {
marker.position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
marker.title = "XYZ"
marker.snippet = "ABCD"
marker.map = mapView
}
}
解决方案
这是我的样本,
struct MapView: UIViewRepresentable {
let coordinate: CLLocationCoordinate2D
let cities = [
[
"name": "Yangon",
"lat": 16.8409,
"long": 96.1735
],
[
"name": "Mandalay",
"lat": 21.9588,
"long": 96.0891
]
]
func makeUIView(context: Self.Context) -> GMSMapView {
let camera = GMSCameraPosition.camera(withLatitude: coordinate.latitude, longitude: coordinate.longitude, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
return mapView
}
func updateUIView(_ mapView: GMSMapView, context: Self.Context) {
for city in cities {
let marker : GMSMarker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: city["lat"] as! CLLocationDegrees, longitude: city["long"] as! CLLocationDegrees)
marker.title = city["name"] as? String
marker.snippet = "Welcome to \(city["name"] as! String)"
marker.map = mapView
}
}
}
推荐阅读
- pylint - pylint 不能检测到未解析的属性引用吗?
- c++ - 我应该如何格式化 getline 以不跳到代码的末尾?
- swift - Swift / SwiftUI:如何检查环境 /binding var 是否为空字符串(.isEmpty 出现构建错误)
- regex - REXEX 匹配一个字符串并选择最多一个字符
- node.js - 显然“npm install -g”不能作为全局库工作
- c++ - AWS C++ Route53 Unknown(100) 错误
- php - 如何从我的商店使用我的本地 bd sql 加入 bd shopify
- ios - SwiftUI ActionSheet 禁用按钮
- ssl - 什么是 OCSP 签名证书和密钥?谁应该发行它?
- python - 如何从另一个数据框创建一个数据框,每个值列只有最后一个非负值?