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