自定义路线多段线
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
在自定义路线折线(或标记)之前,您需要先初始化界面自定义选项。
初始化界面自定义选项
用于最初设置界面自定义选项的推荐回调在 GMTCMapViewDelegate
中声明。当 GMTCMapView
对象准备好渲染地图时,系统会触发 mapViewDidInitialize
回调。
样式协调器已初始化,但不存在任何界面元素。
Swift
/** ViewController.swift */
class ViewController: UIViewController, GMTCMapViewDelegate {
// MARK: - GMTCMapViewDelegate
func mapViewDidInitialize(_ mapview: GMTCMapView) {
// Set the UI customization options here.
}
}
Objective-C
/** ViewController.m */
@interface ViewController () <GMTCMapViewDelegate>
#pragma mark GMTCMapViewDelegate
- (void)mapViewDidInitialize:(GMTCMapView *)mapview {
// Set the UI customization options here.
}
自定义多段线
使用 GMTCConsumerMapStyleCoordinator#setPolylineStyleOptions(_:polylineType:)
设置多段线自定义。
以下示例展示了如何设置折线样式选项:
多段线类型
您可以自定义以下折线类型:
GMTCPolylineType.activeRoute
:车辆前往乘客下一个地点(上车地点或下车地点)的路线。
GMTCPolylineType.remainingRoute
:车辆完成 GMTCPolylineType.activeRoute
后剩余的行程路段。
这两种类型的照片都会在共享旅程中显示。
多段线属性
您可以为每条折线自定义的属性是 Google 地图 PolylineOptions
上提供的属性的子集。Consumer SDK GMTCPolylineStyleOptions
属性具有以下特征:
- 使用初始化程序构建。
- 如果您想为任何属性提供自定义值,则可以是不可变的,也可以是可变的。
- 具有默认值。
您可以自定义以下属性:
color
width
isVisible
:如需停用折线,请将 isVisible
设置为 false
。
isTrafficEnabled
:此属性默认设置为 false
。
示例
Swift
/** MapViewController.swift */
private func updatePolylineUIOptions() {
// Get the GMTCConsumerMapStyleCoordinator
let consumerMapStyleCoordinator = mapView.consumerMapStyleCoordinator
// The polyline type that you would like to set custom UI options for.
let customizablePolylineType = GMTCPolylineType.activeRoute
// Initializing polyline options with default values (immutable version).
let polylineStyleOptions = GMTCPolylineStyleOptions()
consumerMapStyleCoordinator.setPolylineStyleOptions(
polylineStyleOptions, polylineType: customizablePolylineType)
// Initializing polyline options with custom values (mutable version).
let mutablePolylineStyleOptions = GMTCMutablePolylineStyleOptions()
mutablePolylineStyleOptions.isVisible = true
mutablepolylineStyleOptions.strokeWidth = 8.0
mutablepolylineStyleOptions.strokeColor = .blue
mutablepolylineStyleOptions.zIndex = 1000
mutablepolylineStyleOptions.isGeodesic = true
mutablePolylineStyleOptions.isTrafficEnabled = true
mutablePolylineStyleOptions.setTrafficColorFor(.slow, color: .yellow)
mutablePolylineStyleOptions.setTrafficColorFor(.trafficJam, color: .purple)
consumerMapStyleCoordinator.setPolylineStyleOptions(
mutablePolylineStyleOptions, polylineType: customizablePolylineType)
// Reset polyline options to default values.
consumerMapStyleCoordinator.setPolylineStyleOptions(
nil, polylineType: customizablePolylineType)
}
Objective-C
/** MapViewController.m */
- (void)updatePolylineUIOptions {
// Get the GMTCConsumerMapStyleCoordinator
GMTCConsumerMapStyleCoordinator *consumerMapStyleCoordinator = [_mapView consumerMapStyleCoordinator];
// The polyline type that you would like to set custom UI options for.
GMTCPolylineType customizablePolylineType = GMTCPolylineTypeActiveRoute;
// Initializing polyline options with default values (immutable version).
GMTCPolylineStyleOptions *polylineStyleOptions = [[GMTCPolylineStyleOptions alloc] init];
[consumerMapStyleCoordinator setPolylineStyleOptions:polylineStyleOptions
polylineType:customizablePolylineType];
// Initializing polyline options with custom values (mutable version).
GMTCMutablePolylineStyleOptions *mutablePolylineStyleOptions = [[GMTCMutablePolylineStyleOptions alloc] init];
mutablePolylineStyleOptions.isVisible = YES;
mutablepolylineStyleOptions.strokeWidth = 8.0;
mutablepolylineStyleOptions.strokeColor = [UIColor blueColor];
mutablepolylineStyleOptions.zIndex = 1000;
mutablepolylineStyleOptions.isGeodesic = YES;
mutablePolylineStyleOptions.isTrafficEnabled = YES;
[mutablePolylineStyleOptions setTrafficColorForSpeed:GMTSSpeedTypeSlow color:[UIColor yellowColor]];
[mutablePolylineStyleOptions setTrafficColorForSpeed:GMTSSpeedTypeTrafficJam color:[UIColor purpleColor]];
[consumerMapStyleCoordinator setPolylineStyleOptions:mutablePolylineStyleOptions
polylineType:customizablePolylineType];
// Reset polyline options to default values.
[consumerMapStyleCoordinator setPolylineStyleOptions:nil
polylineType:customizablePolylineType];
}
考虑路况的多段线
折线的流量图层默认处于停用状态。如果您使用 polylineStyleOptions.isTrafficEnabled = true
启用此功能,系统会绘制表示非正常流量时段的路段作为路线。
路况以四种速度之一表示:
GMTSSpeedType.noData
GMTSSpeedType.normal
GMTSSpeedType.slow
GMTSSpeedType.trafficJam
您可以使用 setTrafficColorFor(_:color:)
自定义表示每种速度分类的颜色。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-31。
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eCustomize the appearance of active and remaining route polylines, including color, width, and visibility, using \u003ccode\u003eGMTCPolylineStyleOptions\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eInitialize UI customization options within the \u003ccode\u003emapViewDidInitialize\u003c/code\u003e callback of the \u003ccode\u003eGMTCMapViewDelegate\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eEnable traffic-aware polylines and customize their color representation for different traffic speeds.\u003c/p\u003e\n"],["\u003cp\u003eAccess and modify polyline properties through the \u003ccode\u003eGMTCConsumerMapStyleCoordinator\u003c/code\u003e using the \u003ccode\u003esetPolylineStyleOptions\u003c/code\u003e method.\u003c/p\u003e\n"]]],["Initially, UI customization options are set within the `mapViewDidInitialize` callback. Polyline customization is managed via `GMTCConsumerMapStyleCoordinator#setPolylineStyleOptions(_:polylineType:)`, allowing customization of `activeRoute` and `remainingRoute`. Properties like `color`, `width`, `isVisible`, and `isTrafficEnabled` can be modified. Traffic-aware polylines, disabled by default, can be enabled and traffic conditions such as `noData`, `normal`, `slow`, and `trafficJam` can have custom colors. These options can be set to default or with custom values.\n"],null,["Select platform: [Android](/maps/documentation/mobility/journey-sharing/on-demand/android/customize-polylines \"View this page for the Android platform docs.\") [iOS](/maps/documentation/mobility/journey-sharing/on-demand/ios/customize-polylines \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/mobility/journey-sharing/on-demand/javascript/customize-route-polylines \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nBefore you customize route polylines (or markers), you first need to initialize\nthe UI customization options.\n\nInitialize UI customization options\n\nThe recommended callback used to initially set the UI customization options\nis declared in the `GMTCMapViewDelegate`. The `mapViewDidInitialize`\ncallback is triggered when the `GMTCMapView` object is ready to render the map.\nThe style coordinator is initialized, but no UI elements are present. \n\nSwift \n\n /** ViewController.swift */\n\n class ViewController: UIViewController, GMTCMapViewDelegate {\n\n // MARK: - GMTCMapViewDelegate\n\n func mapViewDidInitialize(_ mapview: GMTCMapView) {\n // Set the UI customization options here.\n }\n }\n\nObjective-C \n\n /** ViewController.m */\n\n @interface ViewController () \u003cGMTCMapViewDelegate\u003e\n\n #pragma mark GMTCMapViewDelegate\n\n - (void)mapViewDidInitialize:(GMTCMapView *)mapview {\n // Set the UI customization options here.\n }\n\nCustomize polylines\n\nPolyline customization is set using\n`GMTCConsumerMapStyleCoordinator#setPolylineStyleOptions(_:polylineType:)`.\n\nThe following example shows how to set polyline style options:\n\nPolyline types\n\nYou can customize the following polyline types:\n\n- `GMTCPolylineType.activeRoute`: The route the vehicle is taking to the rider's next point, whether it is the pickup or the drop-off.\n- `GMTCPolylineType.remainingRoute`: The segment of the trip that remains after the vehicle completes the `GMTCPolylineType.activeRoute`.\n\nBoth of these types are displayed throughout a shared journey.\n\nPolyline properties\n\nThe properties you can customize for each polyline are a subset of the\nproperties provided on Google Maps\n[`PolylineOptions`](https://developers.google.com/android/reference/com/google/android/gms/maps/model/PolylineOptions).\nThe Consumer SDK\n[`GMTCPolylineStyleOptions`](/maps/documentation/mobility/journey-sharing/on-demand/reference/ios/interface_g_m_t_c_polyline_style_options)\nproperties have the following traits:\n\n- Built using an initializer.\n- Can be immutable or mutable if you want to provide custom values for any property.\n- Have default values.\n\nYou can customize the following properties:\n\n- `color`\n- `width`\n- `isVisible`: To disable a polyline, set `isVisible` to `false`.\n- `isTrafficEnabled`: This property is set to `false` by default.\n\nExample \n\nSwift \n\n /** MapViewController.swift */\n\n private func updatePolylineUIOptions() {\n // Get the GMTCConsumerMapStyleCoordinator\n let consumerMapStyleCoordinator = mapView.consumerMapStyleCoordinator\n\n // The polyline type that you would like to set custom UI options for.\n let customizablePolylineType = GMTCPolylineType.activeRoute\n\n // Initializing polyline options with default values (immutable version).\n let polylineStyleOptions = GMTCPolylineStyleOptions()\n consumerMapStyleCoordinator.setPolylineStyleOptions(\n polylineStyleOptions, polylineType: customizablePolylineType)\n\n // Initializing polyline options with custom values (mutable version).\n let mutablePolylineStyleOptions = GMTCMutablePolylineStyleOptions()\n mutablePolylineStyleOptions.isVisible = true\n mutablepolylineStyleOptions.strokeWidth = 8.0\n mutablepolylineStyleOptions.strokeColor = .blue\n mutablepolylineStyleOptions.zIndex = 1000\n mutablepolylineStyleOptions.isGeodesic = true\n mutablePolylineStyleOptions.isTrafficEnabled = true\n mutablePolylineStyleOptions.setTrafficColorFor(.slow, color: .yellow)\n mutablePolylineStyleOptions.setTrafficColorFor(.trafficJam, color: .purple)\n consumerMapStyleCoordinator.setPolylineStyleOptions(\n mutablePolylineStyleOptions, polylineType: customizablePolylineType)\n\n // Reset polyline options to default values.\n consumerMapStyleCoordinator.setPolylineStyleOptions(\n nil, polylineType: customizablePolylineType)\n }\n\nObjective-C \n\n /** MapViewController.m */\n\n - (void)updatePolylineUIOptions {\n // Get the GMTCConsumerMapStyleCoordinator\n GMTCConsumerMapStyleCoordinator *consumerMapStyleCoordinator = [_mapView consumerMapStyleCoordinator];\n\n // The polyline type that you would like to set custom UI options for.\n GMTCPolylineType customizablePolylineType = GMTCPolylineTypeActiveRoute;\n\n // Initializing polyline options with default values (immutable version).\n GMTCPolylineStyleOptions *polylineStyleOptions = [[GMTCPolylineStyleOptions alloc] init];\n [consumerMapStyleCoordinator setPolylineStyleOptions:polylineStyleOptions\n polylineType:customizablePolylineType];\n\n // Initializing polyline options with custom values (mutable version).\n GMTCMutablePolylineStyleOptions *mutablePolylineStyleOptions = [[GMTCMutablePolylineStyleOptions alloc] init];\n mutablePolylineStyleOptions.isVisible = YES;\n mutablepolylineStyleOptions.strokeWidth = 8.0;\n mutablepolylineStyleOptions.strokeColor = [UIColor blueColor];\n mutablepolylineStyleOptions.zIndex = 1000;\n mutablepolylineStyleOptions.isGeodesic = YES;\n mutablePolylineStyleOptions.isTrafficEnabled = YES;\n [mutablePolylineStyleOptions setTrafficColorForSpeed:GMTSSpeedTypeSlow color:[UIColor yellowColor]];\n [mutablePolylineStyleOptions setTrafficColorForSpeed:GMTSSpeedTypeTrafficJam color:[UIColor purpleColor]];\n [consumerMapStyleCoordinator setPolylineStyleOptions:mutablePolylineStyleOptions\n polylineType:customizablePolylineType];\n\n // Reset polyline options to default values.\n [consumerMapStyleCoordinator setPolylineStyleOptions:nil\n polylineType:customizablePolylineType];\n }\n\nTraffic-aware polylines\n\nThe traffic layer of the polyline is disabled by default. When you enable it\nby using `polylineStyleOptions.isTrafficEnabled = true`, segments representing\nstretches of non-normal traffic are drawn as the route.\n\nTraffic conditions are represented as one of four speeds:\n\n- `GMTSSpeedType.noData`\n- `GMTSSpeedType.normal`\n- `GMTSSpeedType.slow`\n- `GMTSSpeedType.trafficJam`\n\nYou can customize the color representing each of those speed classifications\nby using `setTrafficColorFor(_:color:)`."]]