ปรับแต่งเส้นประกอบเส้นทาง
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ก่อนที่จะปรับแต่งเส้นหลายเส้น (หรือเครื่องหมาย) ของเส้นทาง คุณต้องเริ่มต้นตัวเลือกการปรับแต่ง UI ก่อน
เริ่มต้นตัวเลือกการปรับแต่ง UI
ประกาศ Callback ที่แนะนำซึ่งใช้ในการตั้งค่าตัวเลือกการปรับแต่ง UI ในตอนแรก
ใน GMTCMapViewDelegate
mapViewDidInitialize
การเรียกกลับจะทริกเกอร์เมื่อออบเจ็กต์ GMTCMapView
พร้อมแสดงแผนที่
เริ่มต้นตัวประสานงานสไตล์แล้ว แต่ไม่มีองค์ประกอบ UI
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:)
ตัวอย่างต่อไปนี้แสดงวิธีตั้งค่าตัวเลือกรูปแบบของ Polyline
ประเภทเส้นประกอบ
คุณปรับแต่งประเภทเส้นหลายเส้นต่อไปนี้ได้
GMTCPolylineType.activeRoute
: เส้นทางที่ยานพาหนะใช้ไปยังจุดถัดไปของผู้โดยสาร ไม่ว่าจะเป็นจุดรับหรือจุดส่ง
GMTCPolylineType.remainingRoute
: ส่วนของการเดินทางที่เหลืออยู่
หลังจากที่ยานพาหนะทำGMTCPolylineType.activeRoute
เสร็จแล้ว
ทั้ง 2 ประเภทนี้จะแสดงตลอดเส้นทางที่แชร์
คุณสมบัติของโพลีไลน์
พร็อพเพอร์ตี้ที่คุณปรับแต่งได้สำหรับแต่ละเส้นประกอบคือชุดย่อยของ
พร็อพเพอร์ตี้ที่ระบุไว้ใน Google Maps
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
ระบบจะวาดกลุ่มที่แสดง
ช่วงการรับส่งข้อมูลที่ไม่ปกติเป็นเส้นทาง
สภาพการจราจรจะแสดงเป็นความเร็ว 4 ระดับดังนี้
GMTSSpeedType.noData
GMTSSpeedType.normal
GMTSSpeedType.slow
GMTSSpeedType.trafficJam
คุณปรับแต่งสีที่แสดงถึงการจัดประเภทความเร็วแต่ละประเภทได้
โดยใช้ setTrafficColorFor(_:color:)
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\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:)`."]]