Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snap to route #57

Merged
merged 9 commits into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions MapboxNavigation/RouteController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ open class RouteController: NSObject {
public var routeProgress: RouteProgress


/*
If true, the user puck is snapped to closest location on the route.
*/
public var snapUserToRoute = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

“Snap user to route” sounds like a command, so this sounds like an action method instead of a property that can be set. (In Objective-C, an action method and a property getter call can look identical.) Also, it sounds funny to say that we’re moving the user themselves; we’re actually moving the user location annotation.

Let’s call this property snapsUserLocationAnnotationToRoute. It’s a mouthful, but the developer won’t have to call it often anyways.



/*
Intializes a new `RouteController`.

Expand Down
8 changes: 0 additions & 8 deletions MapboxNavigationUI/NavigationMapView.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
//
// NavigationMapView.swift
// MapboxNavigation
//
// Created by Bobby Sudekum on 3/21/17.
// Copyright © 2017 Mapbox. All rights reserved.
//

import Foundation

public class NavigationMapView: MGLMapView {
Expand Down
11 changes: 8 additions & 3 deletions MapboxNavigationUI/RouteMapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,14 @@ extension RouteMapViewController: NavigationMapViewDelegate {
let route = routeController.routeProgress.route
guard let coordinates = route.coordinates else { return nil }

// Snap to route
let snappedCoordinate = closestCoordinate(on: coordinates, to: location.coordinate)
guard let newCoordinate = snappedCoordinate?.coordinate else { return nil }
var newCoordinate = location.coordinate
if routeController.snapUserToRoute {
// Snap to route
let snappedCoordinate = closestCoordinate(on: coordinates, to: location.coordinate)
if let coordinate = snappedCoordinate?.coordinate {
newCoordinate = coordinate
}
}

return CLLocation(coordinate: newCoordinate, altitude: location.altitude, horizontalAccuracy: location.horizontalAccuracy, verticalAccuracy: location.verticalAccuracy, course: location.course, speed: location.speed, timestamp: location.timestamp)
}
Expand Down