Skip to content

Commit

Permalink
v0.40.0
Browse files Browse the repository at this point in the history
Also made self weak in completion handlers to avoid reference cycles.
  • Loading branch information
1ec5 committed Jun 1, 2020
1 parent 4933956 commit 4ba6992
Show file tree
Hide file tree
Showing 12 changed files with 244 additions and 166 deletions.
26 changes: 17 additions & 9 deletions DocsCode/NavigationTutorial/NavigationTutorialViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import MapboxDirections
class ViewController: UIViewController, MGLMapViewDelegate {
// #-code-snippet: navigation vc-variables-swift
var mapView: NavigationMapView!
var directionsRoute: Route?
var routeOptions: NavigationRouteOptions?
var route: Route?
// #-end-code-snippet: navigation vc-variables-swift

// #-code-snippet: navigation view-did-load-swift
Expand Down Expand Up @@ -66,23 +67,27 @@ class ViewController: UIViewController, MGLMapViewDelegate {
let destination = Waypoint(coordinate: destination, coordinateAccuracy: -1, name: "Finish")

// Specify that the route is intended for automobiles avoiding traffic
let options = NavigationRouteOptions(waypoints: [origin, destination], profileIdentifier: .automobileAvoidingTraffic)
let routeOptions = NavigationRouteOptions(waypoints: [origin, destination], profileIdentifier: .automobileAvoidingTraffic)

// Generate the route object and draw it on the map
_ = Directions.shared.calculate(options) { [unowned self] (waypoints, routes, error) in
self.directionsRoute = routes?.first
_ = Directions.shared.calculate(routeOptions) { [weak self] (session, result) in
guard case let .success(response) = result, let route = response.routes?.first, let strongSelf = self else {
return
}
strongSelf.route = route
strongSelf.routeOptions = routeOptions
// Draw the route on the map after creating it
self.drawRoute(route: self.directionsRoute!)
strongSelf.drawRoute(route: route)
}
}
// #-end-code-snippet: navigation calculate-route-swift

// #-code-snippet: navigation draw-route-swift
func drawRoute(route: Route) {
guard route.coordinateCount > 0 else { return }
guard let routeShape = route.shape, routeShape.coordinates.count > 0 else { return }
// Convert the route’s coordinates into a polyline
var routeCoordinates = route.coordinates!
let polyline = MGLPolylineFeature(coordinates: &routeCoordinates, count: route.coordinateCount)
var routeCoordinates = routeShape.coordinates
let polyline = MGLPolylineFeature(coordinates: &routeCoordinates, count: UInt(routeCoordinates.count))

// If there's already a route line on the map, reset its shape to the new route
if let source = mapView.style?.source(withIdentifier: "route-source") as? MGLShapeSource {
Expand Down Expand Up @@ -110,7 +115,10 @@ class ViewController: UIViewController, MGLMapViewDelegate {

// Present the navigation view controller when the callout is selected
func mapView(_ mapView: MGLMapView, tapOnCalloutFor annotation: MGLAnnotation) {
let navigationViewController = NavigationViewController(for: directionsRoute!)
guard let route = route, let routeOptions = routeOptions else {
return
}
let navigationViewController = NavigationViewController(for: route, routeOptions: routeOptions)
navigationViewController.modalPresentationStyle = .fullScreen
self.present(navigationViewController, animated: true, completion: nil)
}
Expand Down
32 changes: 22 additions & 10 deletions Navigation-Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,13 @@
"${PODS_ROOT}/Target Support Files/Pods-Navigation-Examples/Pods-Navigation-Examples-frameworks.sh",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/Mapbox.framework",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/Mapbox.framework.dSYM",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/B8CEDF0B-90B7-325D-BA23-1B44AB41FEC9.bcsymbolmap",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/1182BB8D-8A58-30EC-8D04-5E89575ED646.bcsymbolmap",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/EF3F193B-8B19-3D7A-9C4D-CC7ACB8FD1B2.bcsymbolmap",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/AF8AFA46-95A0-3C36-89B4-0A23D7D0A613.bcsymbolmap",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/224E36C4-6533-3853-8416-A668FDC17C35.bcsymbolmap",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/E1D60A9C-E180-3412-BFDB-809E8A1C178E.bcsymbolmap",
"${PODS_ROOT}/MapboxAccounts/MapboxAccounts.framework",
"${BUILT_PRODUCTS_DIR}/MapboxCoreNavigation/MapboxCoreNavigation.framework",
"${BUILT_PRODUCTS_DIR}/MapboxDirections.swift/MapboxDirections.framework",
"${BUILT_PRODUCTS_DIR}/MapboxDirections/MapboxDirections.framework",
"${BUILT_PRODUCTS_DIR}/MapboxMobileEvents/MapboxMobileEvents.framework",
"${BUILT_PRODUCTS_DIR}/MapboxNavigation/MapboxNavigation.framework",
"${PODS_ROOT}/MapboxNavigationNative/MapboxNavigationNative.framework",
Expand All @@ -342,8 +345,11 @@
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Mapbox.framework",
"${DWARF_DSYM_FOLDER_PATH}/Mapbox.framework.dSYM",
"${BUILT_PRODUCTS_DIR}/B8CEDF0B-90B7-325D-BA23-1B44AB41FEC9.bcsymbolmap",
"${BUILT_PRODUCTS_DIR}/1182BB8D-8A58-30EC-8D04-5E89575ED646.bcsymbolmap",
"${BUILT_PRODUCTS_DIR}/EF3F193B-8B19-3D7A-9C4D-CC7ACB8FD1B2.bcsymbolmap",
"${BUILT_PRODUCTS_DIR}/AF8AFA46-95A0-3C36-89B4-0A23D7D0A613.bcsymbolmap",
"${BUILT_PRODUCTS_DIR}/224E36C4-6533-3853-8416-A668FDC17C35.bcsymbolmap",
"${BUILT_PRODUCTS_DIR}/E1D60A9C-E180-3412-BFDB-809E8A1C178E.bcsymbolmap",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxAccounts.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxCoreNavigation.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxDirections.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxMobileEvents.framework",
Expand Down Expand Up @@ -424,10 +430,13 @@
"${PODS_ROOT}/Target Support Files/Pods-DocsCode/Pods-DocsCode-frameworks.sh",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/Mapbox.framework",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/Mapbox.framework.dSYM",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/B8CEDF0B-90B7-325D-BA23-1B44AB41FEC9.bcsymbolmap",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/1182BB8D-8A58-30EC-8D04-5E89575ED646.bcsymbolmap",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/EF3F193B-8B19-3D7A-9C4D-CC7ACB8FD1B2.bcsymbolmap",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/AF8AFA46-95A0-3C36-89B4-0A23D7D0A613.bcsymbolmap",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/224E36C4-6533-3853-8416-A668FDC17C35.bcsymbolmap",
"${PODS_ROOT}/Mapbox-iOS-SDK/dynamic/E1D60A9C-E180-3412-BFDB-809E8A1C178E.bcsymbolmap",
"${PODS_ROOT}/MapboxAccounts/MapboxAccounts.framework",
"${BUILT_PRODUCTS_DIR}/MapboxCoreNavigation/MapboxCoreNavigation.framework",
"${BUILT_PRODUCTS_DIR}/MapboxDirections.swift/MapboxDirections.framework",
"${BUILT_PRODUCTS_DIR}/MapboxDirections/MapboxDirections.framework",
"${BUILT_PRODUCTS_DIR}/MapboxMobileEvents/MapboxMobileEvents.framework",
"${BUILT_PRODUCTS_DIR}/MapboxNavigation/MapboxNavigation.framework",
"${PODS_ROOT}/MapboxNavigationNative/MapboxNavigationNative.framework",
Expand All @@ -441,8 +450,11 @@
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Mapbox.framework",
"${DWARF_DSYM_FOLDER_PATH}/Mapbox.framework.dSYM",
"${BUILT_PRODUCTS_DIR}/B8CEDF0B-90B7-325D-BA23-1B44AB41FEC9.bcsymbolmap",
"${BUILT_PRODUCTS_DIR}/1182BB8D-8A58-30EC-8D04-5E89575ED646.bcsymbolmap",
"${BUILT_PRODUCTS_DIR}/EF3F193B-8B19-3D7A-9C4D-CC7ACB8FD1B2.bcsymbolmap",
"${BUILT_PRODUCTS_DIR}/AF8AFA46-95A0-3C36-89B4-0A23D7D0A613.bcsymbolmap",
"${BUILT_PRODUCTS_DIR}/224E36C4-6533-3853-8416-A668FDC17C35.bcsymbolmap",
"${BUILT_PRODUCTS_DIR}/E1D60A9C-E180-3412-BFDB-809E8A1C178E.bcsymbolmap",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxAccounts.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxCoreNavigation.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxDirections.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MapboxMobileEvents.framework",
Expand Down
33 changes: 21 additions & 12 deletions Navigation-Examples/Examples/Advanced.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Mapbox
class AdvancedViewController: UIViewController, MGLMapViewDelegate, CLLocationManagerDelegate, NavigationMapViewDelegate, NavigationViewControllerDelegate {

var mapView: NavigationMapView?
var routeOptions: NavigationRouteOptions?
var currentRoute: Route? {
get {
return routes?.first
Expand All @@ -21,8 +22,8 @@ class AdvancedViewController: UIViewController, MGLMapViewDelegate, CLLocationMa
var routes: [Route]? {
didSet {
guard let routes = routes, let current = routes.first else { mapView?.removeRoutes(); return }
mapView?.showRoutes(routes)
mapView?.showWaypoints(current)
mapView?.show(routes)
mapView?.showWaypoints(on: current)
}
}
var startButton: UIButton?
Expand Down Expand Up @@ -73,11 +74,11 @@ class AdvancedViewController: UIViewController, MGLMapViewDelegate, CLLocationMa


@objc func tappedButton(sender: UIButton) {
guard let route = currentRoute else { return }
guard let route = currentRoute, let routeOptions = routeOptions else { return }
// For demonstration purposes, simulate locations if the Simulate Navigation option is on.
let navigationService = MapboxNavigationService(route: route, simulating: simulationIsEnabled ? .always : .onPoorGPS)
let navigationService = MapboxNavigationService(route: route, routeOptions: routeOptions, simulating: simulationIsEnabled ? .always : .onPoorGPS)
let navigationOptions = NavigationOptions(navigationService: navigationService)
let navigationViewController = NavigationViewController(for: route, options: navigationOptions)
let navigationViewController = NavigationViewController(for: route, routeOptions: routeOptions, navigationOptions: navigationOptions)
navigationViewController.delegate = self

present(navigationViewController, animated: true, completion: nil)
Expand All @@ -97,14 +98,22 @@ class AdvancedViewController: UIViewController, MGLMapViewDelegate, CLLocationMa
let userWaypoint = Waypoint(location: userLocation, heading: mapView?.userLocation?.heading, name: "user")
let destinationWaypoint = Waypoint(coordinate: destination)

let options = NavigationRouteOptions(waypoints: [userWaypoint, destinationWaypoint])
let routeOptions = NavigationRouteOptions(waypoints: [userWaypoint, destinationWaypoint])

Directions.shared.calculate(options) { (waypoints, routes, error) in
guard let routes = routes else { return }
self.routes = routes
self.startButton?.isHidden = false
self.mapView?.showRoutes(routes)
self.mapView?.showWaypoints(self.currentRoute!)
Directions.shared.calculate(routeOptions) { [weak self] (session, result) in
switch result {
case .failure(let error):
print(error.localizedDescription)
case .success(let response):
guard let routes = response.routes, let strongSelf = self else {
return
}
strongSelf.routeOptions = routeOptions
strongSelf.routes = routes
strongSelf.startButton?.isHidden = false
strongSelf.mapView?.show(routes)
strongSelf.mapView?.showWaypoints(on: strongSelf.currentRoute!)
}
}
}

Expand Down
28 changes: 16 additions & 12 deletions Navigation-Examples/Examples/Basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ class BasicViewController: UIViewController {
let destination = CLLocationCoordinate2DMake(37.76556957793795, -122.42409811526268)
let options = NavigationRouteOptions(coordinates: [origin, destination])

Directions.shared.calculate(options) { (waypoints, routes, error) in
guard let route = routes?.first, error == nil else {
print(error!.localizedDescription)
return
Directions.shared.calculate(options) { [weak self] (session, result) in
switch result {
case .failure(let error):
print(error.localizedDescription)
case .success(let response):
guard let route = response.routes?.first, let strongSelf = self else {
return
}

// For demonstration purposes, simulate locations if the Simulate Navigation option is on.
let navigationService = MapboxNavigationService(route: route, routeOptions: options, simulating: simulationIsEnabled ? .always : .onPoorGPS)
let navigationOptions = NavigationOptions(navigationService: navigationService)
let navigationViewController = NavigationViewController(for: route, routeOptions: options, navigationOptions: navigationOptions)
navigationViewController.modalPresentationStyle = .fullScreen

strongSelf.present(navigationViewController, animated: true, completion: nil)
}

// For demonstration purposes, simulate locations if the Simulate Navigation option is on.
let navigationService = MapboxNavigationService(route: route, simulating: simulationIsEnabled ? .always : .onPoorGPS)
let navigationOptions = NavigationOptions(navigationService: navigationService)
let navigationViewController = NavigationViewController(for: route, options: navigationOptions)
navigationViewController.modalPresentationStyle = .fullScreen

self.present(navigationViewController, animated: true, completion: nil)
}
}
}
32 changes: 18 additions & 14 deletions Navigation-Examples/Examples/Custom-Destination-Marker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@ class CustomDestinationMarkerController: UIViewController {

let origin = CLLocationCoordinate2DMake(37.77440680146262, -122.43539772352648)
let destination = CLLocationCoordinate2DMake(37.76556957793795, -122.42409811526268)
let options = NavigationRouteOptions(coordinates: [origin, destination])
let routeOptions = NavigationRouteOptions(coordinates: [origin, destination])

Directions.shared.calculate(options) { (waypoints, routes, error) in
guard let route = routes?.first, error == nil else {
print(error!.localizedDescription)
return
Directions.shared.calculate(routeOptions) { [weak self] (session, result) in
switch result {
case .failure(let error):
print(error.localizedDescription)
case .success(let response):
guard let route = response.routes?.first, let strongSelf = self else {
return
}

// For demonstration purposes, simulate locations if the Simulate Navigation option is on.
let navigationService = MapboxNavigationService(route: route, routeOptions: routeOptions, simulating: simulationIsEnabled ? .always : .onPoorGPS)
let navigationOptions = NavigationOptions(navigationService: navigationService)
let navigationViewController = NavigationViewController(for: route, routeOptions: routeOptions, navigationOptions: navigationOptions)
navigationViewController.modalPresentationStyle = .fullScreen
navigationViewController.mapView?.delegate = strongSelf

strongSelf.present(navigationViewController, animated: true, completion: nil)
}

// For demonstration purposes, simulate locations if the Simulate Navigation option is on.
let navigationService = MapboxNavigationService(route: route, simulating: simulationIsEnabled ? .always : .onPoorGPS)
let navigationOptions = NavigationOptions(navigationService: navigationService)
let navigationViewController = NavigationViewController(for: route, options: navigationOptions)
navigationViewController.modalPresentationStyle = .fullScreen
navigationViewController.mapView?.delegate = self

self.present(navigationViewController, animated: true, completion: nil)
}
}
}
Expand Down
Loading

0 comments on commit 4ba6992

Please sign in to comment.