قبلاز سفارشیسازی نشانگرها (یا چندخطیها)، ابتدا باید گزینههای سفارشیسازی واسط کاربر را مقداردهی اولیه کنید.
مقداردهی اولیه گزینههای سفارشیسازی واسط کاربر
فراخوانی توصیهشده که برای تنظیم اولیه گزینههای سفارشیسازی واسط کاربر استفاده میشود
در GMTCMapViewDelegate اعلام شده است. وقتی شیء GMTCMapView برای ارائه نقشه آماده باشد،
فراخوانی برگشتی mapViewDidInitialize راهاندازی میشود.
هماهنگکننده سبک مقداردهی اولیه شده است، اما هیچ عنصر میانای کاربری وجود ندارد.
Swift
/** ViewController.swift */
class ViewController: UIViewController, GMTCMapViewDelegate {
// MARK: - GMTCMapViewDelegate
func mapViewDidInitialize(_ mapview: GMTCMapView) {
// Set the UI customization options here.
}
}
آبجکتیو-سی
/** ViewController.m */
@interface ViewController () <GMTCMapViewDelegate>
#pragma mark GMTCMapViewDelegate
- (void)mapViewDidInitialize:(GMTCMapView *)mapview {
// Set the UI customization options here.
}
سفارشیسازی نشانگرها
مثال زیر از GMTCMapView برای سفارشیسازی سبکهای نشانگر استفاده میکند.
برای تنظیم نوع نشانگر و ویژگیهای آن، از
setMarkerStyleOptions(_:markerType:) استفاده کنید. گزینههای نشانگر سفارشی شما مقادیر پیشفرضی را که «کیت توسعه نرمافزار مصرفکننده» ارائه کرده است لغو میکند.
Swift
/** MapViewController.swift */
func updateMarkerUIOptions() {
let customizableMarkerType = GMTCCustomizableMarkerType.tripVehicle
let markerStyleOptions = GMTCMutableMarkerStyleOptions()
markerStyleOptions.groundAnchor = groundAnchor
markerStyleOptions.isVisible = true
markerStyleOptions.icon = icon
markerStyleOptions.zIndex = 100
markerStyleOptions.isFlat = false
let coordinator = self.mapView.consumerMapStyleCoordinator
coordinator.setMarkerStyleOptions(markerStyleOptions, markerType: customizableMarkerType)
}
/** 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)
}
آبجکتیو-سی
/** MapViewController.m */
- (void)updateMarkerUIOptions {
// The marker type that you would like to set custom UI options for.
GMTCCustomizableMarkerType customizableMarkerType = GMTCCustomizableMarkerTypeTripVehicle;
GMTCMutableMarkerStyleOptions *markerStyleOptions =
[[GMTCMutableMarkerStyleOptions alloc] init];
markerStyleOptions.groundAnchor = groundAnchor;
markerStyleOptions.isVisible = YES;
markerStyleOptions.icon = icon;
markerStyleOptions.zIndex = 100;
markerStyleOptions.isFlat = NO;
[[_mapView consumerMapStyleCoordinator] setMarkerStyleOptions:markerStyleOptions markerType:customizableMarkerType];
}
/** 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];
}
انواع نشانگر
میتوانید نشانگرهای زیر را سفارشیسازی کنید:
GMTCCustomizableMarkerType.unknownGMTCCustomizableMarkerType.tripPickupPointGMTCCustomizableMarkerType.tripDropoffPointGMTCCustomizableMarkerType.tripVehicleGMTCCustomizableMarkerType.intermediateDestination
از GMTCCustomizableMarkerType.tripPickupPoint،
GMTCCustomizableMarkerType.intermediateDestination، و
GMTCCustomizableMarkerType.tripDropoffPoint برای سفارشیسازی نقاطمسیر هنگام
همرسانی سفر استفاده کنید.
از GMTCCustomizableMarkerType.tripVehicle برای سفارشیسازی کردن نماد خودرو
هنگام همرسانی سفر استفاده کنید. نماد نشانگر براساس نوع واقعی خودرو برای سفر تغییر نمیکند.
گزینههای نشانگر
ویژگیهای قابلسفارشیسازی دردسترس برای هر نشانگر زیرمجموعهای از
ویژگیهای ارائهشده توسط Google Maps
MarkerOptions است.
کیت توسعه نرمافزار مصرفکننده
GMTCMarkerStyleOptions
ویژگیهای زیر را دارد:
- ساختهشده بااستفاده از مقداردهی اولیه
- پساز ساخته شدن، تغییرناپذیر است.
- مقادیر پیشفرض داشته باشد، بنابراین فقط باید مقادیر سفارشی را مشخص کنید.
میتوانید ویژگیهای زیر را سفارشیسازی کنید:
groundAnchorisVisible: برای خاموش کردن نشانگر،isVisibleرا روی false تنظیم کنید. باید دادههای کافی ارائه شود تا بتوانید از عنصر میانای کاربر خودتان بهجای آن استفاده کنید.iconViewiconzIndexisFlat
مثال
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)
}
آبجکتیو-سی
/** 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];
}
بهروزرسانیهای پویای زمان تقریبی ورود برای نشانگرهای سوار شدن
برای ایجاد نشانگر سوار کردن که بهطور دورهای زمان تخمینی رسیدن بهروزرسانیشده را بهصورت پویا نشان میدهد،
گزینههای سبک نشانگر را برای
GMTCCustomizableMarkerType.tripPickupPoint بهروز کنید.
مثال
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)
}
آبجکتیو-سی
/** 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];
}