Commit 45245015 authored by Deployer's avatar Deployer

Added distance method

parent b7d7e48d
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!
target Example_Swift do
target 'Example_Swift' do
pod 'CedarMaps'
end
......@@ -9,6 +9,7 @@
@import CoreLocation;
@class CSQueryParameters;
@class CSDistancePoints;
/* A CSMapKit is used to display map tiles from a network-based map hosted on CedarMaps as well as getting geocoding data from server.
* Maps should be referenced by their map ID.
......@@ -20,6 +21,8 @@
- (instancetype)initWithMapID:(NSString *)mapID;
- (void)styleURLWithCompletion:(void (^) (NSURL *url))completion;
- (void)distanceBetweenPoints:(CSDistancePoints *)points withCompletion:(void (^) (NSArray *results, NSError *error))completion;
- (void)forwardGeocodingWithQueryString:(NSString *)query
parameters:(CSQueryParameters *)parameters
completion:(void (^)(NSArray *results, NSError *error))completion;
......@@ -38,3 +41,11 @@
- (void)addLocationWithCoordinate:(CLLocationCoordinate2D)coordinate;
@end
#pragma mark
@interface CSDistancePoints: NSObject
- (void)addCoordinatePairWithDeparture:(CLLocationCoordinate2D)departure destination:(CLLocationCoordinate2D)destination;
@end
......@@ -23,6 +23,14 @@
@end
#pragma mark - CSDistancePoints Private Interface
@interface CSDistancePoints ()
@property (nonatomic, strong) NSMutableString *pointsDesc;
@end
#pragma mark - CSMapSource Private Interface
@interface CSMapKit ()
......@@ -60,6 +68,60 @@
}];
}
#pragma mark Distance
- (void)distanceBetweenPoints:(CSDistancePoints *)points withCompletion:(void (^) (NSArray *results, NSError *error))completion {
NSString *arguments = @"";
if (points.pointsDesc.length > 0 && [[points.pointsDesc substringFromIndex:points.pointsDesc.length-1] isEqualToString:@"/"]) {
arguments = [points.pointsDesc substringToIndex:points.pointsDesc.length - 1];
}
NSMutableString *URLString = [NSMutableString stringWithFormat:@"%@/distance/cedarmaps.driving/%@", [[CSAuthenticationManager sharedManager] baseURL], arguments];
NSURL *URL = [NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[[CSAuthenticationManager sharedManager] savedAccessToken:^(NSString *token) {
if (token) {
[request setValue:[NSString stringWithFormat:@"Bearer %@", token] forHTTPHeaderField:@"Authorization"];
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if ([(NSHTTPURLResponse *)response statusCode] == HTTP_401_NOT_AUTHORIZED) {
NSString *description = NSLocalizedString(INVALID_CREDINTIAL, @"");
NSError *responseError = [NSError errorWithDomain:NSCocoaErrorDomain code:kCFURLErrorUserCancelledAuthentication userInfo:@{NSLocalizedDescriptionKey: description}];
completion(nil, responseError);
} else {
if (error != nil) {
completion(nil, error);
} else {
NSError *serializationError;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&serializationError];
if (serializationError == nil) {
NSArray *output = [NSArray array];
if ([json.allKeys containsObject:@"result"]) {
NSArray *results = [[json objectForKey:@"result"] objectForKey:@"routes"];
output = results;
}
completion(output, nil);
}
else {
completion(nil, serializationError);
}
}
}
}] resume];
} else {
NSString *description = NSLocalizedString(TOKEN_NOT_PROVIDED, @"");
NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:TOKEN_NOT_PROVIDED_CODE userInfo:@{NSLocalizedDescriptionKey: description}];
completion(nil, error);
}
}];
}
#pragma mark Geocoding
- (void)forwardGeocodingWithQueryString:(NSString *)query
......@@ -207,3 +269,24 @@
}
@end
#pragma mark
@implementation CSDistancePoints
- (id)init
{
self = [super init];
if (self != nil) {
self.pointsDesc = [NSMutableString string];
}
return self;
}
- (void)addCoordinatePairWithDeparture:(CLLocationCoordinate2D)departure destination:(CLLocationCoordinate2D)destination {
[self.pointsDesc appendString:[NSString stringWithFormat:@"%f,%f;%f,%f/", departure.latitude, departure.longitude, destination.latitude, destination.longitude]];
}
@end
# CedarMap
# CedarMaps
## Usage
......@@ -37,9 +37,19 @@ or initialise a ``MGLMapView`` with the url:
- (void)forwardGeocodingWithQueryString:(NSString *)query parameters:(CSQueryParameters *)parameters completion:(void (^)(NSArray *results, NSError *error))completion;
- (void)reverseGeocodingWithCoordinate:(CLLocationCoordinate2D)coordinate completion:(void (^)(NSDictionary *result, NSError *error))completion;
``CSMapKit`` has a method for getting distance between one pair or multiple pairs of points. First you create an instance of ``CSDistancePoints`` and use the following function to add points:
- (void)addCoordinatePairWithDeparture:(CLLocationCoordinate2D)departure destination:(CLLocationCoordinate2D)destination;
This can be called multiple times. Then you can call the following method to get the distance info in an ``NSArray``:
- (void)distanceBetweenPoints:(CSDistancePoints *)points withCompletion:(void (^) (NSArray *results, NSError *error))completion;
The results would be available once the request finishes loading in an array in the completion handler.
In case you have got a credential error with ``nil`` as the result, there might be something wrong with your credentials at server side. So, before retrying and sending the request again request a new access token by calling method ``- (void)requestAccessTokenFromServer:(void (^)(NSString *token, NSError *error))completion;`` of ``CSAuthenticationManager`` class.
Example projects for both ``Objective C`` and ``Swift`` are included.
Example projects for both ``Objective-C`` and ``Swift`` are included.
## Requirements
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment