Last active
August 29, 2015 13:58
-
-
Save franciscojma86/9928451 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 <Foundation/Foundation.h> | |
@class MapDirectionsHelper; | |
@protocol MapDirectionsHelperDelegate | |
@optional | |
-(void)mapDirectionsHelper:(MapDirectionsHelper *)sender didGetRoute:(MKRoute *)route; | |
-(void)mapDirectionsHelper:(MapDirectionsHelper *)sender didFailToGetRoute:(NSError *)error; | |
-(void)mapDirectionsHelper:(MapDirectionsHelper *)sender didGetRouteETA:(NSTimeInterval)eta; | |
-(void)mapDirectionsHelper:(MapDirectionsHelper *)sender didFailToGetRouteETA:(NSError *)error; | |
@end | |
@interface FLMapDirectionsHelper : NSObject | |
@property (nonatomic,weak) id<MapDirectionsHelperDelegate>delegate; | |
-(void)routeDirectionsFromOrigin:(CLLocationCoordinate2D)originCoords | |
destination:(CLLocationCoordinate2D)destinationCoords; | |
-(void)calculateETAForOrigin:(CLLocationCoordinate2D)originCoords | |
destination:(CLLocationCoordinate2D)destinationCoords; | |
@end |
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 <MapKit/MapKit.h> | |
#import "MapDirectionsHelper.h" | |
@implementation MapDirectionsHelper | |
#pragma mark map item methods | |
+(MKMapItem *)mapItemWithCoords:(CLLocationCoordinate2D)coords | |
{ | |
NSDictionary *optionsDict = [NSDictionary dictionaryWithObjectsAndKeys:@"",@"", nil]; | |
MKPlacemark *source = [[MKPlacemark alloc]initWithCoordinate:coords | |
addressDictionary:optionsDict]; | |
return [[MKMapItem alloc]initWithPlacemark:source]; | |
} | |
+(MKMapItem *)sourceItemWithCoords:(CLLocationCoordinate2D)coords | |
{ | |
return [MapDirectionsHelper mapItemWithCoords:coords]; | |
} | |
+(MKMapItem *)destinationItemWithCoords:(CLLocationCoordinate2D)coords | |
{ | |
return [MapDirectionsHelper mapItemWithCoords:coords]; | |
} | |
#pragma mark routing methods | |
-(void)routeDirectionsFromOrigin:(CLLocationCoordinate2D)originCoords | |
destination:(CLLocationCoordinate2D)destinationCoords | |
{ | |
MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init]; | |
[request setSource:[MapDirectionsHelper sourceItemWithCoords:originCoords]]; | |
[request setDestination:[MapDirectionsHelper destinationItemWithCoords:destinationCoords]]; | |
[request setTransportType:MKDirectionsTransportTypeAutomobile]; | |
MKDirections *direction = [[MKDirections alloc]initWithRequest:request]; | |
//request directions from apple and draw line in reoute | |
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse *response, | |
NSError *error) { | |
if (error) | |
{ | |
[self.delegate fLMapDirectionsHelper:self | |
didFailToGetRoute:error]; | |
return;//handle errors properly | |
} | |
NSArray *arrRoutes = [response routes]; | |
[arrRoutes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { | |
MKRoute *route = obj; | |
[self.delegate fLMapDirectionsHelper:self | |
didGetRoute:route]; | |
}]; | |
}]; | |
} | |
-(void)calculateETAForOrigin:(CLLocationCoordinate2D)originCoords | |
destination:(CLLocationCoordinate2D)destinationCoords | |
{ | |
//create our request object | |
MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init]; | |
[request setSource:[MapDirectionsHelper sourceItemWithCoords:originCoords]]; | |
[request setDestination:[MapDirectionsHelper destinationItemWithCoords:destinationCoords]]; | |
[request setTransportType:MKDirectionsTransportTypeAutomobile]; | |
MKDirections *direction = [[MKDirections alloc]initWithRequest:request]; | |
[direction calculateETAWithCompletionHandler:^(MKETAResponse *response, | |
NSError *error) { | |
if (error) { | |
[self.delegate mapDirectionsHelper:self | |
didFailToGetRouteETA:error]; | |
return;//handle errors properly | |
} | |
[self.delegate mapDirectionsHelper:self | |
didGetRouteETA:[response expectedTravelTime]]; | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment