事件

選取平台: 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: 後一律顯示動畫。系統會將 中間相機位置。

  • 最後,在攝影機位置後會叫用 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) 與對應的圖示會一併顯示在基本地圖上。搜尋點包括公園、學校、政府大樓等,也包括像商店、餐廳和飯店等商家搜尋點。

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

其他事件

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