- August 28, 2025
- Mins Read
Polyline encoder / decoder in Swift
CLLocationCoordinate2D array to a polylineCLLocationCoordinate2DCLLocation array to a polylineCLLocationMKPolylineGMSPolylineNote: The last version to support Swift 2.3 is the 3.x series. 4.0 will only support Swift 3.x.
To use this library in your project you can use CocoaPods, Carthage, and/or integrate it manually :
You can integrate Polyline in your Podfile like this:
pod ‘Polyline’, ‘~> 4.0’
You can integrate Polyline in your Cartfile like this:
github “raphaelmor/Polyline” ~> 4.0
Using [CLLocationCoordinate2D] (recommended) :
let coordinates = [CLLocationCoordinate2D(latitude: 40.2349727, longitude: -3.7707443),
CLLocationCoordinate2D(latitude: 44.3377999, longitude: 1.2112933)]
let polyline = Polyline(coordinates: coordinates)
let encodedPolyline: String = polyline.encodedPolyline
// Or for a functional approach :
let encodedPolyline: String = encodeCoordinates(coordinates)
Using [CLLocation] :
let locations = [CLLocation(latitude: 40.2349727, longitude: -3.7707443),
CLLocation(latitude: 44.3377999, longitude: 1.2112933)]
let polyline = Polyline(locations: locations)
let encodedPolyline: String = polyline.encodedPolyline
// Or for a functional approach :
let encodedPolyline: String = encodeLocations(locations)
You can encode levels too :
let levels: [UInt32] = [0,1,2,255]
let polyline = Polyline(coordinates: coordinates, levels: levels)
let encodedLevels: String? = polyline.encodedLevels
// Or for a functional approach :
let encodedLevels: String = encodedLevels(levels)
You can decode to [CLLocationCoordinate2D] (recommended) :
let polyline = Polyline(encodedPolyline: “qkqtFbn_Vui`Xu`l]”)
let decodedCoordinates: [CLLocationCoordinate2D]? = polyline.coordinates
// Or for a functional approach :
let coordinates: [CLLocationCoordinate2D]? = decodePolyline(“qkqtFbn_Vui`Xu`l]”)
You can also decode to [CLLocation] :
let polyline = Polyline(encodedPolyline: “qkqtFbn_Vui`Xu`l]”)
let decodedLocations: [CLLocation]? = polyline.locations
// Or for a functional approach :
let locations: [CLLocation]? = decodePolyline(“qkqtFbn_Vui`Xu`l]”)
You can decode levels too :
let polyline = Polyline(encodedPolyline: “qkqtFbn_Vui`Xu`l]”, encodedLevels: “BA”)
let decodedLevels: [UInt32]? = polyline.levels
// Or for a functional approach :
let levels: [UInt32]? = decodeLevels(“BA”)
Default precision is 1e5 : 0.12345 (5 digit precision used by Google), but you can specify your own precision in all API methods (and functions).
// OSRM uses a 6 digit precision
let polyline = Polyline(encodedPolyline: “ak{hRak{hR”, precision: 1e6)
This package provides you with an easy way to show tooltips over any SwiftUI view, since Apple does not provide ...
SimpleToast is a simple, lightweight, flexible and easy to use library to show toasts / popup notifications inside iOS or ...
Create Toast Views with Minimal Effort in SwiftUI Using SSToastMessage. SSToastMessage enables you to effortlessly add toast notifications, alerts, and ...