Feature layer events

  • This documentation demonstrates handling click events on map boundaries, specifically usingGMSFeatureTypeLocality for 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.

Select platform:AndroidiOSJavaScript

This example shows map boundaries forGMSFeatureTypeLocality, and implements thedelegate function that styles the clickedpolygon. The result displays an info alert window with the event data.

A diagram showing a clicked boundary polygonmap

Important: Invisible features don't trigger click events. Polygons must have aminimal alpha value to make them tappable.

Swift

classSampleViewController:UIViewController{privatelazyvarmapView:GMSMapView=GMSMapView(frame:.zero,mapID:GMSMapID(identifier:"YOUR_MAP_ID"),camera:GMSCameraPosition(latitude:40,longitude:-80,zoom:12))overridefuncloadView(){view=mapViewletstyle=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{($0as?PlaceFeature)?.placeID}).joined(separator:", "),preferredStyle:.alert)present(toast,animated:true,completion:nil)}}

Objective-C

@interfaceSampleViewController:UIViewController<GMSMapViewDelegate>@end@implementationSampleViewController-(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];}

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-11-21 UTC.