Skip to main content

Draw Shapes

Drawing primitives programmatically on the map using MapLibre shape sources.


Draw a point​

func drawPoint(_ coord: CLLocationCoordinate2D) {
if let source = rawMapView?.style?.source(withIdentifier: "point-source") as? MLNShapeSource {
let point = MLNPointFeature()
point.coordinate = coord
source.shape = point
}
}

Draw multiple points​

func drawPoints(_ coords: [CLLocationCoordinate2D]) {
if let source = rawMapView?.style?.source(withIdentifier: "point-source") as? MLNShapeSource {
let features = coords.map { coord -> MLNPointFeature in
let feature = MLNPointFeature()
feature.coordinate = coord
return feature
}
source.shape = MLNShapeCollectionFeature(shapes: features)
}
}

Draw a line​

func drawLine(_ coords: [CLLocationCoordinate2D]) {
guard coords.count >= 2 else { return }
var mutableCoords = coords
if let source = rawMapView?.style?.source(withIdentifier: "line-source") as? MLNShapeSource {
source.shape = MLNPolylineFeature(coordinates: &mutableCoords, count: UInt(mutableCoords.count))
}
}

Draw a polygon​

func drawPolygon(_ coords: [CLLocationCoordinate2D]) {
guard coords.count >= 3 else { return }
var mutableCoords = coords
if let source = rawMapView?.style?.source(withIdentifier: "polygon-source") as? MLNShapeSource {
source.shape = MLNPolygonFeature(coordinates: &mutableCoords, count: UInt(mutableCoords.count))
}
}

See also: iOS Map SDK overview.