Fai in modo che le funzionalità dei dati rispondano agli eventi di tocco e utilizza l'evento per
visualizzare il valore di un attributo per la funzionalità toccata.
Gestire gli eventi del livello del set di dati
Questo esempio mostra le funzionalità dei dati per un set di dati con ID
YOUR_DATASET_ID e implementa la
funzione delegate per visualizzare il
valore di un attributo per la funzionalità selezionata.
In questo esempio, il set di dati mostra
i parchi di New York City.
Per ogni funzionalità del set di dati, corrispondente a un parco, il set di dati contiene un
attributo denominato acres contenente la superficie del parco. Per un parco
toccato, mostra il valore dell'attributo acres.
Swift
classSampleViewController:UIViewController{privatelazyvarmapView:GMSMapView=GMSMapView(frame:.zero,mapID:GMSMapID(identifier:"YOUR_MAP_ID"),camera:GMSCameraPosition(latitude:40.7,longitude:-74,zoom:12))// Set default styles.view=mapViewletstyle=FeatureStyle(fill:.green.withAlphaComponent(0.5),stroke:.green,strokeWidth:2)mapView.datasetFeatureLayer(of:"YOUR_DATASET_ID").style={_instyle}mapView.delegate=self}extensionSampleViewController:GMSMapViewDelegate{funcmapView(_mapView:GMSMapView,didTapfeatures:[Feature],infeatureLayer:FeatureLayer<Feature>,atLocation:CLLocationCoordinate2D){lettoast=UIAlertController(title:"Area of park",message:(features.compactMap{($0as?DatasetFeature)?.datasetAttributes["acres"]}).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:40.7longitude:-74zoom:12]];mapView.delegete=self;// Set default styles.GMSFeatureStyle*style=[GMSFeatureStylestyleWithFillColor:[[UIColorgreenColor]colorWithAlphaComponent:0.5]strokeColor:[UIColorgreenColor]strokeWidth:2.0];[_mapViewdatasetFeatureLayerOfDatasetID:@"YOUR_DATASET_ID"].style=^(GMSDatasetFeature*feature){returnstyle;};self.view=mapView;}-(void)mapView:(GMSMapView*)mapViewdidTapFeatures:(NSArray<id<GMSFeature>>*)featuresinFeatureLayer:(GMSFeatureLayer*)featureLayeratLocation:(CLLocationCoordinate2D)location{NSMutableArray<NSString*>*parkAreas=[NSMutableArrayarray];for(id<GMSFeature>featureinfeatures){if(![featureisKindOfClass:[GMSDatasetFeatureclass]]){continue;}NSString*nameDefinedInDataset=((GMSDatasetFeature*)feature).datasetAttributes[@"acres"];[parkAreasaddObject:nameDefinedInDataset];}UIAlertController*toast=[UIAlertControlleralertControllerWithTitle:@"Area of park"message:[parkAreascomponentsJoinedByString:@", "]preferredStyle:UIAlertControllerStyleAlert];[selfpresentViewController:toastanimated:YEScompletion:nil];}@end
[null,null,["Ultimo aggiornamento 2025-08-31 UTC."],[[["\u003cp\u003eLearn how to make data features on your map clickable and respond to tap events.\u003c/p\u003e\n"],["\u003cp\u003eUtilize tap events to reveal attribute information, such as displaying the 'acres' value for a park when tapped.\u003c/p\u003e\n"],["\u003cp\u003eThis functionality applies to dataset features, and the provided examples use a dataset of parks in New York City.\u003c/p\u003e\n"],["\u003cp\u003eInvisible features (polygons with insufficient alpha) will not trigger tap events.\u003c/p\u003e\n"],["\u003cp\u003eCode samples demonstrate how to implement this feature for Android, iOS, and JavaScript platforms.\u003c/p\u003e\n"]]],[],null,["Select platform: [Android](/maps/documentation/android-sdk/dds-datasets/make-data-features-clickable \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/dds-datasets/make-data-features-tappable \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/dds-datasets/make-data-features-clickable \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nMake data features respond to tap events, and use the event to\ndisplay the value of an attribute for the tapped feature.\n\n| **Important:** Invisible features don't trigger tap events. Polygons must have a minimal alpha value to make them tappable.\n\nHandle dataset layer events\n\nThis example shows data features for a dataset with ID\n\u003cvar translate=\"no\"\u003eYOUR_DATASET_ID\u003c/var\u003e, and implements the\n[delegate](/maps/documentation/ios-sdk/events) function to display the\nvalue of an attribute for the tapped feature.\n\nIn this example, the dataset shows\n[parks in New York City](https://data.cityofnewyork.us/Recreation/Parks-Properties/enfh-gkve).\nFor each dataset feature, corresponding to a park, the dataset contains an\nattribute named `acres` containing the surface area of the park. For a tapped\npark, display the value of the `acres` attribute. \n\nSwift \n\n```swift\nclass SampleViewController: UIViewController {\n\n private lazy var mapView: GMSMapView = GMSMapView(frame: .zero, mapID: GMSMapID(identifier: \"YOUR_MAP_ID\"), camera: GMSCameraPosition(latitude: 40.7, longitude: -74, zoom: 12))\n\n // Set default styles.\n view = mapView\n let style = FeatureStyle(fill: .green.withAlphaComponent(0.5), stroke: .green, strokeWidth: 2)\n mapView.datasetFeatureLayer(of: \"YOUR_DATASET_ID\").style = { _ in style }\n mapView.delegate = self\n}\n\nextension SampleViewController: GMSMapViewDelegate {\n func mapView(_ mapView: GMSMapView, didTap features: [Feature], in featureLayer: FeatureLayer\u003cFeature\u003e, atLocation: CLLocationCoordinate2D) {\n let toast = UIAlertController(title: \"Area of park\", message: (features.compactMap { ($0 as? DatasetFeature)?.datasetAttributes[\"acres\"] }).joined(separator: \", \"), preferredStyle: .alert)\n present(toast, animated: true, completion: nil)\n }\n}\n```\n\nObjective-C \n\n```objective-c\n@interface SampleViewController: UIViewController \u003cGMSMapViewDelegate\u003e\n@end\n\n@implementation SampleViewController\n- (void)loadView {\n GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero mapID:[GMSMapID mapIDWithIdentifier:@\"YOUR_MAP_ID\"] camera:[GMSCameraPosition cameraWithLatitude:40.7 longitude:-74 zoom:12]];\n mapView.delegete = self;\n\n // Set default styles.\n GMSFeatureStyle *style = [GMSFeatureStyle styleWithFillColor:[[UIColor greenColor] colorWithAlphaComponent:0.5] strokeColor:[UIColor greenColor] strokeWidth:2.0];\n [_mapView datasetFeatureLayerOfDatasetID:@\"YOUR_DATASET_ID\"].style = ^(GMSDatasetFeature *feature) { return style; };\n\n self.view = mapView;\n}\n\n- (void)mapView:(GMSMapView *)mapView didTapFeatures:(NSArray\u003cid\u003cGMSFeature\u003e\u003e *)features inFeatureLayer:(GMSFeatureLayer *)featureLayer atLocation:(CLLocationCoordinate2D)location {\n NSMutableArray\u003cNSString *\u003e *parkAreas = [NSMutableArray array];\n\n for (id\u003cGMSFeature\u003e feature in features) {\n if (![feature isKindOfClass:[GMSDatasetFeature class]]) { continue; }\n NSString *nameDefinedInDataset = ((GMSDatasetFeature *)feature).datasetAttributes[@\"acres\"];\n [parkAreas addObject:nameDefinedInDataset];\n }\n\n UIAlertController *toast = [UIAlertController alertControllerWithTitle:@\"Area of park\" message:[parkAreas componentsJoinedByString:@\", \"] preferredStyle:UIAlertControllerStyleAlert];\n [self presentViewController:toast animated:YES completion:nil];\n}\n@end\n```"]]