เมื่อใช้ Maps SDK สำหรับ 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 options = GMSMapViewOptions() options.camera = camera let mapView = GMSMapView(options: options) 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]; GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init]; options.camera = camera; GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options]; 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 คุณจะรับฟังการเปลี่ยนแปลงตำแหน่งกล้องที่ใช้แสดงแผนที่ได้ โดยมีเหตุการณ์ที่แตกต่างกัน 3 เหตุการณ์
mapView:willMove:บ่งชี้ว่าตำแหน่งกล้องกำลังจะเปลี่ยน หากตั้งค่าอาร์กิวเมนต์gestureเป็นYESแสดงว่าผู้ใช้กำลังใช้ท่าทางที่เป็นธรรมชาติบนGMSMapViewเช่น การเลื่อนหรือการเอียง มิเช่นนั้นNOจะบ่งชี้ว่าการเปลี่ยนแปลงนี้เป็นส่วนหนึ่งของการเปลี่ยนแปลงแบบเป็นโปรแกรม เช่น ผ่านเมธอดอย่างanimateToCameraPosition:หรือการอัปเดตเลเยอร์ของแผนที่โดยตรง ค่าอาจเป็นNOด้วยหากผู้ใช้แตะปุ่มตำแหน่งของฉันหรือปุ่มเข็มทิศ ซึ่งจะสร้างภาพเคลื่อนไหวที่เปลี่ยนกล้องระบบอาจเรียกเมธอดนี้หลายครั้งก่อนที่จะเรียก
mapView:idleAtCameraPosition:แม้ว่าโดยปกติแล้วจะเกิดขึ้นก็ต่อเมื่อภาพเคลื่อนไหวและท่าทางเกิดขึ้นพร้อมกัน เช่น ท่าทางจะยกเลิกภาพเคลื่อนไหวปัจจุบันและเรียกmapView:willMove:เป็นครั้งที่ 2mapView: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
static 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) จะปรากฏบนแผนที่ฐานพร้อมกับไอคอนที่เกี่ยวข้อง POI ได้แก่ สวนสาธารณะ โรงเรียน อาคารของรัฐ และอื่นๆ รวมถึง POI ของธุรกิจ เช่น ร้านค้า ร้านอาหาร และโรงแรม
คุณสามารถตอบสนองต่อเหตุการณ์การคลิกใน POI ได้ ดูคู่มือเกี่ยวกับ ธุรกิจและจุดสนใจอื่นๆ
เหตุการณ์อื่นๆ
ดูรายการเมธอดทั้งหมดใน GMSMapViewDelegate ได้ใน
คู่มืออ้างอิง