กิจกรรม
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
เมื่อใช้ 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) จะปรากฏบนแผนที่ฐานพร้อมกับไอคอนที่เกี่ยวข้อง POI ได้แก่ สวนสาธารณะ โรงเรียน อาคารภาครัฐ และอื่นๆ รวมถึง POI ของธุรกิจ เช่น ร้านค้า ร้านอาหาร และโรงแรม
คุณตอบสนองต่อเหตุการณ์การคลิกในจุดที่น่าสนใจได้ ดูคำแนะนำเกี่ยวกับ
ธุรกิจและจุดที่น่าสนใจอื่นๆ
เหตุการณ์อื่นๆ
ดูรายการวิธีการทั้งหมดใน GMSMapViewDelegate
ได้ที่คำแนะนำข้อมูลอ้างอิง
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eThe Maps SDK for iOS allows you to listen to events like camera changes and marker taps by implementing the \u003ccode\u003eGMSMapViewDelegate\u003c/code\u003e protocol.\u003c/p\u003e\n"],["\u003cp\u003eYou can track camera position changes using events such as \u003ccode\u003ewillMove\u003c/code\u003e, \u003ccode\u003edidChangeCameraPosition\u003c/code\u003e, and \u003ccode\u003eidleAtCameraPosition\u003c/code\u003e to update map content dynamically.\u003c/p\u003e\n"],["\u003cp\u003eRespond to clicks on businesses and points of interest (POIs) using dedicated event handling mechanisms.\u003c/p\u003e\n"],["\u003cp\u003eRefer to the \u003ccode\u003eGMSMapViewDelegate\u003c/code\u003e reference for a comprehensive list of available events and methods.\u003c/p\u003e\n"]]],["To listen to events on the Maps SDK for iOS, implement the `GMSMapViewDelegate` protocol in your view controller. Set the `GMSMapView`'s delegate to your view controller during creation. Implement relevant methods from the `GMSMapViewDelegate` to listen to specific events. For camera position changes, use `mapView:willMove:`, `mapView:didChangeCameraPosition:`, and `mapView:idleAtCameraPosition:`. The example provided demonstrates how to clear the map on move and reverse geocode the final camera position.\n"],null,["\u003cbr /\u003e\n\nSelect platform: [Android](/maps/documentation/android-sdk/events \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/events \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/events \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nUsing the Maps SDK for iOS, you can listen to events that occur on the\nmap, such as camera change events or marker tap events.\n\nIntroduction\n\nTo listen to events, you must implement the\n[`GMSMapViewDelegate`](/maps/documentation/ios-sdk/reference/objc/Protocols/GMSMapViewDelegate) protocol. Typically, you implement\nthis protocol on the view controller that displays the map. Below is an example:\n\n\nSwift \n\n```swift\nimport GoogleMaps\n\nclass Events: UIViewController, GMSMapViewDelegate {\n // ...\n}\n \n```\n\nObjective-C \n\n```objective-c\n@import GoogleMaps;\n\n@interface Events : UIViewController \u003cGMSMapViewDelegate\u003e\n\n@end\n \n```\n\n\u003cbr /\u003e\n\nWhen the `GMSMapView` is created, you can set its delegate to your view\ncontroller. The `GMSMapViewDelegate` provides only optional methods. To listen\nto any particular event, you must implement the relevant method.\n\n\nSwift \n\n```swift\noverride func loadView() {\n super.loadView()\n let camera = GMSCameraPosition.camera(\n withLatitude: 1.285,\n longitude: 103.848,\n zoom: 12\n )\n let mapView = GMSMapView.map(withFrame: .zero, camera: camera)\n mapView.delegate = self\n self.view = mapView\n}\n\n// MARK: GMSMapViewDelegate\n\nfunc mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {\n print(\"You tapped at \\(coordinate.latitude), \\(coordinate.longitude)\")\n}\n \n```\n\nObjective-C \n\n```objective-c\n- (void)loadView {\n [super loadView];\n GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:1.285\n longitude:103.848\n zoom:12];\n GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];\n mapView.delegate = self;\n self.view = mapView;\n}\n\n#pragma mark - GMSMapViewDelegate\n\n- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate {\n NSLog(@\"You tapped at %f,%f\", coordinate.latitude, coordinate.longitude);\n}\n \n```\n\n\u003cbr /\u003e\n\nCamera position\n\nUsing the `GMSMapViewDelegate`, you can listen to changes to the camera position\nused to render the map. There are three distinct events.\n\n- `mapView:willMove:` indicates that the camera position is about to change.\n If the `gesture` argument is set to `YES`, this is due to a user performing\n a natural gesture on the `GMSMapView`, such as a pan or tilt.\n Otherwise, `NO` indicates that this is part of a programmatic change -\n for example, via methods such as `animateToCameraPosition:` or updating the\n map's layer directly. This may also be `NO` if a user has tapped on the My\n Location or compass buttons, which generate animations that change the\n camera.\n\n This method may be called several times before\n `mapView:idleAtCameraPosition:` is invoked, although this typically\n happens only if animations and gestures occur at the same time - a gesture\n will cancel any current animation, for instance, and will call\n `mapView:willMove:` a second time.\n- `mapView:didChangeCameraPosition:` is called repeatedly during a gesture or\n animation, always after a call to `mapView:willMove:`. It is passed the\n intermediate camera position.\n\n- Finally, `mapView:idleAtCameraPosition:` is invoked once the camera position\n on `GMSMapView` becomes idle, and specifies the relevant camera position.\n At this point, all animations and gestures have stopped.\n\n Applications can use this event to trigger a refresh of markers or other\n content being displayed on the `GMSMapView`, rather than, for example,\n reloading the content on every camera change.\n\nFor example, an application can clear the `GMSMapView` on move, and then\nreverse geocode the position the camera comes to rest on.\n\n\nSwift \n\n```swift\nlet geocoder = GMSGeocoder()\n\nfunc mapView(_ mapView: GMSMapView, willMove gesture: Bool) {\n mapView.clear()\n}\n\nfunc mapView(_ mapView: GMSMapView, idleAt cameraPosition: GMSCameraPosition) {\n geocoder.reverseGeocodeCoordinate(cameraPosition.target) { (response, error) in\n guard error == nil else {\n return\n }\n\n if let result = response?.firstResult() {\n let marker = GMSMarker()\n marker.position = cameraPosition.target\n marker.title = result.lines?[0]\n marker.snippet = result.lines?[1]\n marker.map = mapView\n }\n }\n }\n \n```\n\nObjective-C \n\n```objective-c\nGMSGeocoder *geocoder;\n\n- (void)mapView:(GMSMapView *)mapView willMove:(BOOL)gesture {\n [mapView clear];\n}\n\n- (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)cameraPosition {\n id handler = ^(GMSReverseGeocodeResponse *response, NSError *error) {\n if (error != nil) {\n return;\n }\n GMSReverseGeocodeResult *result = response.firstResult;\n GMSMarker *marker = [GMSMarker markerWithPosition:cameraPosition.target];\n marker.title = result.lines[0];\n marker.snippet = result.lines[1];\n marker.map = mapView;\n };\n [geocoder reverseGeocodeCoordinate:cameraPosition.target completionHandler:handler];\n}\n \n```\n\n\u003cbr /\u003e\n\nEvents on businesses and other points of interest\n\nBy default, points of interest (POIs) appear on the base map along with their corresponding icons. POIs include parks, schools, government buildings, and more, as well as business POIs such as shops, restaurants, and hotels.\n\nYou can respond to click events on a POI. See the guide to\n[businesses and other points of interest](/maps/documentation/ios-sdk/poi).\n\nOther events\n\nTo learn about the full list of methods on `GMSMapViewDelegate`, see\n[the reference guide](/maps/documentation/ios-sdk/reference/objc/Protocols/GMSMapViewDelegate)."]]