Khiến các đối tượng dữ liệu phản hồi các sự kiện nhấn và sử dụng sự kiện này để hiển thị giá trị của một thuộc tính cho đối tượng được nhấn.
Xử lý các sự kiện lớp tập dữ liệu
Ví dụ này cho thấy các đối tượng dữ liệu cho một tập dữ liệu có mã nhận dạng YOUR_DATASET_ID và triển khai hàm delegate để hiển thị giá trị của một thuộc tính cho đối tượng được nhấn.
Trong ví dụ này, tập dữ liệu cho thấy các công viên ở Thành phố New York.
Đối với mỗi đối tượng tập dữ liệu, tương ứng với một công viên, tập dữ liệu chứa một thuộc tính có tên là acres chứa diện tích bề mặt của công viên. Đối với một công viên được nhấn, hãy hiển thị giá trị của thuộc tính 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,["Cập nhật lần gần đây nhất: 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```"]]