-
Notifications
You must be signed in to change notification settings - Fork 35
Add tutorial code from mapbox.com/help #127
Conversation
Podfile
Outdated
@@ -1,9 +1,10 @@ | |||
platform :ios, '8.0' | |||
platform :ios, '9.0' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We’ve been trying to keep this at the same minimum deployment target as the SDK, as one more check that we’re not breaking promised backwards compatibility. I’d like to keep it at 8, unless there’s absolutely no way not to.
Podfile
Outdated
use_frameworks! | ||
|
||
def shared_pods | ||
#pod 'Mapbox-iOS-SDK', '~> 3.6.3' | ||
pod 'Mapbox-iOS-SDK-symbols', :podspec => 'https://raw.githubusercontent.com/mapbox/mapbox-gl-native/ios-v3.6.4/platform/ios/Mapbox-iOS-SDK-symbols.podspec' | ||
pod 'MapboxNavigation', '~> 0.10' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is apparently problematic — tests are failing because CocoaPods is trying to install Mapbox.framework twice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shared_pods
is installed across all targets, while MapboxNavigation
is only needed for DocsCode.
@@ -0,0 +1,128 @@ | |||
// #-code-snippet: navigation dependencies-swift | |||
import Mapbox | |||
import MapboxCoreNavigation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to import all four of these? At least MapboxCoreNavigation
should be able to be implicitly imported.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks likes it is required from testing it out 😐
a262b67
to
3f3dbb6
Compare
It seems like the build is failing because the DocsCode target is using Swift 4. I'll need to wait until mapbox/mapbox-navigation-ios#663 lands and then update the dependency here - once that is done this should pass on BitRise. |
Edit magic comment placement syntax for DDS tutorial
6e3a94c
to
bfd8f60
Compare
|
||
// #-code-snippet: dds-circle add-style-layer-swift | ||
layer.circleColor = MGLStyleValue<UIColor>(interpolationMode: .interval, | ||
sourceStops: stops, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: indent these lines to make it clear that they are children of L52. Up to you whether or not to align the arguments or not — I don’t think Swift usually does, while ObjC does.
// #-end-code-snippet: dds-circle add-vector-source-objc | ||
|
||
// #-code-snippet: dds-circle add-circle-layer-objc | ||
MGLCircleStyleLayer *layer = [[MGLCircleStyleLayer alloc] initWithIdentifier: @"tree-style" source:source]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: ObjC method calls don’t typically have a space before the parameter. E.g., initWithIdentifier:@"tree-style”
.
There are other instances of unstylish spaces — it would be good to be consistent with this.
toDestination :(CLLocationCoordinate2D)destination | ||
completion:(void(^)(MBRoute *_Nullable route, NSError *_Nullable error))completion { | ||
|
||
MBWaypoint *originWaypoint = [[MBWaypoint alloc] initWithCoordinate:origin coordinateAccuracy:-1 name:@"Start"]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does coordinateAccuracy:-1
do? Is -1
the correct value/type? Is this worth explaining to the reader?
MBNavigationRouteOptions *options = [[MBNavigationRouteOptions alloc] initWithWaypoints:@[originWaypoint, destinationWaypoint] profileIdentifier:MBDirectionsProfileIdentifierAutomobileAvoidingTraffic]; | ||
|
||
// Generate the route object and draw it on the map | ||
NSURLSessionDataTask *task = [[MBDirections sharedDirections] calculateDirectionsWithOptions:options completionHandler:^(NSArray<MBWaypoint *> * _Nullable waypoints, NSArray<MBRoute *> * _Nullable routes, NSError * _Nullable error){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a very long line — can it be broken into something easier to read? The _Nullable
annotations could be removed, though maybe they’re useful for a first-timer.
mapView.addAnnotation(annotation) | ||
|
||
// Calcuate the route from the user's location to the set destination | ||
calculateRoute(from: (mapView.userLocation!.coordinate), to: annotation.coordinate) { [unowned self] (route, error) in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There’s no use of self
in this callback, so [unowned self]
should be unnecessary.
let options = NavigationRouteOptions(waypoints: [origin, destination], profileIdentifier: .automobileAvoidingTraffic) | ||
|
||
// Generate the route object and draw it on the map | ||
_ = Directions.shared.calculate(options) { (waypoints, routes, error) in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self
is used in this closure — should it be unowned
or weak
(or stay strong
)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:squints:
Looks like we missed this one.
|
||
// Generate the route object and draw it on the map | ||
_ = Directions.shared.calculate(options) { (waypoints, routes, error) in | ||
guard let route = routes?.first else { return } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could skip creating this route
variable and directly assign to self.directionsRoute
here.
|
||
// Customize the route line color and width | ||
let lineStyle = MGLLineStyleLayer(identifier: "route-style", source: source) | ||
lineStyle.lineColor = MGLStyleValue(rawValue: #colorLiteral(red: 0.1897518039, green: 0.3010634184, blue: 0.7994888425, alpha: 1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: looks like there’s an extra space before #colorLiteral(
.
@@ -10,7 +10,9 @@ target 'Examples' do | |||
end | |||
|
|||
target 'DocsCode' do | |||
shared_pods |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might as well remove shared_pods
entirely, now that it’s only used for the Examples
target.
Probably should also switch the Examples
target to use the non-symbols flavor of our SDK, that way we’re not downloading two different versions for this project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. 👍
let options = NavigationRouteOptions(waypoints: [origin, destination], profileIdentifier: .automobileAvoidingTraffic) | ||
|
||
// Generate the route object and draw it on the map | ||
_ = Directions.shared.calculate(options) { (waypoints, routes, error) in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:squints:
Looks like we missed this one.
|
||
// Generate the route object and draw it on the map | ||
NSURLSessionDataTask *task = [[MBDirections sharedDirections] calculateDirectionsWithOptions:options completionHandler:^( | ||
NSArray<MBWaypoint *> *waypoints, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These look one indent too far, judging by what follows.
if (error != nil) { | ||
printf("Error calculating route"); | ||
} | ||
}]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This closing }];
should align with its opening on L71 ([self calc...
).
toDestination:annotation.coordinate | ||
completion:^(MBRoute * _Nullable route, NSError * _Nullable error) { | ||
if (error != nil) { | ||
printf("Error calculating route"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use NSLog()
in ObjC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, since we have the error
object, let’s print that, too. (Like this.)
|
||
// #-code-snippet: navigation calculate-route-objc | ||
-(void)calculateRoutefromOrigin:(CLLocationCoordinate2D)origin | ||
toDestination :(CLLocationCoordinate2D)destination |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: extra space before this colon.
// Calcuate the route from the user's location to the set destination | ||
calculateRoute(from: (mapView.userLocation!.coordinate), to: annotation.coordinate) { (route, error) in | ||
if error != nil { | ||
// Print an error message |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ObjC version doesn’t have this comment — it’s probably unnecessary?
@@ -10,7 +10,9 @@ target 'Examples' do | |||
end | |||
|
|||
target 'DocsCode' do | |||
shared_pods |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. 👍
|
||
// #-code-snippet: navigation gesture-recognizer-objc | ||
// Add a gesture recognizer to the map view | ||
UITapGestureRecognizer *setDestination = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPress:)]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This incorrectly declares UILongPressGestureRecognizer
as UITapGestureRecognizer
.
MBNavigationRouteOptions *options = [[MBNavigationRouteOptions alloc] initWithWaypoints:@[originWaypoint, destinationWaypoint] profileIdentifier:MBDirectionsProfileIdentifierAutomobileAvoidingTraffic]; | ||
|
||
// Generate the route object and draw it on the map | ||
NSURLSessionDataTask *task = [[MBDirections sharedDirections] calculateDirectionsWithOptions:options completionHandler:^( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line triggers the warning, Unused variable “task”
.
Until mapbox/mapbox-directions-swift#209 lands, the way to avoid this is to explicitly discard the result, like so:
(void)[[[MBDirections sharedDirections] calc...
calculateRoute(from: (mapView.userLocation!.coordinate), to: annotation.coordinate) { (route, error) in | ||
if error != nil { | ||
// Print an error message | ||
print("Error calculating route") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -58,7 +58,7 @@ class ViewController: UIViewController, MGLMapViewDelegate { | |||
// Calcuate the route from the user's location to the set destination | |||
calculateRoute(from: (mapView.userLocation!.coordinate), to: annotation.coordinate) { (route, error) in | |||
if error != nil { | |||
print("Error calculating route") | |||
NSLog("Error calculating route: \(String(describing: error))") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So print()
is the recommended way to put text to console in Swift. 😁 It’s possible to use NSLog()
in Swift (and there are behavioral differences that aren’t relevant here), but print()
is preferred.
The issue with the ObjC code was in using printf()
, which is a C function.
let options = NavigationRouteOptions(waypoints: [origin, destination], profileIdentifier: .automobileAvoidingTraffic) | ||
|
||
// Generate the route object and draw it on the map | ||
Directions.shared.calculate(options) { (waypoints, routes, error) in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need _ =
to avoid a warning about an unused return value. We can remove it when mapbox/mapbox-directions-swift#209 makes it into a release.
Adds the code used for iOS tutorials from mapbox.com/help/tutorials into the DocsCode target. This will be used for testing code snippets before being used in documentation on mapbox.com/help.