//// CustomAnnotationModels.swift//importMapbox// MGLAnnotation protocol reimplementationclassCustomPointAnnotation:NSObject,MGLAnnotation{// As a reimplementation of the MGLAnnotation protocol, we have to add mutable coordinate and (sub)title properties ourselves.varcoordinate:CLLocationCoordinate2Dvartitle:String?varsubtitle:String?// Custom properties that we will use to customize the annotation's image.varimage:UIImage?varreuseIdentifier:String?init(coordinate:CLLocationCoordinate2D,title:String?,subtitle:String?){self.coordinate=coordinateself.title=titleself.subtitle=subtitle}}// MGLPolyline subclassclassCustomPolyline:MGLPolyline{// Because this is a subclass of MGLPolyline, there is no need to redeclare its properties.// Custom property that we will use when drawing the polyline.varcolor:UIColor?}

//
// CustomAnnotationModels.h
//
@importMapbox;// MGLAnnotation protocol reimplementation
@interfaceCustomPointAnnotation:NSObject<MGLAnnotation>// As a reimplementation of the MGLAnnotation protocol, we have to add mutable coordinate and (sub)title properties ourselves.
@property(nonatomic,assign)CLLocationCoordinate2Dcoordinate;@property(nonatomic,copy,nullable)NSString*title;@property(nonatomic,copy,nullable)NSString*subtitle;// Custom properties that we will use to customize the annotation's image.
@property(nonatomic,copy,nonnull)UIImage*image;@property(nonatomic,copy,nonnull)NSString*reuseIdentifier;@end@implementationCustomPointAnnotation@end// MGLPolyline subclass
@interfaceCustomPolyline:MGLPolyline// Because this is a subclass of MGLPolyline, there is no need to redeclare its properties.
// Custom property that we will use when drawing the polyline.
@property(nonatomic,strong,nullable)UIColor*color;@end@implementationCustomPolyline@end

Swift 4Objective-C

//// CustomAnnotationModelViewController.swift//importMapboxclassCustomAnnotationModelViewController:UIViewController,MGLMapViewDelegate{overridefuncviewDidLoad(){super.viewDidLoad()letmapView=MGLMapView(frame:view.bounds)mapView.autoresizingMask=[.flexibleWidth,.flexibleHeight]mapView.styleURL=MGLStyle.lightStyleURL(withVersion:9)mapView.tintColor=.darkGraymapView.zoomLevel=1mapView.delegate=selfview.addSubview(mapView)// Polyline// Create a coordinates array with all of the coordinates for our polyline.varcoordinates=[CLLocationCoordinate2D(latitude:35,longitude:-25),CLLocationCoordinate2D(latitude:20,longitude:-30),CLLocationCoordinate2D(latitude:0,longitude:-25),CLLocationCoordinate2D(latitude:-15,longitude:0),CLLocationCoordinate2D(latitude:-45,longitude:10),CLLocationCoordinate2D(latitude:-45,longitude:40),]letpolyline=CustomPolyline(coordinates:&coordinates,count:UInt(coordinates.count))// Set the custom `color` property, later used in the `mapView:strokeColorForShapeAnnotation:` delegate method.polyline.color=.darkGray// Add the polyline to the map. Note that this method name is singular.mapView.addAnnotation(polyline)// Point Annotations// Add a custom point annotation for every coordinate (vertex) in the polyline.varpointAnnotations=[CustomPointAnnotation]()forcoordinateincoordinates{letcount=pointAnnotations.count+1letpoint=CustomPointAnnotation(coordinate:coordinate,title:"Custom Point Annotation \(count)",subtitle:nil)// Set the custom `image` and `reuseIdentifier` properties, later used in the `mapView:imageForAnnotation:` delegate method.// Create a unique reuse identifier for each new annotation image.point.reuseIdentifier="customAnnotation\(count)"// This dot image grows in size as more annotations are added to the array.point.image=dot(size:5*count)// Append each annotation to the array, which will be added to the map all at once.pointAnnotations.append(point)}// Add the point annotations to the map. This time the method name is plural.// If you have multiple annotations to add, batching their addition to the map is more efficient.mapView.addAnnotations(pointAnnotations)}funcdot(size:Int)->UIImage{letfloatSize=CGFloat(size)letrect=CGRect(x:0,y:0,width:floatSize,height:floatSize)letstrokeWidth:CGFloat=1UIGraphicsBeginImageContextWithOptions(rect.size,false,UIScreen.main.scale)letovalPath=UIBezierPath(ovalIn:rect.insetBy(dx:strokeWidth,dy:strokeWidth))UIColor.darkGray.setFill()ovalPath.fill()UIColor.white.setStroke()ovalPath.lineWidth=strokeWidthovalPath.stroke()letimage:UIImage=UIGraphicsGetImageFromCurrentImageContext()!UIGraphicsEndImageContext()returnimage}// MARK: - MGLMapViewDelegate methodsfuncmapView(_mapView:MGLMapView,imageForannotation:MGLAnnotation)->MGLAnnotationImage?{ifletpoint=annotationas?CustomPointAnnotation,letimage=point.image,letreuseIdentifier=point.reuseIdentifier{ifletannotationImage=mapView.dequeueReusableAnnotationImage(withIdentifier:reuseIdentifier){// The annotatation image has already been cached, just reuse it.returnannotationImage}else{// Create a new annotation image.returnMGLAnnotationImage(image:image,reuseIdentifier:reuseIdentifier)}}// Fallback to the default marker image.returnnil}funcmapView(_mapView:MGLMapView,strokeColorForShapeAnnotationannotation:MGLShape)->UIColor{ifletannotation=annotationas?CustomPolyline{// Return orange if the polyline does not have a custom color.returnannotation.color??.orange}// Fallback to the default tint color.returnmapView.tintColor}funcmapView(_mapView:MGLMapView,annotationCanShowCalloutannotation:MGLAnnotation)->Bool{returntrue}}

//
// CustomAnnotationModelViewController.m
//
#import "CustomAnnotationModelViewController.h"
#import "CustomAnnotationModels.h"
@importMapbox;@interfaceCustomAnnotationModelViewController()<MGLMapViewDelegate>@end@implementationCustomAnnotationModelViewController-(void)viewDidLoad{[superviewDidLoad];MGLMapView*mapView=[[MGLMapViewalloc]initWithFrame:self.view.bounds];mapView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;mapView.styleURL=[MGLStylelightStyleURLWithVersion:9];mapView.tintColor=[UIColordarkGrayColor];mapView.zoomLevel=1;mapView.delegate=self;[self.viewaddSubview:mapView];// Polyline
// Create a coordinates array with all of the coordinates for our polyline.
CLLocationCoordinate2Dcoordinates[]={CLLocationCoordinate2DMake(35,-25),CLLocationCoordinate2DMake(20,-30),CLLocationCoordinate2DMake(0,-25),CLLocationCoordinate2DMake(-15,0),CLLocationCoordinate2DMake(-45,10),CLLocationCoordinate2DMake(-45,40),};NSUIntegernumberOfCoordinates=sizeof(coordinates)/sizeof(CLLocationCoordinate2D);CustomPolyline*polyline=[CustomPolylinepolylineWithCoordinates:coordinatescount:numberOfCoordinates];//
// Set the custom `color` property, later used in the `mapView:strokeColorForShapeAnnotation:` delegate method.
polyline.color=[UIColordarkGrayColor];// Add the polyline to the map. Note that this method name is singular.
[mapViewaddAnnotation:polyline];// Point Annotations
// Add a custom point annotation for every coordinate (vertex) in the polyline.
NSMutableArray*pointAnnotations=[NSMutableArrayarrayWithCapacity:numberOfCoordinates];for(NSUIntegeri=0;i<numberOfCoordinates;i++){NSUIntegercount=pointAnnotations.count+1;CustomPointAnnotation*point=[[CustomPointAnnotationalloc]init];point.coordinate=coordinates[i];point.title=[NSStringstringWithFormat:@"Custom Point Annotation %lu",(unsignedlong)count];// Set the custom `image` and `reuseIdentifier` properties, later used in the `mapView:imageForAnnotation:` delegate method.
// Create a unique reuse identifier for each new annotation image.
point.reuseIdentifier=[NSStringstringWithFormat:@"customAnnotation%lu",(unsignedlong)count];// This dot image grows in size as more annotations are added to the array.
point.image=[selfdotWithSize:(5*count)];// Append each annotation to the array, which will be added to the map all at once.
[pointAnnotationsaddObject:point];}// Add the point annotations to the map. This time the method name is plural.
// If you have multiple annotations to add, batching their addition to the map is more efficient.
[mapViewaddAnnotations:pointAnnotations];}-(UIImage*)dotWithSize:(NSUInteger)size{size=(CGFloat)size;CGRectrect=CGRectMake(0,0,size,size);CGFloatstrokeWidth=1;UIGraphicsBeginImageContextWithOptions(rect.size,NO,[[UIScreenmainScreen]scale]);UIBezierPath*ovalPath=[UIBezierPathbezierPathWithOvalInRect:CGRectInset(rect,strokeWidth,strokeWidth)];[UIColor.darkGrayColorsetFill];[ovalPathfill];[UIColor.whiteColorsetStroke];ovalPath.lineWidth=strokeWidth;[ovalPathstroke];UIImage*image=UIGraphicsGetImageFromCurrentImageContext();UIGraphicsEndImageContext();returnimage;}#pragma mark - MGLMapViewDelegate methods
-(MGLAnnotationImage*)mapView:(MGLMapView*)mapViewimageForAnnotation:(id<MGLAnnotation>)annotation{if([annotationisKindOfClass:[CustomPointAnnotationclass]]){CustomPointAnnotation*point=(CustomPointAnnotation*)annotation;MGLAnnotationImage*annotationImage=[mapViewdequeueReusableAnnotationImageWithIdentifier:point.reuseIdentifier];if(annotationImage){// The annotatation image has already been cached, just reuse it.
returnannotationImage;}elseif(point.image&&point.reuseIdentifier){// Create a new annotation image.
return[MGLAnnotationImageannotationImageWithImage:point.imagereuseIdentifier:point.reuseIdentifier];}}// Fallback to the default marker image.
returnnil;}-(UIColor*)mapView:(MGLMapView*)mapViewstrokeColorForShapeAnnotation:(MGLShape*)annotation{if([annotationisKindOfClass:[CustomPolylineclass]]){// Return orange if the polyline does not have a custom color.
return[(CustomPolyline*)annotationcolor]?:[UIColororangeColor];}// Fallback to the default tint color.
returnmapView.tintColor;}-(BOOL)mapView:(MGLMapView*)mapViewannotationCanShowCallout:(id<MGLAnnotation>)annotation{returnYES;}@end