टैप इवेंट को मैनेज करना

प्लैटफ़ॉर्म चुनें: Android iOS JavaScript

डेटा सुविधाओं को इस तरह सेट करें कि वे टैप करने पर प्रतिक्रिया दें. टैप करते ही सिस्टम उसे पहचान लेता है और सुविधा से जुड़ी किसी एट्रिब्यूट की वैल्यू दिखा देता है.

टैप करते ही स्टाइलिंग बदलें.

डेटासेट लेयर के इवेंट मैनेज करना

इस उदाहरण में, ID YOUR_DATASET_ID वाले डेटासेट के लिए डेटा की सुविधाएं दिखाई गई हैं. साथ ही, टैप की गई सुविधा के लिए किसी एट्रिब्यूट की वैल्यू दिखाने के लिए, delegate फ़ंक्शन का इस्तेमाल किया गया है.

इस उदाहरण में, डेटासेट में न्यूयॉर्क सिटी के पार्क दिखाए गए हैं. डेटासेट में, पार्क के हिसाब से हर डेटासेट सुविधा के लिए, acres नाम का एक एट्रिब्यूट होता है. एट्रिब्यूट में पार्क का कुल क्षेत्रफल भी बताया जाता है. टैप किए गए पार्क के लिए, acres एट्रिब्यूट की वैल्यू दिखाएं.

Swift

class SampleViewController: UIViewController {

  private lazy var mapView: GMSMapView = {
    let options = GMSMapViewOptions()
    options.mapID = GMSMapID(identifier: "YOUR_MAP_ID")
    options.camera = GMSCameraPosition(latitude: 40.7, longitude: -74, zoom: 12)
    return GMSMapView(options: options)
  }()

  // Set default styles.
  view = mapView
  let style = FeatureStyle(fill: .green.withAlphaComponent(0.5), stroke: .green, strokeWidth: 2)
  mapView.datasetFeatureLayer(of: "YOUR_DATASET_ID").style = { _ in style }
  mapView.delegate = self
}

extension SampleViewController: GMSMapViewDelegate {
  func mapView(_ mapView: GMSMapView, didTap features: [Feature], in featureLayer: FeatureLayer<Feature>, atLocation: CLLocationCoordinate2D) {
    let toast = UIAlertController(title: "Area of park", message: (features.compactMap { ($0 as? DatasetFeature)?.datasetAttributes["acres"] }).joined(separator: ", "), preferredStyle: .alert)
    present(toast, animated: true, completion: nil)
  }
}

Objective-C

@interface SampleViewController: UIViewController <GMSMapViewDelegate>
@end

@implementation SampleViewController
- (void)loadView {
    GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init];
    options.camera = [GMSCameraPosition cameraWithLatitude:40.7 longitude:-74 zoom:12];
    options.mapID = [GMSMapID mapIDWithIdentifier:@"YOUR_MAP_ID"];
    GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options];
    mapView.delegate = self;

    // Set default styles.
    GMSFeatureStyle *style = [GMSFeatureStyle styleWithFillColor:[[UIColor greenColor] colorWithAlphaComponent:0.5] strokeColor:[UIColor greenColor] strokeWidth:2.0];
    [mapView datasetFeatureLayerOfDatasetID:@"YOUR_DATASET_ID"].style = ^(GMSDatasetFeature *feature) { return style; };

    self.view = mapView;
}

- (void)mapView:(GMSMapView *)mapView didTapFeatures:(NSArray<id<GMSFeature>> *)features inFeatureLayer:(GMSFeatureLayer *)featureLayer atLocation:(CLLocationCoordinate2D)location {
  NSMutableArray<NSString *> *parkAreas = [NSMutableArray array];

  for (id<GMSFeature> feature in features) {
    if (![feature isKindOfClass:[GMSDatasetFeature class]]) { continue; }
    NSString *nameDefinedInDataset = ((GMSDatasetFeature *)feature).datasetAttributes[@"acres"];
    [parkAreas addObject:nameDefinedInDataset];
  }

  UIAlertController *toast = [UIAlertController alertControllerWithTitle:@"Area of park" message:[parkAreas componentsJoinedByString:@", "] preferredStyle:UIAlertControllerStyleAlert];
  [self presentViewController:toast animated:YES completion:nil];
}
@end