事件

選取平台: Android iOS JavaScript

您可以使用 Maps SDK for iOS 監聽地圖上發生的事件,例如相機變更事件或標記輕觸事件。

引言

如要監聽事件,您必須實作 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: 後。而傳遞到中階相機位置。

  • 最後,當 GMSMapView 上的攝影機位置處於閒置狀態,並指定相關相機位置時,就會叫用 mapView:idleAtCameraPosition:。在這一點上,所有的動畫和手勢停止了。

    應用程式可以使用這個事件來觸發重新整理 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];
}
      

商家和其他搜尋點的事件

根據預設,搜尋點與對應的圖示會一併顯示在基本地圖上。搜尋點包括公園、學校、政府大樓等,也包括像商店、餐廳和飯店等商家搜尋點。

您可以回應搜尋點上的點擊事件。請參閱商家和其他搜尋點指南。

其他事件

如要瞭解 GMSMapViewDelegate 的完整方法清單,請參閱參考指南