Bước 2: Xử lý các sự kiện của khung hiển thị bản đồ
Sau khi bạn khởi tạo khung hiển thị bản đồ, đây là cách triển khai một uỷ quyền để xử lý các thay đổi về sự kiện khung hiển thị bản đồ khi xe di chuyển trong hành trình.
Swift
funcmapViewDidInitialize(_mapview:GMTCMapView){// Handle the update to the state of the map view to browsing.}funcmapView(_mapView:GMSMapView,didTapConsumerMarkermapMarker:GMSMarker,markerType:GMTCMapViewMarkerType)->Bool{// Handle the mapView marker was tapped.}
Objective-C
/* * MapViewController.m */#pragma mark - GMTCMapViewDelegate implementation// Handle state update of map view.-(void)mapViewDidInitializeCustomerState:(GMTCMapView*)mapview{// Handle the update to the state of the map view to browsing.}-(void)mapView:(GMSMapView*)mapViewdidTapConsumerMarker:(nonnullGMSMarker*)mapMarkermarkerType:(GMTCMapViewMarkerType)markerType{// Handle the mapView marker was tapped.}
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eThis documentation outlines how to set up and use the Google Maps SDK for iOS to track and display trips in real-time within your application.\u003c/p\u003e\n"],["\u003cp\u003eThe guide covers initializing the map view, enabling user location, and handling events like the map becoming ready and marker taps.\u003c/p\u003e\n"],["\u003cp\u003eYou can customize the appearance of the map using styling options provided by the SDK, as well as follow a vehicle's trip progression with built-in features.\u003c/p\u003e\n"]]],["This content details how to set up a map to follow a trip in iOS. The process involves two key steps: first, initializing the map view (`GMTCMapView`) within a view controller, as demonstrated in Swift and Objective-C code examples. Second, implementing a delegate to handle map view events, which includes managing state updates and responding to marker taps. Customization of the map's style and the process to follow a trip are suggested as next steps.\n"],null,["Select platform: [Android](/maps/documentation/mobility/journey-sharing/on-demand/android/create-ui \"View this page for the Android platform docs.\") [iOS](/maps/documentation/mobility/journey-sharing/on-demand/ios/create-ui \"View this page for the iOS platform docs.\")\n\n\u003cbr /\u003e\n\nTo set up a map to follow a trip in iOS, complete the following steps:\n\n1. [Initialize the map view](#map-view)\n2. [Handle map events](#handle-updates)\n\n| **Note:** After you initialize the map view, you can customize the map. For more details, see [Style a map](/maps/documentation/mobility/journey-sharing/on-demand/ios/style).\n\nStep 1: Initialize the map view\n\nTo follow a trip, you must initialize a map view.\n\nThe following example shows how to initialize `GMTCMapView`. \n\nSwift \n\n /*\n * MapViewController.swift\n */\n class ViewController: UIViewController, GMTCMapViewDelegate {\n private var rideSharingMap: GMTCMapView?\n\n override func viewDidLoad() {\n super.viewDidLoad()\n\n self.rideSharingMap = GMTCMapView(frame: UIScreen.main.bounds)\n self.rideSharingMap.delegate = self\n self.rideSharingMap?.settings.myLocationButton = true\n self.view.addSubview(self.rideSharingMap!)\n ...\n }\n }\n\nObjective-C \n\n /*\n * MapViewController.h\n */\n @interface MapViewController : UIViewController\u003cGMTCMapViewDelegate\u003e\n ...\n @end\n\n /*\n * MapViewController.m\n */\n @implementation MapViewController\n\n - (void)viewDidLoad {\n [super viewDidLoad];\n ...\n self.mapView = [[GMTCMapView alloc] initWithFrame:CGRectZero];\n self.mapView.settings.myLocationButton = YES;\n self.mapView.delegate = self;\n ...\n }\n\n ...\n\n @end\n\nStep 2: Handle map view events\n\nNow that you've initialized the map view, here's how to implement a delegate\nto handle map view event changes as the vehicle progresses along its journey. \n\nSwift \n\n func mapViewDidInitialize(_ mapview: GMTCMapView) {\n // Handle the update to the state of the map view to browsing.\n }\n\n func mapView(_ mapView: GMSMapView, didTapConsumerMarker mapMarker: GMSMarker, markerType: GMTCMapViewMarkerType) -\u003e Bool {\n // Handle the mapView marker was tapped.\n }\n\nObjective-C \n\n /*\n * MapViewController.m\n */\n #pragma mark - GMTCMapViewDelegate implementation\n\n // Handle state update of map view.\n - (void)mapViewDidInitializeCustomerState:(GMTCMapView *)mapview {\n // Handle the update to the state of the map view to browsing.\n }\n\n - (void)mapView:(GMSMapView *)mapView\n didTapConsumerMarker:(nonnull GMSMarker *)mapMarker\n markerType:(GMTCMapViewMarkerType)markerType {\n // Handle the mapView marker was tapped.\n }\n\nWhat's next\n\n- [Style a map](/maps/documentation/mobility/journey-sharing/on-demand/ios/style)\n- [Follow a trip in iOS](/maps/documentation/mobility/journey-sharing/on-demand/ios/share-journey)"]]