您可以使用 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
的方法完整清單,請參閱參考指南。