เมื่อใช้ 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 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
คุณจะฟังการเปลี่ยนแปลงตำแหน่งกล้องที่ใช้แสดงผลแผนที่ได้ โดยมี 3 เหตุการณ์ที่แตกต่างกัน
mapView:willMove:
บ่งบอกว่าตำแหน่งของกล้องกำลังจะเปลี่ยน หากตั้งค่าอาร์กิวเมนต์gesture
เป็นYES
กรณีนี้เกิดจากผู้ใช้มีท่าทางสัมผัสตามธรรมชาติในGMSMapView
เช่น การแพนหรือเอียง มิฉะนั้นNO
จะระบุว่าการดำเนินการนี้เป็นส่วนหนึ่งของการเปลี่ยนแปลงแบบเป็นโปรแกรม เช่น ผ่านเมธอดอย่างanimateToCameraPosition:
หรืออัปเดตเลเยอร์ของแผนที่โดยตรง ค่านี้อาจเป็นNO
หากผู้ใช้แตะปุ่ม "ตำแหน่งของฉัน" หรือปุ่มเข็มทิศซึ่งจะสร้างภาพเคลื่อนไหวที่เปลี่ยนกล้องอาจมีการเรียกเมธอดนี้หลายครั้งก่อนที่จะมีการเรียกใช้
mapView:idleAtCameraPosition:
แม้ว่าโดยปกติจะเกิดขึ้นเฉพาะเมื่อมีการภาพเคลื่อนไหวและท่าทางสัมผัสเกิดขึ้นพร้อมกันเท่านั้น ตัวอย่างเช่น ท่าทางสัมผัสจะยกเลิกภาพเคลื่อนไหวปัจจุบัน และจะเรียกใช้mapView:willMove:
เป็นครั้งที่ 2จะมีการเรียก
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
โปรดดูคู่มืออ้างอิง