Bevor Sie Routenpolylinien (oder Markierungen) anpassen, müssen Sie zuerst die Optionen zur Anpassung der Benutzeroberfläche initialisieren.
Optionen zur Anpassung der Benutzeroberfläche initialisieren
Der empfohlene Callback, der zum anfänglichen Festlegen der Optionen zur Anpassung der Benutzeroberfläche verwendet wird, ist in GMTCMapViewDelegate deklariert. Der Callback mapViewDidInitialize wird ausgelöst, wenn das GMTCMapView-Objekt bereit ist, die Karte zu rendern.
Der Stilkoordinator wird initialisiert, aber es sind keine UI-Elemente vorhanden.
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.
}
Polylinien anpassen
Die Polylinienanpassung wird mit GMTCConsumerMapStyleCoordinator#setPolylineStyleOptions(_:polylineType:) festgelegt.
Im folgenden Beispiel wird gezeigt, wie Sie Optionen für den Polylinienstil festlegen:
Polylinientypen
Sie können die folgenden Polylinientypen anpassen:
GMTCPolylineType.activeRoute: Die Route, die das Fahrzeug zum nächsten Punkt des Fahrers nimmt, unabhängig davon, ob es sich um den Abhol- oder den Zielort handelt.GMTCPolylineType.remainingRoute: Das Segment der Fahrt, das nach Abschluss derGMTCPolylineType.activeRoutedurch das Fahrzeug verbleibt.
Beide Typen werden während einer gemeinsamen Fahrt angezeigt.
Polylinienattribute
Die Attribute, die Sie für jede Polylinie anpassen können, sind eine Teilmenge der
Attribute, die in den Google Maps
PolylineOptions bereitgestellt werden.
Die
GMTCPolylineStyleOptions
Attribute des Consumer SDK haben die folgenden Merkmale:
- Mit einem Initialisierer erstellt.
- Sie können unveränderlich oder veränderlich sein, wenn Sie benutzerdefinierte Werte für ein Attribut angeben möchten.
- Sie haben Standardwerte.
Sie können die folgenden Attribute anpassen:
colorwidthisVisible: Wenn Sie eine Polylinie deaktivieren möchten, legen SieisVisibleauffalsefest.isTrafficEnabled: Dieses Attribut ist standardmäßig auffalsegesetzt.
Beispiel
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];
}
Verkehrsabhängige Polylinien
Die Verkehrsebene der Polylinie ist standardmäßig deaktiviert. Wenn Sie sie mit polylineStyleOptions.isTrafficEnabled = true aktivieren, werden Segmente, die Strecken mit ungewöhnlichem Verkehrsaufkommen darstellen, als Route gezeichnet.
Die Verkehrslage wird als eine von vier Geschwindigkeiten dargestellt:
GMTSSpeedType.noDataGMTSSpeedType.normalGMTSSpeedType.slowGMTSSpeedType.trafficJam
Mit setTrafficColorFor(_:color:) können Sie die Farbe anpassen, die für jede dieser Geschwindigkeitsklassen steht.