Feature layer events
Stay organized with collections
Save and categorize content based on your preferences.
Page Summary
-
This documentation demonstrates handling click events on map boundaries, specifically using
GMSFeatureTypeLocalityfor the example. -
When a boundary is clicked, an info window appears displaying event data, such as the place IDs of the clicked features.
-
It's crucial to note that map boundaries need a minimum alpha value for click events to register, ensuring they are tappable.
-
Sample code snippets in Swift and Objective-C are provided for implementation guidance on setting up and responding to click events on map boundaries.
This example shows map boundaries for
GMSFeatureTypeLocality, and implements the
delegate function that styles the clicked
polygon. The result displays an info alert window with the event data.
A diagram showing a clicked boundary polygon map
Swift
classSampleViewController:UIViewController{ privatelazyvarmapView:GMSMapView=GMSMapView(frame:.zero,mapID:GMSMapID(identifier:"YOUR_MAP_ID"),camera:GMSCameraPosition(latitude:40,longitude:-80,zoom:12)) overridefuncloadView(){ view=mapView letstyle=FeatureStyle(fill:.orange.withAlphaComponent(0.5),stroke:.orange,strokeWidth:2) mapView.featureLayer(of:.locality).style={_instyle} mapView.delegate=self } } extensionSampleViewController:GMSMapViewDelegate{ funcmapView(_mapView:GMSMapView,didTapfeatures:[Feature],infeatureLayer:FeatureLayer<Feature>,atLocation:CLLocationCoordinate2D){ lettoast=UIAlertController(title:"Clicked places",message:(features.compactMap{(0ドルas?PlaceFeature)?.placeID}).joined(separator:", "),preferredStyle:.alert) present(toast,animated:true,completion:nil) } }
Objective-C
@interface SampleViewController: UIViewController<GMSMapViewDelegate> @end @implementation SampleViewController - (void)loadView{ GMSMapView*mapView=[GMSMapViewmapWithFrame:CGRectZeromapID:[GMSMapIDmapIDWithIdentifier:@"YOUR_MAP_ID"]camera:[GMSCameraPositioncameraWithLatitude:40longitude:-80zoom:12]]; mapView.delegete=self; GMSFeatureStyle*style=[GMSFeatureStylestyleWithFillColor:[[UIColororangeColor]colorWithAlphaComponent:0.5]strokeColor:[UIColororangeColor]strokeWidth:2.0]; [mapViewfeatureLayerOfFeatureType:GMSFeatureTypeLocality].style=^(GMSPlaceFeature*feature){returnstyle;}; self.view=mapView; } - (void)mapView:(GMSMapView*)mapViewdidTapFeatures:(NSArray<id<GMSFeature>>*)featuresinFeatureLayer:(GMSFeatureLayer*)featureLayeratLocation:(CLLocationCoordinate2D)location{ NSMutableArray<NSString*>*places=[NSMutableArrayarray]; for(id<GMSFeature>featureinfeatures){ if(![featureisKindOfClass:[GMSPlaceFeatureclass]]){continue;} NSString*placeID=((GMSPlaceFeature*)feature).placeID; [placesaddObject:placeID]; } UIAlertController*toast=[UIAlertControlleralertControllerWithTitle:@"Clicked places"message:[placescomponentsJoinedByString:@", "]preferredStyle:UIAlertControllerStyleAlert]; [selfpresentViewController:toastanimated:YEScompletion:nil]; }