0

I watch GeekyLemon's maps tutorial for iOS it works but I need help drawing a line from the current location to the annotation in the MKMapView and NOT open the maps app. This app if for www.arf.net so please help.

My code does not have any working or non working code for directions.

MapViewController.h

//
//  MapViewController.h
//  ARF
//
//  Created by Tyler on 9/9/14.
//  Copyright (c) 2014 Tyler Olson. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController {
    MKMapView *mapView;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapView;

-(IBAction)setMap:(id)sender;

-(IBAction)goBack;

@end

MapViewController.m

//
//  MapViewController.m
//  ARF
//
//  Created by Tyler on 9/9/14.
//  Copyright (c) 2014 Tyler Olson. All rights reserved.
//

#import "MapViewController.h"
#import "MoreViewController.h"
#import "MapPin.h"

@interface MapViewController ()

@end

@implementation MapViewController

@synthesize mapView;

#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

- (void)viewDidLoad
{
    [super viewDidLoad];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    mapView.showsUserLocation = YES;

    MKCoordinateRegion
    regionARF = {{0.0, 0.0}, {0.0,0.0}};
    regionARF.center.latitude = 37.93301;
    regionARF.center.longitude = -122.02052;
    regionARF.span.latitudeDelta = 0.01f;
    regionARF.span.longitudeDelta = 0.01f;
    [mapView setRegion:regionARF animated:YES];

    MapPin *ann = [[MapPin alloc] init];
    ann.title = @"ARF - Animal Rescue Foundation";
    ann.subtitle = @"2890 Mitchel Drive, Walnut Crek, CA";
    ann.coordinate = regionARF.center;
    [mapView addAnnotation:ann];

    MKPlacemark *sourcePlackmark = [[MKPlacemark alloc] initWithCoordinate:mapView.userLocation.coordinate addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil]];
    MKMapItem *sourceMapItem = [[MKMapItem alloc] initWithPlacemark:sourcePlackmark];
    [sourceMapItem setName:@""];

    MKPlacemark *destinationPlackmark = [[MKPlacemark alloc] initWithCoordinate:ann.coordinate addressDictionary:[NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil]];
    MKMapItem *destinationMapItem = [[MKMapItem alloc] initWithPlacemark:destinationPlackmark];
    [destinationMapItem setName:@""];

    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    [request setSource:sourceMapItem];
    [request setDestination:destinationMapItem];
    [request setTransportType:MKDirectionsTransportTypeAny];
    [request setRequestsAlternateRoutes:YES];
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, NSError *error) {
        if (!error) {
            for (MKRoute *route in [response routes]) {
                [mapView addOverlay:[route polyline] level:MKOverlayLevelAboveRoads];
            }
        }
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (MKOverlayPathRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
        [renderer setStrokeColor:[UIColor greenColor]];
        [renderer setLineWidth:5.0];
        return renderer;
    }
    return nil;
}

- (IBAction)setMap:(id)sender {
    switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
        case 0:
            mapView.mapType = MKMapTypeStandard;
            break;
        case 1:
            mapView.mapType = MKMapTypeSatellite;
            break;
        case 2:
            mapView.mapType = MKMapTypeHybrid;
            break;

        default:
            break;
    }
}

- (IBAction)showMe {
    mapView.showsUserLocation = !mapView.showsUserLocation;
}

@end
Tyler
  • 1
  • 2
  • Its explained quite simply in apple's article. Follow the below link https://developer.apple.com/Library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/ProvidingDirections/ProvidingDirections.html – Nofel Mahmood Sep 13 '14 at 22:19
  • @Nofel Mahmood It is not working – Tyler Sep 14 '14 at 04:07
  • Can u elaborate by posting your code here and updating your question because just by saying dat it doesn't work is not good enough ! – Nofel Mahmood Sep 14 '14 at 10:20
  • @Nofel Mahmood Ok I added my code but this does NOT have any working or non working code of directions. – Tyler Sep 15 '14 at 01:32
  • There are several issues in the code shown. But for your main question, see [this answer](http://stackoverflow.com/questions/19772900/is-there-a-way-to-get-directions-in-mkmapview-using-a-built-in-apple-api). Be sure to set the map view's delegate otherwise rendererForOverlay won't get called. –  Sep 15 '14 at 01:52
  • @Anna You said that the are several issues in the code, can you point some out so I can be a better coder? – Tyler Sep 15 '14 at 02:22

0 Answers0