탭 이벤트 처리

데이터 지형지물이 탭 이벤트에 응답하도록 하고, 이벤트를 사용하여 탭한 지형지물의 속성 값을 표시합니다.

탭 이벤트에 대한 응답으로 스타일을 적용합니다.

데이터 세트 레이어 이벤트 처리

이 예에서는 ID가 YOUR_DATASET_ID인 데이터 세트의 데이터 특성을 보여주고 대리자 함수를 구현하여 탭한 지형지물의 속성 값을 표시합니다.

이 예에서 데이터 세트는 뉴욕시의 공원을 보여줍니다. 공원에 해당하는 각 데이터 세트 지형지물의 경우 데이터 세트에 공원의 표면적이 포함된 acres라는 속성이 포함됩니다. 탭한 공원의 경우 acres 속성 값을 표시합니다.

Swift

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

Objective-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