Mapbox Annotation Extension for iOS
The Mapbox Annotation Extension is a lightweight library you can use with the Mapbox Maps SDK for iOS to quickly add basic shapes, icons, and other annotations to a map.
This extension leverages the power of runtime styling with an object oriented approach to simplify the creation and styling of annotations.
⚠️ This product is currently in active beta development, is not intended for production usage. ⚠️
Installation
Using CocoaPods
To install the Mapbox Annotation Extension using CocoaPods:
Create a Podfile with the following specification:
pod 'MapboxAnnotationExtension', '0.0.1-beta.1'
Run pod repo update && pod install
and open the resulting Xcode workspace.
Using Carthage
Alternatively, to install the Mapbox Annotation Extension using Carthage:
Create a Cartfile with the following dependency:
github "mapbox/mapbox-annotation-extension" ~> 0.0.1-beta.1
Run carthage update --platform iOS
to build just the iOS dependencies.
Manual Installation
Download and unzip the latest release.
Drag MapboxAnnotationExtension.framework into your project’s Embedded Binaries section in the project editor. In the sheet that appears, make sure Copy items if needed
is checked, then select Finish.
Annotation extension examples
You can find a sample application within this project by running the annotationapp
target.
Usage
The Mapbox Annotation Extension supports the addition of circles, lines, polygons, and symbols. Each shape type has its own corresponding controller which manages the addition of multiple shape objects of the same type.
Since the map needs to be finished loading before adding shapes to it, all shapes should be added within the MGLMapView:didFinishLoadingStyle:
delegate method.
Circles (MGLCircleStyleAnnotation
)
Circles represent a coordinate point on a map with an associated radius, measured in pixels.
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
let circleAnnotationController = MGLCircleAnnotationController(mapView: self.mapView)
let circle = MGLCircleStyleAnnotation(center: CLLocationCoordinate2D(latitude: 59.31, longitude: 18.06), radius: 3.0, color: .blue)
circle.opacity = 0.5
circleAnnotationController.add(circle)
}
Lines (MGLLineStyleAnnotation
)
Lines represent a connected series of coordinates.
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
let lineAnnotationController = MGLLineAnnotationController(mapView: self.mapView)
let lineCoordinates = [
CLLocationCoordinate2D(latitude: 59, longitude: 18),
CLLocationCoordinate2D(latitude: 60, longitude: 20)
]
let line = MGLLineStyleAnnotation(coordinates: lineCoordinates, count: UInt(lineCoordinates.count))
line.color = .purple
lineAnnotationController.add(line)
}
Polygons (MGLPolygonStyleAnnotation
)
Polygons represent a connected series of coordinates, where the first and last coordinates are equal.
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
let polygonAnnotationController = MGLPolygonAnnotationController(mapView: self.mapView)
let polygonCoordinates = [
CLLocationCoordinate2DMake(59, 18),
CLLocationCoordinate2DMake(62, 19),
CLLocationCoordinate2DMake(54, 20),
CLLocationCoordinate2DMake(59, 18)
]
let polygon = MGLPolygonStyleAnnotation(coordinates: polygonCoordinates, count: UInt(polygonCoordinates.count))
polygon.fillOutlineColor = .red
polygonAnnotationController.add(line)
}
Symbols (MGLSymbolStyleAnnotation
)
Symbols represent a coordinate point on a map with a label and an associated optional icon image. An image must be supplied to the map style’s sprite before being able to assign it to be the icon image of a symbol.
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
let attraction = UIImage(named: "attraction")
if let styleImage = attraction {
self.mapView.style?.setImage(styleImage, forName: "attraction")
}
let symbolAnnotationController = MGLSymbolAnnotationController(mapView: self.mapView)
let symbol = MGLSymbolStyleAnnotation(coordinate: CLLocationCoordinate2DMake(59, 18));
symbol.iconImageName = "attraction"
symbol.text = "This is a cool place!"
symbol.textFontSize = 16
symbolAnnotationController.add(symbol)
}
Displaying callouts for style annotations
Style annotations support callouts that appear when the annotation is selected. Each callout can display additional information about its annotation.
1) To enable callouts for a style annotation, create a class property with the controller type you are enabling interaction for:
class ViewController: UIViewController {
var mapView: MGLMapView!
var circleAnnotationController: MGLCircleAnnotationController!
}
2) The style annotation must contain a title and must implement two delegate methods using the Maps SDK for iOS. The first method, annotationCanShowCallout
should return true
. The second method, viewFor annotation:
should return an empty
MGLAnnotationView
matching the size of the style annotation’s shape, as shown below.
extension ViewController : MGLMapViewDelegate {
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
circleAnnotationController = MGLCircleAnnotationController(mapView: self.mapView)
let circle = MGLCircleStyleAnnotation(center: CLLocationCoordinate2D(latitude: 59.31, longitude: 18.06), radius: 3.0, color: .blue)
circle.opacity = 0.5
circle.title = "Title"
circleAnnotationController.add(circle)
}
func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
return true;
}
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
return MGLAnnotationView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
}
}
Supported properties
Each MGLStyleAnnotation
class can be assigned a specific set of properties to configure its layout and style. Additionally, MGLAnnotationController
s also have properties that can assign a specific layout property to all of its child MGLStyleAnnotation
s.
All of these properties mirror the same values specified in the Mapbox Style Specification. Below is a list of supported properties on each class along with its corresponding property within the Mapbox Style Specification.
MGLCircleStyleAnnotation
MGLCircleAnnotationController
MGLLineStyleAnnotation
MGLLineAnnotationController
MGLPolygonStyleAnnotation
MGLPolygonAnnotationController
MGLSymbolStyleAnnotation
Icon images
iconScale
iconImageName
iconRotation
iconOffset
iconAnchor
iconOpacity
iconColor
iconHaloColor
iconHaloWidth
iconHaloBlur
Symbol text
text
fontNames
textFontSize
maximumTextWidth
textLetterSpacing
textJustification
textRadialOffset
textAnchor
textRotation
textTransform
textOffset
textOpacity
textColor
textHaloColor
textHaloWidth
textHaloBlur
Icon image & symbol text
MGLSymbolAnnotationController
Icon images
iconAllowsOverlap
iconIgnoresPlacement
iconOptional
iconPadding
iconPitchAlignment
iconRotationAlignment
iconTextFit
iconTranslation
iconTranslationAnchor
keepsIconUpright
Symbol text
textAllowsOverlap
textLineHeight
textOptional
textPadding
textPitchAlignment
textRotationAlignment
textVariableAnchor
textTranslation
textTranslationAnchor
keepsTextUpRight
maximumTextAngle
Icon image & symbol text
Support
These extensions are still in active development, and as such APIs are subject to rapid change. If you have questions or feedback, please open a new issue in this repository.
If you’d like to report a bug, please include as much information as possible so that we can quickly reproduce the issue.
For help with using the Mapbox Maps SDK for iOS, check out our documentation.