Reputation: 605
So I'm learning and getting my head around swift/google maps API. However, no matter what I try, when I tap on the marker I cannot get the print statement to appear in the console.
Tapping the marker displays the marker Name, however it never prints. I've tried a few variations of the private func found online, but maybe I'm missing something more fundamental here...
import UIKit
import GoogleMaps
class ViewController: UIViewController, GMSMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
struct MarkerStruct {
let name: String
let lat: CLLocationDegrees
let long: CLLocationDegrees
}
let markers = [
MarkerStruct(name: "Food Hut 1", lat: 52.649030, long: 1.174155),
MarkerStruct(name: "Foot Hut 2", lat: 52.649030, long: 1.174185),
]
let camera = GMSCameraPosition.camera(withLatitude: 52.649030, longitude: 1.174155, zoom: 14)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.delegate = self
self.view = mapView
for marker in markers {
let position = CLLocationCoordinate2D(latitude: marker.lat, longitude: marker.long)
let locationmarker = GMSMarker(position: position)
locationmarker.title = marker.name
locationmarker.map = mapView
}
}
private func mapView(mapView: GMSMapView, didTapMarker marker: GMSMarker) {
print("Marker tapped")
}
}
Upvotes: 4
Views: 3416
Reputation: 1690
I'm writing an answer for Google Maps SDK for iOS version 9.0 and advanced markers.
Advanced markers are pretty new as of this writing. Eventually, regular markers in the Google Maps SDK for iOS will be deprecated (according to Google) and the use of advanced markers will be the norm. Official documentation here: https://developers.google.com/maps/documentation/ios-sdk/advanced-markers/add-marker
The problem I was running into was that after creating advanced markers on my mapView (GMSSMapView) they were not responding to the didTap delegate event:
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool { }
I know this because I set a breakpoint on the first line of code within the didTap event and it was never hit. However, I did notice that when an advanced marker was tapped, the marker triggered the camera position to shift so that the marker was centered in the mapView. So I knew didTap was responding to an internal event.
In order to run custom code in the didTap event my solution was to create an extension of the GMSMap delegate and add the didTap event within. See here:
extension VCMap: GMSMapViewDelegate {
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
let x = 0 // breakpoint was set here
return true
} }
Note: In case you aren't familiar with extensions in Swift, refer to this link: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/extensions/
Then I set the delegate property of the mapView = self.
Now when clicking an advanced marker, the breakpoint was hit. So to recap, within the delegate Extension, include the didTap event and place your custom code within. Hope this helps anyone else looking to im plement advanced markers!
Upvotes: 0
Reputation: 20804
You are using an old method name and you also need remove the private
keyword, that is why your delegate method is not called
now is
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
print("Marker tapped")
return true
}
working now
Upvotes: 5