Skip to content

Commit

Permalink
Decode polyline6
Browse files Browse the repository at this point in the history
  • Loading branch information
frederoni committed Jun 25, 2018
1 parent 3faf9f0 commit 363cef9
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
binary "https://www.mapbox.com/ios-sdk/Mapbox-iOS-SDK.json" "3.7.6"
binary "https://www.mapbox.com/ios-sdk/Mapbox-iOS-SDK.json" "3.7.8"
github "AliSoftware/OHHTTPStubs" "6.1.0"
github "raphaelmor/Polyline" "v4.2.0"
8 changes: 8 additions & 0 deletions MapboxDirections.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
358D48871E2EAB4500F32A65 /* MBRouteOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 358D48851E2EAB4500F32A65 /* MBRouteOptions.m */; };
358D48881E2EAB4500F32A65 /* MBRouteOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 358D48851E2EAB4500F32A65 /* MBRouteOptions.m */; };
358D48891E2EAB4500F32A65 /* MBRouteOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 358D48851E2EAB4500F32A65 /* MBRouteOptions.m */; };
35CEAF7020DC466E000308F8 /* v5_driving_dc_polyline6.json in Resources */ = {isa = PBXBuildFile; fileRef = 35CEAF6F20DC466E000308F8 /* v5_driving_dc_polyline6.json */; };
35CEAF7120DC466E000308F8 /* v5_driving_dc_polyline6.json in Resources */ = {isa = PBXBuildFile; fileRef = 35CEAF6F20DC466E000308F8 /* v5_driving_dc_polyline6.json */; };
35CEAF7220DC466E000308F8 /* v5_driving_dc_polyline6.json in Resources */ = {isa = PBXBuildFile; fileRef = 35CEAF6F20DC466E000308F8 /* v5_driving_dc_polyline6.json */; };
35EFD00B207DFACA00BF3873 /* MBVisualInstruction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35EFD00A207DFACA00BF3873 /* MBVisualInstruction.swift */; };
35EFD00C207DFACA00BF3873 /* MBVisualInstruction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35EFD00A207DFACA00BF3873 /* MBVisualInstruction.swift */; };
35EFD00D207DFACA00BF3873 /* MBVisualInstruction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35EFD00A207DFACA00BF3873 /* MBVisualInstruction.swift */; };
Expand Down Expand Up @@ -260,6 +263,7 @@
/* Begin PBXFileReference section */
358D48801E2EAB2500F32A65 /* MBRouteOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MBRouteOptions.h; sourceTree = "<group>"; };
358D48851E2EAB4500F32A65 /* MBRouteOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MBRouteOptions.m; sourceTree = "<group>"; };
35CEAF6F20DC466E000308F8 /* v5_driving_dc_polyline6.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = v5_driving_dc_polyline6.json; sourceTree = "<group>"; };
35EFD00A207DFACA00BF3873 /* MBVisualInstruction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MBVisualInstruction.swift; sourceTree = "<group>"; };
8D381B601FD9F5B1008D5A58 /* noDestinationName.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = noDestinationName.json; sourceTree = "<group>"; };
8D381B621FDB01D1008D5A58 /* apiDestinationName.json */ = {isa = PBXFileReference; explicitFileType = text.json; path = apiDestinationName.json; sourceTree = "<group>"; };
Expand Down Expand Up @@ -580,6 +584,7 @@
children = (
DA737EE31D05F91E005BDA16 /* v5_driving_dc_geojson.json */,
DAC05F1B1CFC1E5300FA0071 /* v5_driving_dc_polyline.json */,
35CEAF6F20DC466E000308F8 /* v5_driving_dc_polyline6.json */,
);
name = V5;
path = v5;
Expand Down Expand Up @@ -1007,6 +1012,7 @@
8D381B661FDB089E008D5A58 /* apiDestinationName.json in Resources */,
8D381B641FDB0898008D5A58 /* noDestinationName.json in Resources */,
C5DAACB5201AA9A7001F9261 /* match.json in Resources */,
35CEAF7120DC466E000308F8 /* v5_driving_dc_polyline6.json in Resources */,
DA737EEF1D06175B005BDA16 /* v4_driving_dc_geojson.json in Resources */,
C53A022B1E92C281009837BD /* annotation.json in Resources */,
);
Expand All @@ -1031,6 +1037,7 @@
8D381B671FDB089F008D5A58 /* apiDestinationName.json in Resources */,
8D381B651FDB0899008D5A58 /* noDestinationName.json in Resources */,
C5DAACB6201AA9A7001F9261 /* match.json in Resources */,
35CEAF7220DC466E000308F8 /* v5_driving_dc_polyline6.json in Resources */,
DA737EF01D06175B005BDA16 /* v4_driving_dc_geojson.json in Resources */,
C53A022C1E92C281009837BD /* annotation.json in Resources */,
);
Expand Down Expand Up @@ -1062,6 +1069,7 @@
8D381B611FD9F5B1008D5A58 /* noDestinationName.json in Resources */,
DA737EEE1D06175B005BDA16 /* v4_driving_dc_geojson.json in Resources */,
C5DAACB4201AA9A7001F9261 /* match.json in Resources */,
35CEAF7020DC466E000308F8 /* v5_driving_dc_polyline6.json in Resources */,
C5A3D3981E8188FE00D494A0 /* annotation.json in Resources */,
8D381B631FDB01D1008D5A58 /* apiDestinationName.json in Resources */,
);
Expand Down
2 changes: 1 addition & 1 deletion MapboxDirections/MBDirectionsResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Polyline
@objc(MBDirectionsResult)
open class DirectionsResult: NSObject, NSSecureCoding {

@objc internal init(options: DirectionsOptions, legs: [RouteLeg], distance: CLLocationDistance, expectedTravelTime: TimeInterval, coordinates: [CLLocationCoordinate2D]?, speechLocale: Locale?) {
@objc internal init(legs: [RouteLeg], distance: CLLocationDistance, expectedTravelTime: TimeInterval, coordinates: [CLLocationCoordinate2D]?, speechLocale: Locale?, options: DirectionsOptions) {
self.directionsOptions = options
self.legs = legs
self.distance = distance
Expand Down
37 changes: 22 additions & 15 deletions MapboxDirections/MBRoute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import Polyline
open class Route: DirectionsResult {
// MARK: Creating a Route

@objc internal init(routeOptions: RouteOptions, legs: [RouteLeg], distance: CLLocationDistance, expectedTravelTime: TimeInterval, coordinates: [CLLocationCoordinate2D]?, speechLocale: Locale?) {
super.init(options: routeOptions, legs: legs, distance: distance, expectedTravelTime: expectedTravelTime, coordinates: coordinates, speechLocale: speechLocale)
@objc internal override init(legs: [RouteLeg], distance: CLLocationDistance, expectedTravelTime: TimeInterval, coordinates: [CLLocationCoordinate2D]?, speechLocale: Locale?, options: DirectionsOptions) {
super.init(legs: legs, distance: distance, expectedTravelTime: expectedTravelTime, coordinates: coordinates, speechLocale: speechLocale, options: options)
}

/**
Expand All @@ -23,32 +23,39 @@ open class Route: DirectionsResult {
- parameter routeOptions: The `RouteOptions` used to create the request.
*/
@objc(initWithJSON:waypoints:routeOptions:)
public init(json: [String: Any], waypoints: [Waypoint], routeOptions: RouteOptions) {
public init(json: [String: Any], waypoints: [Waypoint], options: RouteOptions) {
// Associate each leg JSON with a source and destination. The sequence of destinations is offset by one from the sequence of sources.
let legInfo = zip(zip(waypoints.prefix(upTo: waypoints.endIndex - 1), waypoints.suffix(from: 1)),
json["legs"] as? [JSONDictionary] ?? [])
let legs = legInfo.map { (endpoints, json) -> RouteLeg in
RouteLeg(json: json, source: endpoints.0, destination: endpoints.1, profileIdentifier: routeOptions.profileIdentifier)
RouteLeg(json: json, source: endpoints.0, destination: endpoints.1, options: options)
}
let distance = json["distance"] as! Double
let expectedTravelTime = json["duration"] as! Double

var coordinates: [CLLocationCoordinate2D]?
switch json["geometry"] {
case let geometry as JSONDictionary:
coordinates = CLLocationCoordinate2D.coordinates(geoJSON: geometry)
case let geometry as String:
coordinates = decodePolyline(geometry, precision: 1e5)!
default:
coordinates = nil

switch options.shapeFormat {
case .geoJSON:
if let geometry = json["geometry"] as? JSONDictionary {
coordinates = CLLocationCoordinate2D.coordinates(geoJSON: geometry)
}
case .polyline:
if let geometry = json["geometry"] as? String {
coordinates = decodePolyline(geometry, precision: 1e5)!
}
case .polyline6:
if let geometry = json["geometry"] as? String {
coordinates = decodePolyline(geometry, precision: 1e6)!
}
}

var speechLocale: Locale?
if let locale = json["voiceLocale"] as? String {
speechLocale = Locale(identifier: locale)
}

super.init(options: routeOptions, legs: legs, distance: distance, expectedTravelTime: expectedTravelTime, coordinates: coordinates, speechLocale: speechLocale)
super.init(legs: legs, distance: distance, expectedTravelTime: expectedTravelTime, coordinates: coordinates, speechLocale: speechLocale, options: options)
}

public var routeOptions: RouteOptions {
Expand All @@ -63,8 +70,8 @@ open class Route: DirectionsResult {
// MARK: Support for Directions API v4

internal class RouteV4: Route {
convenience override init(json: JSONDictionary, waypoints: [Waypoint], routeOptions: RouteOptions) {
let leg = RouteLegV4(json: json, source: waypoints.first!, destination: waypoints.last!, profileIdentifier: routeOptions.profileIdentifier)
convenience override init(json: JSONDictionary, waypoints: [Waypoint], options: RouteOptions) {
let leg = RouteLegV4(json: json, source: waypoints.first!, destination: waypoints.last!, options: options)
let distance = json["distance"] as! Double
let expectedTravelTime = json["duration"] as! Double

Expand All @@ -78,6 +85,6 @@ internal class RouteV4: Route {
coordinates = nil
}

self.init(routeOptions: routeOptions, legs: [leg], distance: distance, expectedTravelTime: expectedTravelTime, coordinates: coordinates, speechLocale: nil)
self.init(legs: [leg], distance: distance, expectedTravelTime: expectedTravelTime, coordinates: coordinates, speechLocale: nil, options: options)
}
}
17 changes: 8 additions & 9 deletions MapboxDirections/MBRouteLeg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ open class RouteLeg: NSObject, NSSecureCoding {

// MARK: Creating a Leg

@objc internal init(steps: [RouteStep], json: JSONDictionary, source: Waypoint, destination: Waypoint, profileIdentifier: MBDirectionsProfileIdentifier) {
@objc internal init(steps: [RouteStep], json: JSONDictionary, source: Waypoint, destination: Waypoint, options: RouteOptions) {
self.source = source
self.destination = destination
self.profileIdentifier = profileIdentifier
self.profileIdentifier = options.profileIdentifier
self.steps = steps
distance = json["distance"] as! Double
expectedTravelTime = json["duration"] as! Double
Expand Down Expand Up @@ -57,11 +57,10 @@ open class RouteLeg: NSObject, NSSecureCoding {
- parameter destination: The waypoint at the end of the leg.
- parameter profileIdentifier: The profile identifier used to request the routes.
*/
@objc(initWithJSON:source:destination:profileIdentifier:)
public convenience init(json: [String: Any], source: Waypoint, destination: Waypoint, profileIdentifier: MBDirectionsProfileIdentifier) {
let steps = (json["steps"] as? [JSONDictionary] ?? []).map { RouteStep(json: $0) }

self.init(steps: steps, json: json, source: source, destination: destination, profileIdentifier: profileIdentifier)
@objc(initWithJSON:source:destination:options:)
public convenience init(json: [String: Any], source: Waypoint, destination: Waypoint, options: RouteOptions) {
let steps = (json["steps"] as? [JSONDictionary] ?? []).map { RouteStep(json: $0, options: options) }
self.init(steps: steps, json: json, source: source, destination: destination, options: options)
}

public required init?(coder decoder: NSCoder) {
Expand Down Expand Up @@ -224,8 +223,8 @@ open class RouteLeg: NSObject, NSSecureCoding {
// MARK: Support for Directions API v4

internal class RouteLegV4: RouteLeg {
internal convenience init(json: JSONDictionary, source: Waypoint, destination: Waypoint, profileIdentifier: MBDirectionsProfileIdentifier) {
internal convenience init(json: JSONDictionary, source: Waypoint, destination: Waypoint, options: RouteOptions) {
let steps = (json["steps"] as? [JSONDictionary] ?? []).map { RouteStepV4(json: $0) }
self.init(steps: steps, json: json, source: source, destination: destination, profileIdentifier: profileIdentifier)
self.init(steps: steps, json: json, source: source, destination: destination, options: options)
}
}
4 changes: 2 additions & 2 deletions MapboxDirections/MBRouteOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ open class RouteOptions: DirectionsOptions {
let waypoints = namedWaypoints ?? self.waypoints

let routes = (json["routes"] as? [JSONDictionary])?.map {
Route(json: $0, waypoints: waypoints, routeOptions: self)
Route(json: $0, waypoints: waypoints, options: self)
}
return (waypoints, routes)
}
Expand Down Expand Up @@ -253,7 +253,7 @@ open class RouteOptionsV4: RouteOptions {
let intermediateWaypoints = (json["waypoints"] as! [JSONDictionary]).compactMap { Waypoint(geoJSON: $0) }
let waypoints = [sourceWaypoint] + intermediateWaypoints + [destinationWaypoint]
let routes = (json["routes"] as? [JSONDictionary])?.map {
RouteV4(json: $0, waypoints: waypoints, routeOptions: self)
RouteV4(json: $0, waypoints: waypoints, options: self)
}
return (waypoints, routes)
}
Expand Down
25 changes: 16 additions & 9 deletions MapboxDirections/MBRouteStep.swift
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,8 @@ open class RouteStep: NSObject, NSSecureCoding {
- parameter json: A JSON object that conforms to the [route step](https://www.mapbox.com/api-documentation/#routestep-object) format described in the Directions API documentation.
*/
@objc(initWithJSON:)
public convenience init(json: [String: Any]) {
@objc(initWithJSON:options:)
public convenience init(json: [String: Any], options: RouteOptions) {
let maneuver = json["maneuver"] as! JSONDictionary
let finalHeading = maneuver["bearing_after"] as? Double
let maneuverType = ManeuverType(description: maneuver["type"] as? String ?? "") ?? .none
Expand All @@ -632,13 +632,20 @@ open class RouteStep: NSObject, NSSecureCoding {
let name = json["name"] as! String

var coordinates: [CLLocationCoordinate2D]?
switch json["geometry"] {
case let geometry as JSONDictionary:
coordinates = CLLocationCoordinate2D.coordinates(geoJSON: geometry)
case let geometry as String:
coordinates = decodePolyline(geometry, precision: 1e5)!
default:
coordinates = nil

switch options.shapeFormat {
case .geoJSON:
if let geometry = json["geometry"] as? JSONDictionary {
coordinates = CLLocationCoordinate2D.coordinates(geoJSON: geometry)
}
case .polyline:
if let geometry = json["geometry"] as? String {
coordinates = decodePolyline(geometry, precision: 1e5)!
}
case .polyline6:
if let geometry = json["geometry"] as? String {
coordinates = decodePolyline(geometry, precision: 1e6)!
}
}

self.init(finalHeading: finalHeading, maneuverType: maneuverType, maneuverDirection: maneuverDirection, drivingSide: drivingSide, maneuverLocation: maneuverLocation, name: name, coordinates: coordinates, json: json)
Expand Down
4 changes: 2 additions & 2 deletions MapboxDirections/Match/MBMatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ open class Match: DirectionsResult {
self.confidence = confidence
self.tracepoints = tracepoints
self.waypointIndices = waypointIndices
super.init(options: matchOptions, legs: legs, distance: distance, expectedTravelTime: expectedTravelTime, coordinates: coordinates, speechLocale: speechLocale)
super.init(legs: legs, distance: distance, expectedTravelTime: expectedTravelTime, coordinates: coordinates, speechLocale: speechLocale, options: matchOptions)
}

/**
Expand All @@ -27,7 +27,7 @@ open class Match: DirectionsResult {
let legInfo = zip(zip(tracepoints.prefix(upTo: tracepoints.endIndex - 1), tracepoints.suffix(from: 1)),
json["legs"] as? [JSONDictionary] ?? [])
let legs = legInfo.map { (endpoints, json) -> RouteLeg in
RouteLeg(json: json, source: endpoints.0, destination: endpoints.1, profileIdentifier: matchOptions.profileIdentifier)
return RouteLeg(json: json, source: endpoints.0, destination: endpoints.1, options: RouteOptions(matchOptions: matchOptions))
}

let distance = json["distance"] as! Double
Expand Down
2 changes: 1 addition & 1 deletion MapboxDirections/Match/MBMatchOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ open class MatchOptions: DirectionsOptions {
}

let routes = (json["matchings"] as? [JSONDictionary])?.map {
Route(json: $0, waypoints: filteredWaypoints ?? waypoints, routeOptions: opts)
Route(json: $0, waypoints: filteredWaypoints ?? waypoints, options: opts)
}

return (waypoints, routes)
Expand Down
Loading

0 comments on commit 363cef9

Please sign in to comment.