Mit dem Consumer SDK können Sie benutzerdefinierte Markierungen und Routenpolygone auf Ihr App-Design anwenden. Mit diesen Designelementen kann in Ihrer Consumer-App eine dynamische Vorschau der Route des Fahrzeugs angezeigt werden.
In diesem Leitfaden werden die Optionen beschrieben, die das SDK für das Attribut consumerMapStyleCoordinator
bereitstellt. Dieses Attribut ist über die Klasse GMTCMapView
verfügbar. Es werden nur die UI-Elemente behandelt. Außerdem wird davon ausgegangen, dass Sie eine funktionsfähige Consumer-App haben. Informationen zum Einrichten der Back-End-Dienste, die vom Consumer SDK benötigt werden, finden Sie unter Erste Schritte mit Fleet Engine.
Anpassungsoptionen der Benutzeroberfläche werden initialisiert
Der empfohlene Callback, der zum anfänglichen Festlegen der Optionen für die UI-Anpassung verwendet wird, ist in GMTCMapViewDelegate
deklariert. Der mapViewDidInitialize
-Callback wird ausgelöst, wenn das GMTCMapView
-Objekt bereit ist, die Karte zu rendern.
Der Stilkoordinator wurde 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.
}
Benutzerdefinierte Markierungen
Im folgenden Beispiel wird GMTCMapView
zum Anpassen der Markierungsstile verwendet.
Mit setMarkerStyleOptions(_:markerType:)
können Sie den Typ und die Eigenschaften der Markierung festlegen. Die Optionen für benutzerdefinierte Markierungen überschreiben die vom Consumer SDK bereitgestellten Standardwerte.
Swift
/** ViewController.swift */
private func changeMarkerStyle(
markerStyleOptions: GMTCMarkerStyleOptions?,
markerType: GMTCCustomizableMarkerType
) {
let styleCoordinator = mapView.consumerMapStyleCoordinator
styleCoordinator.setMarkerStyleOptions(markerStyleOptions, markerType: markerType)
}
/** To restore the default values, call setMarkerStyleOptions(_:markerType:) using nil for the GMTCMarkerStyleOptions parameter.
Here is an example of retrieving the active GMTCMarkerStyleOptions. */
private func retrieveMarkerStyle(markerType: GMTCCustomizableMarkerType) {
let styleCoordinator = mapView.consumerMapStyleCoordinator
// The 'markerStyleOptions' contains the stored style options for this marker type.
let markerStyleOptions = styleCoordinator.markerStyleOptions(for: markerType)
}
Objective-C
/** ViewController.h */
- (void)changeMarkerStyle:(nullable GMTCMarkerStyleOptions *)markerStyleOptions
markerType:(GMTCCustomizableMarkerType)markerType {
GMTCConsumerMapStyleCoordinator *styleCoordinator = _mapView.consumerMapStyleCoordinator;
[styleCoordinator setMarkerStyleOptions:markerStyleOptions markerType:markerType];
}
/** To restore the default values, call setMarkerStyleOptions:markerStyleOptions:markerType: using nil for the GMTCMarkerStyleOptions parameter.
Here is an example of retrieving the active GMTCMarkerStyleOptions. */
- (void)retrieveMarkerStyle:(GMTCCustomizableMarkerType)markerType {
GMTCConsumerMapStyleCoordinator *styleCoordinator = _mapView.consumerMapStyleCoordinator;
// The 'markerStyleOptions' contains the stored style options for this marker type.
GMTCMarkerStyleOptions *markerStyleOptions = [styleCoordinator markerStyleOptionsForType:markerType];
}
Markierungstypen
Die folgenden Markierungen können angepasst werden:
GMTCCustomizableMarkerType.unknown
GMTCCustomizableMarkerType.tripPickupPoint
GMTCCustomizableMarkerType.tripDropoffPoint
GMTCCustomizableMarkerType.tripVehicle
GMTCCustomizableMarkerType.intermediateDestination
Mit GMTCCustomizableMarkerType.tripPickupPoint
, GMTCCustomizableMarkerType.intermediateDestination
und GMTCCustomizableMarkerType.tripDropoffPoint
lassen sich Wegpunkte während des Fahrt- und Bestellfortschritts anpassen.
Verwende GMTCCustomizableMarkerType.tripVehicle
, um das Fahrzeugsymbol während der Fahrt und des Bestellfortschritts anzupassen. Das Markierungssymbol ändert sich nicht entsprechend dem tatsächlichen Fahrzeugtyp für die Fahrt.
Markierungsoptionen
Die für jede Markierung verfügbaren anpassbaren Eigenschaften sind eine Teilmenge der von Google Maps MarkerOptions
bereitgestellten Eigenschaften.
Das Consumer SDK GMTCMarkerStyleOptions
wird mit einem Initialisierer erstellt und ist nach seiner Erstellung unveränderlich. Für jede Eigenschaft werden Standardwerte bereitgestellt, sodass nur benutzerdefinierte Werte angegeben werden müssen.
Die folgenden Eigenschaften können angepasst werden:
groundAnchor
isVisible
iconView
icon
zIndex
isFlat
Das Festlegen von isVisible
auf „false“ entspricht dem „Deaktivieren“ der Markierung.
Es sollten genügend Daten bereitgestellt werden, damit Sie stattdessen Ihr eigenes UI-Element verwenden können.
Beispiel
Swift
/** MapViewController.swift */
private func updateMarkerUIOptions() {
// Get the GMTCConsumerMapStyleCoordinator
let consumerMapStyleCoordinator = mapView.consumerMapStyleCoordinator
// The marker type that you would like to set custom UI options for.
let customizableMarkerType = GMTCCustomizableMarkerType.tripVehicle
// Initializing marker options.
let markerStyleOptions = GMTCMutableMarkerStyleOptions()
markerStyleOptions.groundAnchor = kGMSMarkerDefaultGroundAnchor
markerStyleOptions.icon = icon
markerStyleOptions.zIndex = 100
markerStyleOptions.isFlat = false
markerStyleOptions.isVisible = true
consumerMapStyleCoordinator.setMarkerStyleOptions(markerStyleOptions, markerType: customizableMarkerType)
// Reset marker options to default values.
consumerMapStyleCoordinator.setMarkerStyleOptions(nil, markerType: customizableMarkerType)
}
Objective-C
/** MapViewController.m */
- (void)updateMarkerUIOptions {
// Get the GMTCConsumerMapStyleCoordinator
GMTCConsumerMapStyleCoordinator *consumerMapStyleCoordinator = [_mapView consumerMapStyleCoordinator];
// The marker type that you would like to set custom UI options for.
GMTCCustomizableMarkerType customizableMarkerType = GMTCCustomizableMarkerTypeTripVehicle;
// Initializing marker options.
GMTCMutableMarkerStyleOptions *markerStyleOptions =
[[GMTCMutableMarkerStyleOptions alloc] init];
markerStyleOptions.groundAnchor = kGMSMarkerDefaultGroundAnchor;
markerStyleOptions.icon = icon;
markerStyleOptions.zIndex = 100;
markerStyleOptions.isFlat = NO;
markerStyleOptions.isVisible = YES;
[consumerMapStyleCoordinator setMarkerStyleOptions:markerStyleOptions markerType:customizableMarkerType];
// Reset marker options to default values.
[consumerMapStyleCoordinator setMarkerStyleOptions:nil markerType:customizableMarkerType];
}
Dynamische Aktualisierungen der voraussichtlichen Ankunftszeit für Abholmarkierungen
Wenn Sie eine Abholmarkierung erstellen möchten, bei der die aktualisierte voraussichtliche Ankunftszeit regelmäßig dynamisch angezeigt wird, müssen Sie die Stiloptionen der Markierung für GMTCCustomizableMarkerType.tripPickupPoint
aktualisieren.
Beispiel
Swift
/** MapViewController.swift */
/// Updates the ETA every minute by creating a Timer that repeats every minute.
private func schedulePickupMarkerStyleUpdates() {
Timer.scheduledTimer(
timeInterval: 60.0, // Update marker ETA every minute.
target: self,
selector: #selector(updatePickupMarkerETA),
userInfo: nil,
repeats: true)
}
/// Updates the marker options for GMTCCustomizableMarkerType.tripPickupPoint for the current time.
@objc private func updatePickupMarkerETA() {
let consumerMapStyleCoordinator = mapView.consumerMapStyleCoordinator
let previousOptions = consumerMapStyleCoordinator.markerStyleOptions(for: .tripPickupPoint)
// Get updated ETA icon.
let updatedETAIcon = pickupIconForCurrentTime()
let markerStyleOptions = GMTCMutableMarkerStyleOptions()
markerStyleOptions.groundAnchor = kGMSMarkerDefaultGroundAnchor
markerStyleOptions.icon = updatedETAIcon
markerStyleOptions.zIndex = 100
markerStyleOptions.isFlat = false
markerStyleOptions.isVisible = true
consumerMapStyleCoordinator.setMarkerStyleOptions(markerStyleOptions, markerType: .tripPickupPoint)
}
Objective-C
/** MapViewController.m */
/** Updates the ETA every minute by creating an NSTimer that repeats every minute. */
- (void)schedulePickupMarkerStyleUpdates {
[NSTimer scheduledTimerWithTimeInterval:60.0 // Update marker ETA every minute.
target:self
selector:@selector(updatePickupMarkerETA)
userInfo:nil
repeats:YES];
}
/** Updates the marker options for GMTCCustomizableMarkerTypeTripPickupPoint for the current time. */
- (void)updatePickupMarkerETA {
GMTCConsumerMapStyleCoordinator *consumerMapStyleCoordinator = [_mapView consumerMapStyleCoordinator];
GMTCMarkerStyleOptions *previousOptions = [consumerMapStyleCoordinator markerStyleOptionsForType:GMTCCustomizableMarkerTypeTripPickupPoint];
// Get updated ETA icon.
UIImage *updatedETAIcon = [self pickupIconForCurrentTime];
GMTCMutableMarkerStyleOptions *markerStyleOptions =
[[GMTCMutableMarkerStyleOptions alloc] init];
markerStyleOptions.groundAnchor = kGMSMarkerDefaultGroundAnchor;
markerStyleOptions.icon = updatedETAIcon;
markerStyleOptions.zIndex = 100;
markerStyleOptions.isFlat = NO;
markerStyleOptions.isVisible = YES;
[consumerMapStyleCoordinator setMarkerStyleOptions:markerStyleOptions markerType:GMTCCustomizableMarkerTypeTripPickupPoint];
}
Benutzerdefinierte Polylinien
Die Anpassung von Polylinien wird mithilfe von GMTCConsumerMapStyleCoordinator#setPolylineStyleOptions(_:polylineType:)
festgelegt.
Das folgende Beispiel zeigt, wie Stiloptionen für Polylinien festgelegt werden:
Swift
/** ViewController.swift */
private func changePolylineStyleOptions(
polylineStyleOptions: GMTCPolylineStyleOptions?,
polylineType: GMTCPolylineType
) {
let styleCoordinator = mapView.consumerMapStyleCoordinator
styleCoordinator.setPolylineStyleOptions(polylineStyleOptions, polylineType: polylineType)
}
/* Setting custom polyline options will override the default values provided by the Consumer SDK.
The default values can be restored by calling setPolylineStyleOptions(_:polylineType:) with nil for the GMTCPolylineStyleOptions.
The active GMTCPolylineStyleOptions can be retrieved via */
private func retrievePolylineStyleOptions(for polylineType: GMTCPolylineType) {
let styleCoordinator = mapView.consumerMapStyleCoordinator
// The 'polylineStyleOptions' contains the stored style options for this polyline type.
let polylineStyleOptions = styleCoordinator.polylineStyleOptions(for: polylineType)
}
Objective-C
/** ViewController.h */
- (void)changePolylineStyleOptions:(nullable GMTCPolylineStyleOptions *)polylineStyleOptions
polylineType:(GMTCPolylineType)polylineType {
GMTCConsumerMapStyleCoordinator *styleCoordinator = _mapView.consumerMapStyleCoordinator;
[styleCoordinator setPolylineStyleOptions:polylineStyleOptions polylineType:polylineType];
}
/* Setting custom polyline options will override the default values provided by the Consumer SDK.
The default values can be restored by calling setPolylineStyleOptions:polylineType: with nil for the GMTCPolylineStyleOptions.
The active GMTCPolylineStyleOptions can be retrieved via */
- (void)retrievePolylineStyleOptionsForType:(GMTCPolylineType)polylineType {
GMTCConsumerMapStyleCoordinator *styleCoordinator = _mapView.consumerMapStyleCoordinator;
// The 'polylineStyleOptions' contains the stored style options for this polyline type.
GMTCPolylineStyleOptions *polylineStyleOptions = [styleCoordinator polylineStyleOptionsForType:polylineType];
}
Polylinientypen
Die folgenden Polylinientypen können angepasst werden:
GMTCPolylineType.activeRoute
GMTCPolylineType.remainingRoute
GMTCPolylineType.activeRoute
und GMTCPolylineType.remainingRoute
werden während des gesamten Fahrt- und Bestellfortschritts angezeigt. Die GMTCPolylineType.activeRoute
ist die Route, die das Fahrzeug zum nächsten Punkt des Fahrgasts zurücklegt, ob es sich dabei um den Abhol- oder den Absetzpunkt handelt. Die GMTCPolylineType.remainingRoute
ist der Abschnitt der Fahrt, der verbleibt, nachdem das Fahrzeug GMTCPolylineType.activeRoute
abgeschlossen hat.
Polylinieneigenschaften
Die für jede Polylinie verfügbaren anpassbaren Eigenschaften sind eine Teilmenge der in Google Maps PolylineOptions
bereitgestellten Eigenschaften.
Die GMTCPolylineStyleOptions
des Consumer SDK werden mit einem Initialisierer erstellt. Sie können unveränderlich oder änderbar sein, wenn Sie benutzerdefinierte Werte für ein Attribut angeben möchten. Für jedes Attribut werden Standardwerte angegeben.
Die folgenden Eigenschaften können angepasst werden:
color
width
isVisible
isTrafficEnabled
Wenn Sie isVisible
auf false
festlegen, entspricht dies einer Deaktivierung der Polylinie.
Standardmäßig ist isTrafficEnabled
auf false
festgelegt.
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.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.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];
}
Polylinien mit Verkehrserkennung
Die Verkehrsebene der Polylinie ist standardmäßig deaktiviert. Ist sie mit polylineStyleOptions.isTrafficEnabled = true
aktiviert, werden Segmente, die Abschnitte des ungewöhnlichen Verkehrs darstellen, als Route gezeichnet.
Die Verkehrslage wird in Form einer von vier Geschwindigkeiten dargestellt: GMTSSpeedType.noData
, GMTSSpeedType.normal
, GMTSSpeedType.slow
und GMTSSpeedType.trafficJam
.
Die Farbe für die einzelnen Geschwindigkeitsklassifizierungen kann mit setTrafficColorFor(_:color:)
angepasst werden.