Commit 45245015 authored by Deployer's avatar Deployer

Added distance method

parent b7d7e48d
source 'https://github.com/CocoaPods/Specs.git' source 'https://github.com/CocoaPods/Specs.git'
use_frameworks! use_frameworks!
target Example_Swift do target 'Example_Swift' do
pod 'CedarMaps' pod 'CedarMaps'
end end
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
@import CoreLocation; @import CoreLocation;
@class CSQueryParameters; @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. /* 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. * Maps should be referenced by their map ID.
...@@ -20,6 +21,8 @@ ...@@ -20,6 +21,8 @@
- (instancetype)initWithMapID:(NSString *)mapID; - (instancetype)initWithMapID:(NSString *)mapID;
- (void)styleURLWithCompletion:(void (^) (NSURL *url))completion; - (void)styleURLWithCompletion:(void (^) (NSURL *url))completion;
- (void)distanceBetweenPoints:(CSDistancePoints *)points withCompletion:(void (^) (NSArray *results, NSError *error))completion;
- (void)forwardGeocodingWithQueryString:(NSString *)query - (void)forwardGeocodingWithQueryString:(NSString *)query
parameters:(CSQueryParameters *)parameters parameters:(CSQueryParameters *)parameters
completion:(void (^)(NSArray *results, NSError *error))completion; completion:(void (^)(NSArray *results, NSError *error))completion;
...@@ -38,3 +41,11 @@ ...@@ -38,3 +41,11 @@
- (void)addLocationWithCoordinate:(CLLocationCoordinate2D)coordinate; - (void)addLocationWithCoordinate:(CLLocationCoordinate2D)coordinate;
@end @end
#pragma mark
@interface CSDistancePoints: NSObject
- (void)addCoordinatePairWithDeparture:(CLLocationCoordinate2D)departure destination:(CLLocationCoordinate2D)destination;
@end
...@@ -23,6 +23,14 @@ ...@@ -23,6 +23,14 @@
@end @end
#pragma mark - CSDistancePoints Private Interface
@interface CSDistancePoints ()
@property (nonatomic, strong) NSMutableString *pointsDesc;
@end
#pragma mark - CSMapSource Private Interface #pragma mark - CSMapSource Private Interface
@interface CSMapKit () @interface CSMapKit ()
...@@ -47,7 +55,7 @@ ...@@ -47,7 +55,7 @@
#pragma mark StyleURL #pragma mark StyleURL
- (void)styleURLWithCompletion:(void (^) (NSURL *url))completion { - (void)styleURLWithCompletion:(void (^) (NSURL *url))completion {
[[CSAuthenticationManager sharedManager] savedAccessToken:^(NSString *token) { [[CSAuthenticationManager sharedManager] savedAccessToken:^(NSString *token) {
NSString *tileJSONURLString = [NSString stringWithFormat:@"%@/styles/%@.json", [[CSAuthenticationManager sharedManager] baseURL] , self.mapID]; NSString *tileJSONURLString = [NSString stringWithFormat:@"%@/styles/%@.json", [[CSAuthenticationManager sharedManager] baseURL] , self.mapID];
if (token && token.length > 0) { if (token && token.length > 0) {
...@@ -60,13 +68,67 @@ ...@@ -60,13 +68,67 @@
}]; }];
} }
#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 #pragma mark Geocoding
- (void)forwardGeocodingWithQueryString:(NSString *)query - (void)forwardGeocodingWithQueryString:(NSString *)query
parameters:(CSQueryParameters *)parameters parameters:(CSQueryParameters *)parameters
completion:(void (^)(NSArray *results, NSError *error))completion completion:(void (^)(NSArray *results, NSError *error))completion
{ {
NSMutableString *URLString = [NSMutableString stringWithFormat:@"%@/geocode/%@/%@", [[CSAuthenticationManager sharedManager] baseURL], self.mapID, query]; NSMutableString *URLString = [NSMutableString stringWithFormat:@"%@/geocode/%@/%@", [[CSAuthenticationManager sharedManager] baseURL], self.mapID, query];
if (parameters != nil) { if (parameters != nil) {
[URLString appendString:@"?"]; [URLString appendString:@"?"];
...@@ -77,7 +139,7 @@ ...@@ -77,7 +139,7 @@
[URLString appendFormat:@"%@=%@", key, obj]; [URLString appendFormat:@"%@=%@", key, obj];
}]; }];
} }
NSURL *URL = [NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSURL *URL = [NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
...@@ -88,7 +150,7 @@ ...@@ -88,7 +150,7 @@
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if ([(NSHTTPURLResponse *)response statusCode] == HTTP_401_NOT_AUTHORIZED) { if ([(NSHTTPURLResponse *)response statusCode] == HTTP_401_NOT_AUTHORIZED) {
NSString *description = NSLocalizedString(INVALID_CREDINTIAL, @""); NSString *description = NSLocalizedString(INVALID_CREDINTIAL, @"");
NSError *responseError = [NSError errorWithDomain:NSCocoaErrorDomain code:kCFURLErrorUserCancelledAuthentication userInfo:@{NSLocalizedDescriptionKey: description}]; NSError *responseError = [NSError errorWithDomain:NSCocoaErrorDomain code:kCFURLErrorUserCancelledAuthentication userInfo:@{NSLocalizedDescriptionKey: description}];
...@@ -127,7 +189,7 @@ ...@@ -127,7 +189,7 @@
{ {
NSMutableString *URLString = [NSMutableString stringWithFormat:@"%@/geocode/%@/%@,%@.json", NSMutableString *URLString = [NSMutableString stringWithFormat:@"%@/geocode/%@/%@,%@.json",
[[CSAuthenticationManager sharedManager] baseURL], self.mapID, @(coordinate.latitude), @(coordinate.longitude)]; [[CSAuthenticationManager sharedManager] baseURL], self.mapID, @(coordinate.latitude), @(coordinate.longitude)];
NSURL *URL = [NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSURL *URL = [NSURL URLWithString:[URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
...@@ -182,7 +244,7 @@ ...@@ -182,7 +244,7 @@
if (self != nil) { if (self != nil) {
self.params = [NSMutableDictionary dictionary]; self.params = [NSMutableDictionary dictionary];
} }
return self; return self;
} }
...@@ -207,3 +269,24 @@ ...@@ -207,3 +269,24 @@
} }
@end @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 ## Usage
...@@ -37,9 +37,19 @@ or initialise a ``MGLMapView`` with the url: ...@@ -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)forwardGeocodingWithQueryString:(NSString *)query parameters:(CSQueryParameters *)parameters completion:(void (^)(NSArray *results, NSError *error))completion;
- (void)reverseGeocodingWithCoordinate:(CLLocationCoordinate2D)coordinate completion:(void (^)(NSDictionary *result, 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. 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 ## 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