> For the complete documentation index, see [llms.txt](https://docs.mapbox.com/ios/maps/llms.txt)

# Add polyline annotations

![Show polyline annotations on a map.](https://docs.mapbox.com/ios/assets/ideal-img/maps-examples-add-polylines-annotations.aa3eb01.480.png)

This example demonstrates how to add a polygon annotation to a map using the [`PolylineAnnotation`](https://docs.mapbox.com/ios/maps/api/latest/documentation/mapboxmaps/polylineannotation/) and [`PolylineAnnotationManager`](https://docs.mapbox.com/ios/maps/api/latest/documentation/mapboxmaps/polylineannotationmanager/) classes in the **Mapbox Maps SDK for iOS**.

The example defines a `setupExample` function containing a predefined line as well as randomly generated lines on the map. The predefined line is customized with [`lineColor`](https://docs.mapbox.com/ios/maps/api/latest/documentation/mapboxmaps/polylineannotation/linecolor/) and [`lineWidth`](https://docs.mapbox.com/ios/maps/api/latest/documentation/mapboxmaps/polylineannotation/linewidth/) properties, while the random lines are customized with random colors. These annotations are appended to a list which is assigned to the annotation manager's [`annotations`](https://docs.mapbox.com/ios/maps/api/latest/documentation/mapboxmaps/polylineannotationmanager/annotations/) property.

> **Note: iOS Examples App Available**
> 
> This example code is part of the **Maps SDK for iOS Examples App**, a working iOS project [available on Github](https://github.com/mapbox/mapbox-maps-ios/tree/11.29.0/Sources/Examples). iOS developers are encouraged to run the examples app locally to interact with this example in an emulator and explore other features of the Maps SDK.
> 
> See our [Run the Maps SDK for iOS Examples App](https://docs.mapbox.com/help/tutorials/maps-sdk-ios-examples-app/) tutorial for step-by-step instructions.

**Swift**

Title: `LineAnnotationExample.swift`

[View on GitHub](https://github.com/mapbox/mapbox-maps-ios/blob/main/Sources/Examples/All%20Examples/Annotations/LineAnnotationExample.swift)

```swift
import UIKit
import MapboxMaps

final class ViewController: UIViewController {
    private var mapView: MapView!
    private var cancelables = Set<AnyCancelable>()

    override func viewDidLoad() {
        super.viewDidLoad()

        let cameraOptions = CameraOptions(center: CLLocationCoordinate2D(latitude: 0, longitude: 0), zoom: 2)
        let mapInitOptions = MapInitOptions(cameraOptions: cameraOptions)
        mapView = MapView(frame: view.bounds, mapInitOptions: mapInitOptions)
        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(mapView)

        // Allows the delegate to receive information about map events.
        mapView.mapboxMap.onMapLoaded.observeNext { [weak self] _ in

            // Set up the example
            self?.setupExample()

            
            
        }.store(in: &cancelables)
    }

    private func setupExample() {

        var annotations = [PolylineAnnotation]()
        let coordinates = [
            CLLocationCoordinate2DMake(-2.178992, -4.375974),
            CLLocationCoordinate2DMake(-4.107888, -7.639772),
            CLLocationCoordinate2DMake(2.798737, -11.439207)
        ]
        var lineAnnotation = PolylineAnnotation(lineCoordinates: coordinates)
        lineAnnotation.lineColor = StyleColor(.red)
        lineAnnotation.lineWidth = 5.0
        annotations.append(lineAnnotation)

        // random add lines across the globe
        var randomCoordinates = [CLLocationCoordinate2D]()
        for _ in 0..<400 {
            randomCoordinates.append(.random)
        }

        for chunk in randomCoordinates.chunked(into: 8) {
            // Create the line annotation.
            var randomAnnotation = PolylineAnnotation(lineCoordinates: chunk)

            // Customize the style of the line annotation
            randomAnnotation.lineColor = StyleColor(red: .random(in: 0...255),
                                                    green: .random(in: 0...255),
                                                    blue: .random(in: 0...255),
                                                    alpha: 1)

            annotations.append(randomAnnotation)
        }

        // Create the PolylineAnnotationManager responsible for managing
        // this line annotations (and others if you so choose).
        // Annotation managers are kept alive by `AnnotationOrchestrator`
        // (`mapView.annotations`) until you explicitly destroy them
        // by calling `mapView.annotations.removeAnnotationManager(withId:)`
        let lineAnnnotationManager = mapView.annotations.makePolylineAnnotationManager()

        // Sync the annotation to the manager.
        lineAnnnotationManager.annotations = annotations
    }
}
```