Google Maps SDK热图不会在数组追加时更新



编辑:查看我在解决方案中的评论,看看我在updateUIView中更改了什么

我正在尝试通过按下按钮来更新我在地图上的热图。

屏幕是这样的:

屏幕和交互的GIF图

我将Map使用的数组设置为@Binding变量,数组本身初始化为@State变量。原来的预设值(如下面的代码所示)出现在地图上,但修改后的坐标没有。

import SwiftUI
import GoogleMaps
import GoogleMapsUtils
struct GoogleMapsViewContainer: View {
@ObservedObject var locationManager = LocationManager()
@State var heatmapWeightedData: [GMUWeightedLatLng] = [
GMUWeightedLatLng(coordinate: CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20), intensity: 1),
GMUWeightedLatLng(coordinate: CLLocationCoordinate2D(latitude: -33.8623, longitude: 151.2003), intensity: 3),
GMUWeightedLatLng(coordinate: CLLocationCoordinate2D(latitude: -33.8635, longitude: 151.2010), intensity: 3),
GMUWeightedLatLng(coordinate: CLLocationCoordinate2D(latitude: -33.8587, longitude: 151.1970), intensity: 3),
GMUWeightedLatLng(coordinate: CLLocationCoordinate2D(latitude: -33.8579, longitude: 151.1987), intensity: 5)
]
@Environment(.dismiss) var dismiss
var body: some View {
VStack {
HStack {
Spacer()
Button("Done") {
dismiss()
}
.padding(.top, 45.0)
}
.padding(.trailing, 30.0)
ZStack {
GoogleMapsView(heatmapWeightedData: $heatmapWeightedData)
VStack {
Spacer()
Button(action: addTrashLocation) {
Text("Mark Trash in My Area")
}
.padding()
.overlay(
RoundedRectangle(cornerRadius: 30)
.stroke(/@START_MENU_TOKEN@/Color(hue: 0.373, saturation: 0.717, brightness: 0.466)/@END_MENU_TOKEN@/, lineWidth: 2)
)
.background(.white)
.cornerRadius(30)
.padding(.bottom, 50.0)
}
}
}
.padding(.top, -30)
.ignoresSafeArea()
}
func addTrashLocation() {
self.heatmapWeightedData.append(GMUWeightedLatLng(coordinate: CLLocationCoordinate2D(latitude: -33.8606, longitude: 151.2011), intensity: 5))
}
}

下面是上面引用的GoogleMapsView函数。

import SwiftUI
import GoogleMaps
import GoogleMapsUtils
struct GoogleMapsView: UIViewRepresentable {
@ObservedObject var locationManager = LocationManager()
var marker: GMSMarker = GMSMarker()
@Binding var heatmapWeightedData: [GMUWeightedLatLng]
func makeUIView(context: Context) -> GMSMapView {
let camera = GMSCameraPosition.camera(withLatitude: locationManager.latitude, longitude: locationManager.longitude, zoom: 15)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
let heatmapLayer = GMUHeatmapTileLayer()
heatmapLayer.radius = 150
heatmapLayer.weightedData = heatmapWeightedData
heatmapLayer.map = mapView
return mapView
}
func updateUIView(_ mapView: GMSMapView, context: UIViewRepresentableContext<GoogleMapsView>) {
marker.position = CLLocationCoordinate2D(latitude: locationManager.latitude, longitude: locationManager.longitude)
marker.title = "My location"
marker.map = mapView
let heatmapLayer = GMUHeatmapTileLayer()
heatmapLayer.radius = 150
heatmapLayer.weightedData = heatmapWeightedData
heatmapLayer.map = mapView
mapView.animate(toLocation: CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20))
}
}

为了能够在SwiftUI中使用谷歌地图SDK,我经历了很多麻烦与uiviewrepresable和AI混淆,因为互联网无法帮助我的答案。这种情况导致了我的许多问题,我不会感到惊讶,如果这是不知何故不工作,由于一些细微差别在uiviewrepresable我不知道。

如何让地图更新位置?

您应该使用@StateObject来初始化ObservableObjects

@StateObject var locationManager = LocationManager()

然后用@ObserveObject@EnvironmentObject传递。

@ObservedObject var locationManager : LocationManager

https://developer.apple.com/documentation/swiftui/managing-model-data-in-your-app

每次调用LocationManager()都在创建一个不同的实例。一个不知道另一个。

最新更新