CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.
TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Jun 2016 |
Maintained by Marcin Iwanicki.
A lightweight wrapper for The Google Directions API. See the Google Maps API Web Services documentation.
As Google wrote: "The Google Directions API is a service that calculates directions between locations using an HTTP request. You can search for directions for several modes of transportation, include transit, driving, walking or cycling. Directions may specify origins, destinations and waypoints either as text strings (e.g. "Chicago, IL" or "Darwin, NT, Australia") or as latitude/longitude coordinates. The Directions API can return multi-part directions using a series of waypoints."
The OCGoogleDirectionsAPI library allows your iOS apps to deal with this powerful service easily. IMPORTANT: It uses NSURLSession
only available in iOS 7.0+. It is not compatible with iOS 6.x and lower.
platform :ios, '7.0'
pod "OCGoogleDirectionsAPI", "~> 0.1.7"
It's really simple. To get directions you need to do 4 easy steps.
Importing headers. From now all OCDirections*
general classes will be accessible for you.
#import <OCGoogleDirectionsAPI/OCGoogleDirectionsAPI.h>
Secondly you can provide your Google API Key. The method application:didFinishLaunchingWithOptions:
in AppDelegate seams to be a good place for this code.
However the API Key is now optional. Please check API Key section to decide if you need the key or not.
[OCDirectionsAPIClient provideAPIKey:@"<YOUR API KEY>"];
Prepare a OCDirectionsRequest
object to specify route(s) you want to retrieve from the service.
OCDirectionsRequest *request = [OCDirectionsRequest requestWithOriginString:@"<ORIGIN>" andDestinationString:@"<DESTINATION>"];
Create an instance of OCDirectionsAPIClient
and call directions:response:
method to retrieve required data (OCDirectionsResponse
) asynchronously.
OCDirectionsAPIClient *client = [OCDirectionsAPIClient new];
[client directions:request response:^(OCDirectionsResponse *response, NSError *error) {
// e.g.
if (error) {
return;
}
if (response.status != OCDirectionsResponseStatusOK) {
return
}
// some code
}];
Alternatively you can use - (NSURLSessionDataTask *)dataTaskWithRequest:response:
factory method to have full control over NSURLSessionDataTask
object. It might be especially useful when you need to cancel download operation. After creating instance of NSURLSessionDataTask
you need to remember to call resume
method to start retrieving data.
Some init methods of OCDirectionsAPIClient
:
- (instancetype)initWithKey:(NSString *)key;
- (instancetype)initWithNoKeyUseHttps:(BOOL)useHttps;
- (instancetype)initWithKey:(NSString *)key useHttps:(BOOL)https;
Sequence diagram
That's all! It's quite easy, isn't it? If you like to know a bit more about requests please read the next section OCDirectionsRequest.
To create an isntance of OCDirectionsRequest
you can use one of the following factory methods.
+ (instancetype)requestWithOriginLocation:(CLLocation *)origin andDestinationLocation:(CLLocation *)destination;
+ (instancetype)requestWithOriginString:(NSString *)origin andDestinationLocation:(CLLocation *)destination;
+ (instancetype)requestWithOriginLocation:(CLLocation *)origin andDestinationString:(NSString *)destination;
+ (instancetype)requestWithOriginString:(NSString *)origin andDestinationString:(NSString *)destination;
To read more about available attributes please see the Request parameters page.
[request setAlternatives:YES];
[request setRegion:@"pl"];
[request setLanguage:@"pl"];
Here you can find the list of supported languages: https://developers.google.com/maps/faq#languagesupport
There are 3 different restrictions:
OCDirectionsRequestRestrictionAvoidTolls
OCDirectionsRequestRestrictionAvoidHighways
OCDirectionsRequestRestrictionAvoidFerries
You can ask to avoid one or even all of them by calling the setRestrictions:
method.
[request setRestrictions:@[@(OCDirectionsRequestRestrictionAvoidTolls), @(OCDirectionsRequestRestrictionAviodFerries)];
The API allows to choose one of the following travel modes:
OCDirectionsRequestTravelModeDriving
OCDirectionsRequestTravelModeWalking
OCDirectionsRequestTravelModeBicycling
OCDirectionsRequestTravelModeTransit
[request setTravelMode:OCDirectionsRequestTravelModeBicycling];
Available units:
OCDirectionsRequestUnitDefault
OCDirectionsRequestUnitMetric
OCDirectionsRequestUnitImperia
[request setUnit:OCDirectionsRequestUnitMetric];
You need to set NSArray
of NSString
and CLLocation
objects.
CLLocation *firstLocation = [[CLLocation alloc] initWithLatitude:51.2314 longitude:21.3243];
CLLocation *secondLocation = [[CLLocation alloc] initWithLatitude:51.1314 longitude:21.1321];
[request setWaypoints:@[@"London", firstLocation, "Reading", secondLocation]];
[request setWaypointsOptimise:YES];
[request setTrafficModel:OCDirectionsRequestTrafficModelOptimistic];
[request setTransitMode:OCDirectionsRequestTransitModeBus | OCDirectionsRequestTransitModeTrain];
[request setTransitRoutingPreference:OCDirectionsRequestTransitRoutingPreferenceFewerTransfers];
Classes:
Properties:
NSDictionary*
OCDirectionsResponseStatus
NSArray*
(array of OCDirectionsRoute*
)NSString*
NSDirectionsRoute*
Be aware that geocoded_waypoints
property is not supported in the current version (#8).
Enum:
OCDirectionsResponseStatusNotInitialized
OCDirectionsResponseStatusOK
OCDirectionsResponseStatusNotFound
OCDirectionsResponseStatusZeroResults
OCDirectionsResponseStatusMaxWaypointsExceeded
OCDirectionsResponseStatusInvalidRequest
OCDirectionsResponseStatusOverQueryLimit
OCDirectionsResponseStatusRequestDenied
OCDirectionsResponseStatusUnknownError
Properties:
NSDictionary*
NSArray*
(array of OCDirectionsLeg*
)NSString*
NSArray*
(array of NSString*
)NSArray*
(array of NSNumber*
)OCDirectionsPolyline*
OCDirectionsBounds*
NSString*
Properties:
NSDictionary*
OCDirectionsDistance*
OCDirectionsDuration*
OCDirectionsDuration*
NSString*
OCLocationCoordinate2D
NSString*
OCLocationCoordinate2D
NSArray*
(array of OCDirectionsStep*
)NSArray*
(array of OCDirectionsWaypoint*
)Properties:
NSDictionary*
NSString*
Properties:
NSDictionary*
CLLocationCoordinate2D
CLLocationCoordinate2D
Properties:
NSDictionary*
NSString*
NSNumber*
Properties:
NSDictionary*
NSString*
NSNumber*
Properties:
NSDictionary*
OCDirectionsDistance*
OCDirectionsDuration*
CLLocationCoordinate2D
NSString*
NSString*
OCDirectionsPolyline*
CLLocationCoordinate2D
OCDirectionsRequestTravelMode
Properties:
NSDictionary*
CLLocationCoordinate2D
NSNumber*
NSNumber*
Properties:
NSDictionary*
NSString*
NSString*
NSNumber*
Did you find a bug? Do you have great ideas how to make the library better? or you just want to say hello:) ... drop me a line on twitter @marciniwanicki.
OCGoogleDirectionsAPI is available under the MIT license. See the LICENSE file for more info.