이벤트

플랫폼 선택: Android iOS JavaScript

iOS용 Maps SDK를 사용하면 카메라 변경 이벤트나 마커 탭 이벤트 등입니다.

소개

이벤트를 수신하려면 GMSMapViewDelegate 프로토콜 일반적으로 지도를 표시하는 뷰 컨트롤러의 이 프로토콜입니다. 아래 예시를 참조하세요.

Swift

import GoogleMaps

class Events: UIViewController, GMSMapViewDelegate {
  // ...
}
      

Objective-C

@import GoogleMaps;

@interface Events : UIViewController <GMSMapViewDelegate>

@end
      

GMSMapView를 만들 때 위임을 뷰에 설정할 수 있습니다. 있습니다. GMSMapViewDelegate는 선택적 메서드만 제공합니다. 듣기 를 호출하려면 관련 메서드를 구현해야 합니다.

Swift

override func loadView() {
  super.loadView()
  let camera = GMSCameraPosition.camera(
    withLatitude: 1.285,
    longitude: 103.848,
    zoom: 12
  )
  let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
  mapView.delegate = self
  self.view = mapView
}

// MARK: GMSMapViewDelegate

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
  print("You tapped at \(coordinate.latitude), \(coordinate.longitude)")
}
      

Objective-C

- (void)loadView {
  [super loadView];
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:1.285
                                                          longitude:103.848
                                                               zoom:12];
  GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  mapView.delegate = self;
  self.view = mapView;
}

#pragma mark - GMSMapViewDelegate

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {
  NSLog(@"You tapped at %f,%f", coordinate.latitude, coordinate.longitude);
}
      

카메라 위치

GMSMapViewDelegate를 사용하여 카메라 위치의 변경사항을 수신할 수 있습니다. 사용됩니다. 세 가지 고유한 이벤트가 있습니다.

  • mapView:willMove:는 카메라 위치가 곧 변경됨을 나타냅니다. gesture 인수가 YES로 설정된 경우 이는 사용자가 GMSMapView에서 화면 이동 또는 기울이기와 같은 자연스러운 동작을 합니다. 그렇지 않은 경우 NO은 이것이 프로그래매틱 변경의 일부임을 나타냅니다. 예를 들어 animateToCameraPosition:와 같은 메서드를 통해 또는 레이어를 직접 수정할 수 있습니다. 사용자가 내 버튼을 탭한 경우에도 NO일 수 있습니다. 위치 또는 나침반 버튼은 광고의 방향을 변경하는 애니메이션을 생성합니다. 있습니다.

    이 메서드는 호출하기 전에 여러 번 호출될 수 있습니다. mapView:idleAtCameraPosition:가 호출되지만 일반적으로 애니메이션과 동작이 동시에 발생하는 경우에만 발생합니다. 예를 들어 현재 애니메이션을 취소하고 두 번째 mapView:willMove:.

  • mapView:didChangeCameraPosition:가 동작 중에 반복적으로 호출되거나 애니메이션, 항상 mapView:willMove: 호출 후 이 함수는 중간 카메라 위치입니다.

  • 마지막으로 mapView:idleAtCameraPosition:는 카메라 위치가 지정되면 호출됩니다. GMSMapView에서 유휴 상태가 되어 관련 카메라 위치를 지정합니다. 이 시점에서 모든 애니메이션과 제스처가 중단됩니다.

    애플리케이션은 이 이벤트를 사용하여 마커 또는 기타 GMSMapView에 표시되는 콘텐츠를 예로 들 수 없습니다. 콘텐츠를 다시 로드하는 것이 좋습니다

예를 들어 애플리케이션은 이동 시 GMSMapView를 지운 다음 카메라가 고정되는 위치를 역 지오코딩합니다.

Swift

let geocoder = GMSGeocoder()

func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {
  mapView.clear()
}

func mapView(_ mapView: GMSMapView, idleAt cameraPosition: GMSCameraPosition) {
    geocoder.reverseGeocodeCoordinate(cameraPosition.target) { (response, error) in
      guard error == nil else {
        return
      }

      if let result = response?.firstResult() {
        let marker = GMSMarker()
        marker.position = cameraPosition.target
        marker.title = result.lines?[0]
        marker.snippet = result.lines?[1]
        marker.map = mapView
      }
    }
  }
      

Objective-C

GMSGeocoder *geocoder;

- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture {
  [mapView clear];
}

- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)cameraPosition {
  id handler = ^(GMSReverseGeocodeResponse *response, NSError *error) {
    if (error != nil) {
      return;
    }
    GMSReverseGeocodeResult *result = response.firstResult;
    GMSMarker *marker = [GMSMarker markerWithPosition:cameraPosition.target];
    marker.title = result.lines[0];
    marker.snippet = result.lines[1];
    marker.map = mapView;
  };
  [geocoder reverseGeocodeCoordinate:cameraPosition.target completionHandler:handler];
}
      

비즈니스 및 기타 관심 장소의 이벤트

기본적으로, 관심 지점(POI)은 해당 아이콘과 함께 기본 지도에 나타납니다. POI에는 공원, 학교, 정부 건물 등 뿐만 아니라 상점, 식당, 호텔 등의 사업체 POI가 포함됩니다.

관심 장소의 클릭 이벤트에 응답할 수 있습니다. 다음 가이드를 참고하세요. 비즈니스 및 기타 관심 장소를 참조하세요.

기타 이벤트

GMSMapViewDelegate의 전체 메서드 목록에 관한 자세한 내용은 다음을 참고하세요. 참조 가이드를 확인하세요.