要檢測對 GoogleMaps 上標記的點擊,我應該使用此功能。
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
print("Do what ever you want.")
return true
}
這很好用。但我動態顯示 POI。如何列印被按下的興趣點的標題?
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
...
POIManager.testVariable.forEach {
let marker1 = GMSMarker()
marker1.position = CLLocationCoordinate2D(latitude: $0.geometry.location.lat, longitude: $0.geometry.location.lng)
marker1.title = $0.name
marker1.map = mapView
locationManager.stopUpdatingLocation()
}
...
}
我像這樣嘗試過,但它不起作用。我想將這兩個函式合二為一是一個邏輯錯誤,因為 bool 期望為真?
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition, didTap marker: GMSMarker) -> Bool {
...
POIManager.testVariable.forEach {
let marker1 = GMSMarker()
marker1.position = CLLocationCoordinate2D(latitude: $0.geometry.location.lat, longitude: $0.geometry.location.lng)
marker1.title = $0.name
print($0.name)
marker1.map = mapView
locationManager.stopUpdatingLocation()
}
...
return true
}
uj5u.com熱心網友回復:
您應該考慮利用該GMSMarker userInfo物業。您將其分配給一個字典,可能包含一個易于參考的屬性 (id),您可以查詢該屬性以獲取資料。
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
let data = marker.userData as? [String:String]
print(data?["id"]) // get your array object with this id
return true
}
uj5u.com熱心網友回復:
我用這個解決了它。
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
print(marker.title)
return true
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/473390.html
