AI-generated Key Takeaways
-
Google Maps SDK for iOS allows you to monitor marker events like taps and gestures, displaying information like title and snippet when tapped.
-
You can enable marker dragging using a long press gesture by setting the
GMSMarker.draggableproperty. -
Marker visibility can be controlled based on the map's zoom level using the
GMSMapViewDelegateand setting theGMSMarker.mapproperty conditionally. -
To respond to marker events and access details like title and snippet, implement the
GMSMapViewDelegateprotocol and its corresponding callback methods.
When specific advanced marker properties are set, you can monitor marker events such as taps and gestures. If a marker is tapped, one can see additional information such as a marker title or snippet. One can also move draggable markers using a long press gesture.
- To track marker events, add the
GMSMapViewDelegateto yourview. - To make a marker draggable, set the
GMSMarker.draggableproperty. - To set descriptive text for a marker, use the
GMSMarker.titleproperty.
Respond to marker events
You can respond to marker events by adding
the GMSMapViewDelegate protocol to your view and
implementing the corresponding callback. This example identifies the title and
snippet for a selected marker.
Swift
// MARK: GMSMapViewDelegate func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool { if let title = marker.title { if let snippet = marker.snippet { print("marker title: \(title): snippet: \(snippet)") } } return true }
Objective-C
// MARK: GMSMapViewDelegate - (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker { if (marker.title && marker.snippet) { NSLog(@"marker with title:%@ snippet: %@", marker.title, marker.snippet) } return YES; }
Control marker visibility by map zoom level
To control the visibility of GMSMarker, implement the
GMSMapViewDelegate protocol and add a condition to set
GMSMarker.map.
Swift
// MARK: GMSMapViewDelegate func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) { marker.map = position.zoom >= 14 ? mapView : nil }
Objective-C
// MARK: GMSMapViewDelegate - (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position { marker.map = position.zoom >= 14 ? mapView : nil; }
Make a marker draggable
When you enable the draggable property users can drag markers on the map with
a long press gesture. To make a marker draggable, set the GMSMarker.draggable
property to true.
Swift
marker.draggable = true
Objective-C
marker.draggable = YES;