Skip to content
This repository was archived by the owner on Jun 14, 2024. It is now read-only.

Add tutorial code from mapbox.com/help #127

Merged
merged 30 commits into from
Nov 28, 2017
Merged

Add tutorial code from mapbox.com/help #127

merged 30 commits into from
Nov 28, 2017

Conversation

captainbarbosa
Copy link
Contributor

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.

@captainbarbosa captainbarbosa self-assigned this Nov 21, 2017
Podfile Outdated
@@ -1,9 +1,10 @@
platform :ios, '8.0'
platform :ios, '9.0'
Copy link
Contributor

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'
Copy link
Contributor

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.

Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Contributor Author

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 😐

@captainbarbosa
Copy link
Contributor Author

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.


// #-code-snippet: dds-circle add-style-layer-swift
layer.circleColor = MGLStyleValue<UIColor>(interpolationMode: .interval,
sourceStops: stops,
Copy link
Contributor

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];
Copy link
Contributor

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"];
Copy link
Contributor

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){
Copy link
Contributor

@friedbunny friedbunny Nov 27, 2017

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
Copy link
Contributor

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
Copy link
Contributor

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)?

Copy link
Contributor

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 }
Copy link
Contributor

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))
Copy link
Contributor

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
Copy link
Contributor

@friedbunny friedbunny Nov 27, 2017

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.

Copy link
Contributor

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
Copy link
Contributor

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,
Copy link
Contributor

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");
}
}];
Copy link
Contributor

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");
Copy link
Contributor

Choose a reason for hiding this comment

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

Use NSLog() in ObjC.

Copy link
Contributor

@friedbunny friedbunny Nov 28, 2017

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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:)];
Copy link
Contributor

@friedbunny friedbunny Nov 28, 2017

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:^(
Copy link
Contributor

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")
Copy link
Contributor

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))")
Copy link
Contributor

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
Copy link
Contributor

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.

@captainbarbosa captainbarbosa merged commit 197dd60 into master Nov 28, 2017
@captainbarbosa captainbarbosa deleted the all-the-tuts branch November 28, 2017 20:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants