设置地图

请选择平台: Android iOS

要在 iOS 中设置地图以追踪行程,请完成以下步骤:

  1. 初始化地图视图
  2. 处理地图事件

第 1 步:初始化地图视图

要跟踪行程,您必须初始化地图视图。

以下示例展示了如何初始化 GMTCMapView

Swift

/*
 * MapViewController.swift
 */
class ViewController: UIViewController, GMTCMapViewDelegate {
  private var rideSharingMap: GMTCMapView?

  override func viewDidLoad() {
    super.viewDidLoad()

    self.rideSharingMap = GMTCMapView(frame: UIScreen.main.bounds)
    self.rideSharingMap.delegate = self
    self.rideSharingMap?.settings.myLocationButton = true
    self.view.addSubview(self.rideSharingMap!)
    ...
    }
  }

Objective-C

/*
 * MapViewController.h
 */
@interface MapViewController : UIViewController<GMTCMapViewDelegate>
...
@end

/*
 * MapViewController.m
 */
@implementation MapViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  ...
  self.mapView = [[GMTCMapView alloc] initWithFrame:CGRectZero];
  self.mapView.settings.myLocationButton = YES;
  self.mapView.delegate = self;
  ...
}

...

@end

第 2 步:处理地图视图事件

现在您已初始化地图视图,下面介绍了如何实现委托 在汽车行驶过程中处理地图视图事件变化。

Swift

func mapViewDidInitialize(_ mapview: GMTCMapView) {
  // Handle the update to the state of the map view to browsing.
}

func mapView(_ mapView: GMSMapView, didTapConsumerMarker mapMarker: 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 *)mapView
    didTapConsumerMarker:(nonnull GMSMarker *)mapMarker
              markerType:(GMTCMapViewMarkerType)markerType {
  // Handle the mapView marker was tapped.
}

后续步骤