Giter Site home page Giter Site logo

sssbohdan / swiftgooglepolyline Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mwoollard/swiftgooglepolyline

0.0 1.0 0.0 27 KB

Swift class extensions to encode/decode google coordinate polylines

License: MIT License

Objective-C 6.14% Swift 93.86%

swiftgooglepolyline's Introduction

Swift Google Polylines

Carthage compatible

Google's direction API provides points along route segments as an encoded string. For example the encoded string:

_p~iF~ps|U_ulLnnqC_mqNvxq`@

decodes to the coordinate points:

(38.5, -120.2), (40.7, -120.95), (43.252, -126.453)

Full details of the algorithm used can be found here.

The Swift extensions provided here support encoding and decoding to and from the String and MKPolyline classes.

Usage

Examples of usage

Decoding

Decoding throws GooglePolylineError.InvalidPolylineString if the string is not a valid Google polyline.

let googlePolyline = "`_p~iF~ps|U_ulLnnqC_mqNvxq`@"

// Get lazy sequence of CLLocationCoordinate2D 
let seq = try googlePolyline.makeCoordinateSequenceFromGooglePolyline()

// Get array of CLLocationCoordinate2D
let array = try googlePolyline.makeCoordinateArrayFromGooglePolyline()

// Get MKPolyline
let polyline = try googlePolyline.makeMKPolylineFromGooglePolyline()
let anotherPolyline = try MKPolyline(encodedGooglePolyline:googlePolyline)

Encoding

let googlePolyline = "`_p~iF~ps|U_ulLnnqC_mqNvxq`@"
let polyline = MKPolyline(encodedGooglePolyline:googlePolyline)
let points = [
    CLLocationCoordinate2D(latitude: 38.5, longitude: -120.2),
    CLLocationCoordinate2D(latitude: 40.7, longitude: -120.95),
    CLLocationCoordinate2D(latitude: 43.252, longitude: -126.453)
]

// Encode from sequence of CLLocationCoordinate2D
let encodedFromPoints = String(googlePolylineLocationCoordinateSequence:points)

// Encode from sequence of MKMapPoint
let encodedFromMapPoints = String(googlePolylineMapPointSequence:
	points.map { MKMapPointForCoordinate($0) })

// Encode from MKPolyline
let encodedFromPolyline = String(googlePolylineMKPolyline:polyline)
let anotherEncodedFromPolyline = polyline.googlePolyline

Credits

This library has been created by Mark Woollard and has been made available under the MIT License. Please see the LICENSE file for more information.

swiftgooglepolyline's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.