رویدادهای ضربه زدن را مدیریت کنید

کاری کنید که ویژگی های داده به رویدادهای ضربه زده پاسخ دهند و از رویداد برای نمایش مقدار یک ویژگی برای ویژگی ضربه زده شده استفاده کنید.

در پاسخ به یک رویداد ضربه زدن، یک ظاهر طراحی کنید.

رویدادهای لایه مجموعه داده را مدیریت کنید

این مثال ویژگی‌های داده را برای یک مجموعه داده با شناسه YOUR_DATASET_ID نشان می‌دهد و تابع delegate را برای نمایش مقدار یک ویژگی برای ویژگی ضربه‌خورده پیاده‌سازی می‌کند.

در این مثال، مجموعه داده پارک‌هایی را در شهر نیویورک نشان می‌دهد. برای هر ویژگی مجموعه داده، مربوط به یک پارک، مجموعه داده شامل یک ویژگی به نام acres است که شامل مساحت سطح پارک است. برای یک پارک ضربه خورده، مقدار ویژگی acres را نمایش دهید.

سریع

class SampleViewController: UIViewController {

  private lazy var mapView: GMSMapView = GMSMapView(frame: .zero, mapID: GMSMapID(identifier: "YOUR_MAP_ID"), camera: GMSCameraPosition(latitude: 40.7, longitude: -74, zoom: 12))

  // 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)
  }
}

هدف-C


@interface SampleViewController: UIViewController <GMSMapViewDelegate>
@end

@implementation SampleViewController
- (void)loadView {
    GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero mapID:[GMSMapID mapIDWithIdentifier:@"YOUR_MAP_ID"] camera:[GMSCameraPosition cameraWithLatitude:40.7 longitude:-74 zoom:12]];
    mapView.delegete = 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