GooglePolyline is a Swift package that provides functionality for encoding and decoding polylines as per Google’s Encoded Polyline Algorithm. It offers the ability to simplify the polyline with a specified simplification factor, which can either be a direct value or automatically calculated based on a desired maximum length.
Features
Encodes an array of CLLocation instances into a string that represents a Google Encoded Polyline.
Decodes a string that represents a Google Encoded Polyline into an array of CLLocation instances.
Decodes a string that represents a Google Encoded Polyline into a MKPolyline.
Simplifies the polyline using the Douglas-Peucker algorithm.
Installation
Swift Package Manager
Add the following line to your dependencies in your Package.swift file:
You can then encode and decode polylines as follows:
let locations: [CLLocation] = ...
let encodedPolyline = googlePolyline.encode(locations: locations)
let decodedLocations = googlePolyline.decode(polyline: encodedPolyline)
let decodedMKPolyline = googlePolyline.decodeToMKPolyline(polyline: encodedPolyline)
Implementation Details
The GooglePolyline class provides the primary functionality. You can control the simplification of the polyline by specifying a SimplificationFactor when initializing the class. This can be set to .automatic(maxLength: Int) to automatically calculate the simplification factor based on a maximum length, or .value(Double) to directly set the factor.
The encode(locations:) function takes an array of CLLocation instances and returns a string representing the Google Encoded Polyline. The decode(polyline:) function does the reverse, returning an array of CLLocation instances from an encoded polyline string. decodeToMKPolyline(polyline:) also decodes the polyline but returns a MKPolyline instance.
Legal
Google is a trademark of Google LLC. This software is neither endorsed nor otherwise related to Google LLC apart from being based on Google’s polyline encoding algorithm.
GooglePolyline
GooglePolyline is a Swift package that provides functionality for encoding and decoding polylines as per Google’s Encoded Polyline Algorithm. It offers the ability to simplify the polyline with a specified simplification factor, which can either be a direct value or automatically calculated based on a desired maximum length.
Features
CLLocationinstances into a string that represents a Google Encoded Polyline.CLLocationinstances.MKPolyline.Installation
Swift Package Manager
Add the following line to your dependencies in your
Package.swiftfile:Then, include
"GooglePolyline"as a dependency for your target:Usage
Import the package in your Swift file:
Create an instance of
GooglePolyline:You can then encode and decode polylines as follows:
Implementation Details
The
GooglePolylineclass provides the primary functionality. You can control the simplification of the polyline by specifying aSimplificationFactorwhen initializing the class. This can be set to.automatic(maxLength: Int)to automatically calculate the simplification factor based on a maximum length, or.value(Double)to directly set the factor.The
encode(locations:)function takes an array ofCLLocationinstances and returns a string representing the Google Encoded Polyline. Thedecode(polyline:)function does the reverse, returning an array ofCLLocationinstances from an encoded polyline string.decodeToMKPolyline(polyline:)also decodes the polyline but returns aMKPolylineinstance.Legal
Google is a trademark of Google LLC. This software is neither endorsed nor otherwise related to Google LLC apart from being based on Google’s polyline encoding algorithm.