您可以使用 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:willMove:
後重複呼叫mapView:didChangeCameraPosition:
。系統會傳遞中間攝影機位置。最後,當
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
的完整方法清單,請參閱參考指南。