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