Last active
August 29, 2015 14:26
-
-
Save overcyn/5d644e6adb318550c9c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <UIKit/UIKit.h> | |
| @interface MSCustomRouteViewController : UIViewController | |
| @end | |
| @interface MSCustomRouteViewController () <MRMapViewDelegate> | |
| @property (nonatomic, strong) MRMapView *mapView; | |
| @property (nonatomic, strong) MRDirections *directions; | |
| @property (nonatomic, strong) MRPathOverlay *routeOverlay; | |
| @end | |
| @implementation MSCustomRouteViewController | |
| - (id)initWithNibName:(NSString *)name bundle:(NSBundle *)bundle { | |
| if ((self = [super initWithNibName:name bundle:bundle])) { | |
| self.edgesForExtendedLayout = UIRectEdgeNone; | |
| UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Directions" style:UIBarButtonItemStylePlain target:self action:@selector(directionsAction)]; | |
| self.navigationItem.rightBarButtonItems = @[button]; | |
| } | |
| return self; | |
| } | |
| #pragma mark - UIViewController | |
| - (void)loadView { | |
| self.mapView = [[MRMapView alloc] init]; | |
| self.mapView.delegate = self; | |
| self.mapView.showsUserLocation = YES; | |
| self.mapView.showsRoutePath = NO; | |
| self.mapView.mapKey = [MREditorKey keyForMap:@"13001" app:@"5376006"]; | |
| self.view = self.mapView; | |
| } | |
| #pragma mark - Action | |
| - (void)directionsAction { | |
| if (!self.mapView.route && self.mapView.selectedAnnotation) { | |
| MRDirectionsRequest *request = [[MRDirectionsRequest alloc] init]; | |
| request.app = self.mapView.mapKey.parent; | |
| request.destination = [MRDirectionsDestination destinationWithPlacemarkKey:((MRPlacemark *)self.mapView.selectedAnnotation).key]; | |
| request.source = [MRDirectionsSource sourceWithCurrentLocation]; | |
| __weak typeof(self) weakSelf = self; | |
| self.directions = [[MRDirections alloc] initWithRequest:request presentingViewController:self]; | |
| [self.directions calculateDirectionsWithCompletionHandler:^(MRDirectionsResponse *response, NSError *error) { | |
| [weakSelf directionsResponseDidLoad:response error:error]; | |
| }]; | |
| } else { | |
| [self.mapView setRoute:nil animated:YES]; | |
| [self reloadRouteOverlay]; | |
| } | |
| } | |
| #pragma mark - MRMapViewDelegate | |
| - (MRPathRenderer *)mapView:(MRMapView *)mapView rendererForOverlay:(MRPathOverlay *)overlay { | |
| UIBezierPath *routePath = nil; | |
| if (self.mapView.route) { | |
| routePath = [UIBezierPath bezierPath]; | |
| for (MRRouteStep *i in self.mapView.route.steps) { | |
| if ([i.mapKey isEqualToKey:self.mapView.mapKey]) { | |
| [routePath appendPath:i.path]; | |
| } | |
| } | |
| } | |
| MRPathRenderer *renderer = [[MRPathRenderer alloc] initWithOverlay:overlay]; | |
| renderer.strokeColor = [UIColor blueColor]; | |
| renderer.lineCap = kCGLineCapRound; | |
| renderer.lineJoin = kCGLineJoinRound; | |
| renderer.path = routePath.CGPath; | |
| renderer.lineWidth = self.mapView.visibleMapRect.size.width / 100; // This is to work around a bug where line width is not respected and won't be necessary once its fixed | |
| return renderer; | |
| } | |
| - (void)mapViewDidFinishLoadingMap:(MRMapView *)mapView { | |
| [self reloadRouteOverlay]; | |
| } | |
| // This is to work around a bug where line width is not respected and won't be necessary once its fixed | |
| - (void)mapView:(MRMapView *)mapView visibleMapRectDidChange:(BOOL)animated { | |
| [CATransaction begin]; | |
| [CATransaction setDisableActions:YES]; | |
| MRPathRenderer *renderer = (id)[self.mapView rendererForOverlay:self.routeOverlay]; | |
| renderer.lineWidth = self.mapView.visibleMapRect.size.width / 100; | |
| [CATransaction commit]; | |
| } | |
| #pragma mark - Internal | |
| - (void)directionsResponseDidLoad:(MRDirectionsResponse *)response error:(NSError *)error { | |
| self.directions = nil; | |
| if (!error && response.routes.count > 0) { | |
| MRRoute *route = response.routes.firstObject; | |
| [self.mapView deselectAnnotationAnimated:NO]; | |
| [self.mapView setRoute:route animated:YES]; | |
| [self reloadRouteOverlay]; | |
| } | |
| } | |
| - (void)reloadRouteOverlay { | |
| if (self.mapView.route) { | |
| [self.mapView removeOverlay:self.routeOverlay]; | |
| self.routeOverlay = [[MRPathOverlay alloc] init]; | |
| [self.mapView addOverlay:self.routeOverlay]; | |
| } else { | |
| [self.mapView removeOverlay:self.routeOverlay]; | |
| self.routeOverlay = nil; | |
| } | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment