To detect a tap on a marker on GoogleMaps I should use this function.
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
print("Do what ever you want.")
return true
}
This works great. But I show POI dynamically. How can I print the title of the POI which is pressed?
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()
}
...
}
I tried it like this but it doesn't work. I guess combining both functions in one is a logical mistake, because of the bool which expects to be true?
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
}