diff --git a/Examples/LPDistanceMatrixService/LPDistanceMatrixService/Classes/LPMainViewController.m b/Examples/LPDistanceMatrixService/LPDistanceMatrixService/Classes/LPMainViewController.m index b546ba9..48d094b 100644 --- a/Examples/LPDistanceMatrixService/LPDistanceMatrixService/Classes/LPMainViewController.m +++ b/Examples/LPDistanceMatrixService/LPDistanceMatrixService/Classes/LPMainViewController.m @@ -9,7 +9,7 @@ #import "LPMainViewController.h" -NSString *const googleAPIBrowserKey = @"AIzaSyDYq6cfchnqdab5cWBmNcw258IAr79SRV8"; +NSString *const googleAPIBrowserKey = @""; @interface LPMainViewController () diff --git a/Examples/LPPlacesAutocompleteService/LPPlacesAutocompleteService.xcodeproj/project.pbxproj b/Examples/LPPlacesAutocompleteService/LPPlacesAutocompleteService.xcodeproj/project.pbxproj index 0a4cac5..dd1563d 100644 --- a/Examples/LPPlacesAutocompleteService/LPPlacesAutocompleteService.xcodeproj/project.pbxproj +++ b/Examples/LPPlacesAutocompleteService/LPPlacesAutocompleteService.xcodeproj/project.pbxproj @@ -54,6 +54,9 @@ 0CCB5F2E1912BCB500648FCC /* LPImage.m in Sources */ = {isa = PBXBuildFile; fileRef = 0CCB5F2D1912BCB500648FCC /* LPImage.m */; }; 0CCB5F311912BE6100648FCC /* LPCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 0CCB5F301912BE6100648FCC /* LPCell.m */; }; 0CCB5F331912BEC800648FCC /* LPCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0CCB5F321912BEC800648FCC /* LPCell.xib */; }; + C1D82F051DF99E4C0076EB8B /* GTMStringEncoding.m in Sources */ = {isa = PBXBuildFile; fileRef = C1D82F021DF99E4C0076EB8B /* GTMStringEncoding.m */; }; + C1D82F061DF99E4C0076EB8B /* LPURLSigner.m in Sources */ = {isa = PBXBuildFile; fileRef = C1D82F041DF99E4C0076EB8B /* LPURLSigner.m */; }; + C1D82F091DF99E640076EB8B /* OrderedDictionary.m in Sources */ = {isa = PBXBuildFile; fileRef = C1D82F081DF99E640076EB8B /* OrderedDictionary.m */; }; FFBF8355195B0FF2006A0E0F /* AFHTTPRequestOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = FFBF8331195B0FF2006A0E0F /* AFHTTPRequestOperation.m */; }; FFBF8356195B0FF2006A0E0F /* AFHTTPRequestOperationManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FFBF8333195B0FF2006A0E0F /* AFHTTPRequestOperationManager.m */; }; FFBF8357195B0FF2006A0E0F /* AFHTTPSessionManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FFBF8335195B0FF2006A0E0F /* AFHTTPSessionManager.m */; }; @@ -162,6 +165,13 @@ 0CCB5F2F1912BE6100648FCC /* LPCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LPCell.h; sourceTree = ""; }; 0CCB5F301912BE6100648FCC /* LPCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LPCell.m; sourceTree = ""; }; 0CCB5F321912BEC800648FCC /* LPCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LPCell.xib; sourceTree = ""; }; + C1D82F001DF99E4C0076EB8B /* GTMDefines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTMDefines.h; sourceTree = ""; }; + C1D82F011DF99E4C0076EB8B /* GTMStringEncoding.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GTMStringEncoding.h; sourceTree = ""; }; + C1D82F021DF99E4C0076EB8B /* GTMStringEncoding.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GTMStringEncoding.m; sourceTree = ""; }; + C1D82F031DF99E4C0076EB8B /* LPURLSigner.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LPURLSigner.h; sourceTree = ""; }; + C1D82F041DF99E4C0076EB8B /* LPURLSigner.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LPURLSigner.m; sourceTree = ""; }; + C1D82F071DF99E640076EB8B /* OrderedDictionary.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OrderedDictionary.h; path = ../../../LPGoogleFunctions/OrderedDictionary.h; sourceTree = ""; }; + C1D82F081DF99E640076EB8B /* OrderedDictionary.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OrderedDictionary.m; path = ../../../LPGoogleFunctions/OrderedDictionary.m; sourceTree = ""; }; FFBF8330195B0FF2006A0E0F /* AFHTTPRequestOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFHTTPRequestOperation.h; sourceTree = ""; }; FFBF8331195B0FF2006A0E0F /* AFHTTPRequestOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AFHTTPRequestOperation.m; sourceTree = ""; }; FFBF8332195B0FF2006A0E0F /* AFHTTPRequestOperationManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AFHTTPRequestOperationManager.h; sourceTree = ""; }; @@ -302,6 +312,9 @@ 0CCB5DF11912B1AA00648FCC /* Libraries */ = { isa = PBXGroup; children = ( + C1D82F071DF99E640076EB8B /* OrderedDictionary.h */, + C1D82F081DF99E640076EB8B /* OrderedDictionary.m */, + C1D82EFE1DF99E4C0076EB8B /* AQGoogleSignature */, FFBF832E195B0FF2006A0E0F /* AFNetworking */, 0CCB5F2B1912BC8E00648FCC /* LPImage */, 0CCB5DF21912B1B600648FCC /* LPGoogleFunctions */, @@ -400,6 +413,27 @@ name = LPImage; sourceTree = ""; }; + C1D82EFE1DF99E4C0076EB8B /* AQGoogleSignature */ = { + isa = PBXGroup; + children = ( + C1D82EFF1DF99E4C0076EB8B /* GoogleSignature */, + C1D82F031DF99E4C0076EB8B /* LPURLSigner.h */, + C1D82F041DF99E4C0076EB8B /* LPURLSigner.m */, + ); + name = AQGoogleSignature; + path = ../../../LPGoogleFunctions/AQGoogleSignature; + sourceTree = ""; + }; + C1D82EFF1DF99E4C0076EB8B /* GoogleSignature */ = { + isa = PBXGroup; + children = ( + C1D82F001DF99E4C0076EB8B /* GTMDefines.h */, + C1D82F011DF99E4C0076EB8B /* GTMStringEncoding.h */, + C1D82F021DF99E4C0076EB8B /* GTMStringEncoding.m */, + ); + path = GoogleSignature; + sourceTree = ""; + }; FFBF832E195B0FF2006A0E0F /* AFNetworking */ = { isa = PBXGroup; children = ( @@ -574,15 +608,18 @@ 0CCB5E401912B1B600648FCC /* LPMapImageMarker.m in Sources */, 0CCB5E4B1912B1B600648FCC /* LPStop.m in Sources */, 0CCB5E461912B1B600648FCC /* LPPlaceSearchResults.m in Sources */, + C1D82F061DF99E4C0076EB8B /* LPURLSigner.m in Sources */, 0CCB5E391912B1B600648FCC /* LPEvent.m in Sources */, 0CCB5E441912B1B600648FCC /* LPPlacePhoto.m in Sources */, 0CCB5E501912B1B600648FCC /* LPWaypoint.m in Sources */, 0CCB5E471912B1B600648FCC /* LPPolyline.m in Sources */, 0CCB5E481912B1B600648FCC /* LPPrediction.m in Sources */, + C1D82F091DF99E640076EB8B /* OrderedDictionary.m in Sources */, 0CCB5F2E1912BCB500648FCC /* LPImage.m in Sources */, FFBF8364195B0FF2006A0E0F /* UIRefreshControl+AFNetworking.m in Sources */, FFBF8361195B0FF2006A0E0F /* UIButton+AFNetworking.m in Sources */, 0CCB5E411912B1B600648FCC /* LPMatchedSubstring.m in Sources */, + C1D82F051DF99E4C0076EB8B /* GTMStringEncoding.m in Sources */, 0CCB5E371912B1B600648FCC /* LPDistance.m in Sources */, FFBF8356195B0FF2006A0E0F /* AFHTTPRequestOperationManager.m in Sources */, 0CCB5E3B1912B1B600648FCC /* LPGeocodingResults.m in Sources */, diff --git a/LPGoogleFunctions/LPGoogleFunctions.h b/LPGoogleFunctions/LPGoogleFunctions.h index fafde26..c74a0c5 100644 --- a/LPGoogleFunctions/LPGoogleFunctions.h +++ b/LPGoogleFunctions/LPGoogleFunctions.h @@ -137,10 +137,6 @@ typedef enum { */ - (void)loadStaticMapImageForLocation:(LPLocation *)location zoomLevel:(int)zoom imageSize:(CGSize)size imageScale:(int)scale mapType:(LPGoogleMapType)maptype markersArray:(NSArray *)markers successfulBlock:(void (^)(UIImage *image))successful failureBlock:(void (^)(NSError *error))failure; -- (void)loadStaticMapImageForFromLocation:(LPLocation *)location toLocation:(LPLocation *)location zoomLevel:(int)zoom imageSize:(CGSize)size imageScale:(int)scale mapType:(LPGoogleMapType)maptype markersArray:(NSArray *)markers path:(NSString *)path format:(NSString *)format successfulBlock:(void (^)(UIImage *image))successful failureBlock:(void (^)(NSError *error))failure; - -- (void)loadStaticMapImageForFromLocation:(LPLocation *)location toLocation:(LPLocation *)location zoomLevel:(int)zoom imageSize:(CGSize)size imageScale:(int)scale mapType:(LPGoogleMapType)maptype sourceMarker:(NSString *)sourceMarker destMarker:(NSString *)destMarker path:(NSString *)path format:(NSString *)format successfulBlock:(void (^)(UIImage *image))successful failureBlock:(void (^)(NSError *error))failure; - /** * The Google Maps Image APIs make it easy to embed a static Google Maps image into your image view. * @param Defines the center of the map. @@ -245,6 +241,12 @@ typedef enum { */ - (void)loadDistanceMatrixForOrigins:(NSArray *)origins forDestinations:(NSArray *)destinations directionsTravelMode:(LPGoogleDistanceMatrixTravelMode)travelMode directionsAvoidTolls:(LPGoogleDistanceMatrixAvoid)avoid directionsUnit:(LPGoogleDistanceMatrixUnit)unit departureTime:(NSDate *)departureTime successfulBlock:(void (^)(LPDistanceMatrix *distanceMatrix))successful failureBlock:(void (^)(LPGoogleStatus status))failure; +- (void)loadStaticMapImageWithSize:(CGSize)size imageScale:(int)scale mapType:(LPGoogleMapType)maptype sourceMarker:(NSString *)sourceMarker destMarker:(NSString *)destMarker path:(NSString *)path format:(NSString *)format successfulBlock:(void (^)(UIImage *image))successful failureBlock:(void (^)(NSError *error))failure; + +- (void)loadPlacesAutocompleteForInput:(NSString *)input offset:(int)offset location:(LPLocation *)location placeType:(LPGooglePlaceType)placeType countryRestriction:(NSString *)countryRestriction forceBrowserKey:(NSString *)browserKey successfulBlock:(void (^)(NSArray *placesAutocomplete))successful failureBlock:(void (^)(LPGoogleStatus status))failure; + +- (void)loadPlaceDetailsForPlaceID:(NSString *)placeID forceBrowserKey:(NSString *)browserKey successfulBlock:(void (^)(LPPlaceDetailsResults *placeDetailsResults))successful failureBlock:(void (^)(LPGoogleStatus status))failure; + @end @@ -269,6 +271,8 @@ typedef enum { - (void)googleFunctions:(LPGoogleFunctions *)googleFunctions errorLoadingPlaceDetailsResultWithStatus:(LPGoogleStatus)status; - (void)googleFunctions:(LPGoogleFunctions *)googleFunctions didLoadPlaceDetailsResult:(LPPlaceDetailsResults *)placeDetailsResults; +- (void)googleFunctionsWillLoadPlaceDetailsResult:(LPGoogleFunctions *)googleFunctions forPlaceID:(NSString *)placeID; + #pragma mark - Geocoding - (void)googleFunctionsWillLoadGeocoding:(LPGoogleFunctions *)googleFunctions forAddress:(NSString *)address filterComponents:(NSArray *)filterComponents; - (void)googleFunctionsWillLoadGeocoding:(LPGoogleFunctions *)googleFunctions forLocation:(LPLocation *)location filterComponents:(NSArray *)filterComponents; diff --git a/LPGoogleFunctions/LPGoogleFunctions.m b/LPGoogleFunctions/LPGoogleFunctions.m index 01864c8..edbac4e 100644 --- a/LPGoogleFunctions/LPGoogleFunctions.m +++ b/LPGoogleFunctions/LPGoogleFunctions.m @@ -143,6 +143,12 @@ + (NSString*)getGoogleStatus:(LPGoogleStatus)status } } +- (NSString *)calculateSignatureForURLString:(NSString *)urlString { + NSURL *url = [NSURL URLWithString:urlString]; + NSString *signature = [[LPURLSigner sharedManager] createSignatureWithHMAC_SHA1:[NSString stringWithFormat:@"%@?%@", [url path], [url query]] key:self.googleAPICryptoKey]; + return signature; +} + - (void)loadStreetViewImageForLocation:(LPLocation *)location imageSize:(CGSize)size heading:(float)heading fov:(float)fov pitch:(float)pitch successfulBlock:(void (^)(UIImage *image))successful failureBlock:(void (^)(NSError *error))failure { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; @@ -722,13 +728,14 @@ - (void)loadPlacePhotoForReference:(NSString *)reference maxHeight:(int)maxHeigh }]; } -- (void)loadDistanceMatrixForOrigins:(NSArray *)origins forDestinations:(NSArray *)destinations directionsTravelMode:(LPGoogleDistanceMatrixTravelMode)travelMode directionsAvoidTolls:(LPGoogleDistanceMatrixAvoid)avoid directionsUnit:(LPGoogleDistanceMatrixUnit)unit departureTime:(NSDate *)departureTime successfulBlock:(void (^)(LPDistanceMatrix *distanceMatrix))successful failureBlock:(void (^)(LPGoogleStatus status))failure -{ +#pragma mark - Added methods for custom usage + +- (void)loadDistanceMatrixForOrigins:(NSArray *)origins forDestinations:(NSArray *)destinations directionsTravelMode:(LPGoogleDistanceMatrixTravelMode)travelMode directionsAvoidTolls:(LPGoogleDistanceMatrixAvoid)avoid directionsUnit:(LPGoogleDistanceMatrixUnit)unit departureTime:(NSDate *)departureTime successfulBlock:(void (^)(LPDistanceMatrix *distanceMatrix))successful failureBlock:(void (^)(LPGoogleStatus status))failure { + AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFJSONResponseSerializer serializer]; OrderedDictionary *parameters = [[OrderedDictionary alloc] init]; - // NSMutableDictionary *parameters = [NSMutableDictionary new]; NSMutableString *originsString = [NSMutableString new]; for (int i=0; i<[origins count]; i++) { @@ -772,31 +779,20 @@ - (void)loadDistanceMatrixForOrigins:(NSArray *)origins forDestinations:(NSArray [parameters setObject:[NSString stringWithFormat:@"%.0f", [departureTime timeIntervalSince1970]] forKey:@"departure_time"]; } + NSMutableString *urlString = [NSMutableString stringWithFormat:@"%@/%@?", googleAPIUri, googleAPIDistanceMatrixURLPath]; + for (NSString *key in parameters) { + [urlString appendString:[NSString stringWithFormat:@"%@=%@&", key, parameters[key]]]; + } if (self.googleAPIBrowserKey) { - [parameters setObject:[NSString stringWithFormat:@"%@", self.googleAPIBrowserKey] forKey:@"key"]; + [urlString appendString:[NSString stringWithFormat:@"%@=%@", @"key", [NSString stringWithFormat:@"%@", self.googleAPIBrowserKey]]]; } else { - [parameters setObject:[NSString stringWithFormat:@"%@", self.googleAPIClientID] forKey:@"client"]; - - NSMutableString* urlStr = [NSMutableString stringWithFormat:@"%@/%@?", googleAPIUri, googleAPIDistanceMatrixURLPath]; - for (NSString* key in parameters) { - [urlStr appendString:[NSString stringWithFormat:@"%@=%@&", key, parameters[key]]]; - } - NSString *newUrlStr = [urlStr substringToIndex:[urlStr length]-1]; - NSURL* url = [NSURL URLWithString:newUrlStr]; - NSString* signature = [[LPURLSigner sharedManager] createSignatureWithHMAC_SHA1:[NSString stringWithFormat:@"%@?%@", [url path], [url query]] key:self.googleAPICryptoKey]; - - [parameters setObject:[NSString stringWithFormat:@"%@", signature] forKey:@"signature"]; + [urlString appendString:[NSString stringWithFormat:@"%@=%@", @"client", [NSString stringWithFormat:@"%@", self.googleAPIClientID]]]; + [urlString appendString:[NSString stringWithFormat:@"&%@=%@", @"signature", [self calculateSignatureForURLString:urlString]]]; } - NSMutableString* params = [NSMutableString string]; - for (NSString* key in parameters) { - [params appendString:[NSString stringWithFormat:@"%@=%@&", key, parameters[key]]]; - } - NSString *newParams = [params substringToIndex:[params length]-1]; - - [manager GET:[NSString stringWithFormat:@"%@/%@?%@", googleAPIUri, googleAPIDistanceMatrixURLPath, newParams] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { + [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { LPDistanceMatrix *distanceMatrix = [LPDistanceMatrix distanceMatrixWithObjects:responseObject]; distanceMatrix.requestTravelMode = travelMode; @@ -831,8 +827,8 @@ - (void)loadDistanceMatrixForOrigins:(NSArray *)origins forDestinations:(NSArray }]; } -- (void)loadDirectionsForOrigin:(LPLocation *)origin forDestination:(LPLocation *)destination directionsTravelMode:(LPGoogleDirectionsTravelMode)travelMode directionsAvoidTolls:(LPGoogleDirectionsAvoid)avoid directionsUnit:(LPGoogleDirectionsUnit)unit directionsAlternatives:(BOOL)alternatives departureTime:(NSDate *)departureTime arrivalTime:(NSDate *)arrivalTime waypoints:(NSArray *)waypoints successfulBlock:(void (^)(LPDirections *directions))successful failureBlock:(void (^)(LPGoogleStatus status, NSString* errorMessage))failure -{ +- (void)loadDirectionsForOrigin:(LPLocation *)origin forDestination:(LPLocation *)destination directionsTravelMode:(LPGoogleDirectionsTravelMode)travelMode directionsAvoidTolls:(LPGoogleDirectionsAvoid)avoid directionsUnit:(LPGoogleDirectionsUnit)unit directionsAlternatives:(BOOL)alternatives departureTime:(NSDate *)departureTime arrivalTime:(NSDate *)arrivalTime waypoints:(NSArray *)waypoints successfulBlock:(void (^)(LPDirections *directions))successful failureBlock:(void (^)(LPGoogleStatus status, NSString *errorMessage))failure { + if ([self.delegate respondsToSelector:@selector(googleFunctionsWillLoadDirections:)]) { [self.delegate googleFunctionsWillLoadDirections:self]; } @@ -842,8 +838,6 @@ - (void)loadDirectionsForOrigin:(LPLocation *)origin forDestination:(LPLocation OrderedDictionary *parameters = [[OrderedDictionary alloc] init]; - // NSMutableDictionary *parameters = [NSMutableDictionary new]; - [parameters setObject:[NSString stringWithFormat:@"%f,%f", origin.latitude, origin.longitude] forKey:@"origin"]; [parameters setObject:[NSString stringWithFormat:@"%f,%f", destination.latitude, destination.longitude] forKey:@"destination"]; [parameters setObject:[NSString stringWithFormat:@"%@", self.sensor ? @"true" : @"false"] forKey:@"sensor"]; @@ -875,34 +869,21 @@ - (void)loadDirectionsForOrigin:(LPLocation *)origin forDestination:(LPLocation [parameters setObject:waypointsString forKey:@"waypoints"]; } - + + NSMutableString *urlString = [NSMutableString stringWithFormat:@"%@/%@?", googleAPIUri, googleAPIDirectionsURLPath]; + for (NSString *key in parameters) { + [urlString appendString:[NSString stringWithFormat:@"%@=%@&", key, parameters[key]]]; + } if (self.googleAPIBrowserKey) { - [parameters setObject:[NSString stringWithFormat:@"%@", self.googleAPIBrowserKey] forKey:@"key"]; + [urlString appendString:[NSString stringWithFormat:@"%@=%@", @"key", [NSString stringWithFormat:@"%@", self.googleAPIBrowserKey]]]; } else { - [parameters setObject:[NSString stringWithFormat:@"%@", self.googleAPIClientID] forKey:@"client"]; - - NSMutableString* urlStr = [NSMutableString stringWithFormat:@"%@/%@?", googleAPIUri, googleAPIDirectionsURLPath]; - for (NSString* key in parameters) { - [urlStr appendString:[NSString stringWithFormat:@"%@=%@&", key, parameters[key]]]; - } - NSString *newUrlStr = [urlStr substringToIndex:[urlStr length]-1]; - NSURL* url = [NSURL URLWithString:newUrlStr]; - NSString* signature = [[LPURLSigner sharedManager] createSignatureWithHMAC_SHA1:[NSString stringWithFormat:@"%@?%@", [url path], [url query]] key:self.googleAPICryptoKey]; - - [parameters setObject:[NSString stringWithFormat:@"%@", signature] forKey:@"signature"]; + [urlString appendString:[NSString stringWithFormat:@"%@=%@", @"client", [NSString stringWithFormat:@"%@", self.googleAPIClientID]]]; + [urlString appendString:[NSString stringWithFormat:@"&%@=%@", @"signature", [self calculateSignatureForURLString:urlString]]]; } - NSMutableString* params = [NSMutableString string]; - for (NSString* key in parameters) { - [params appendString:[NSString stringWithFormat:@"%@=%@&", key, parameters[key]]]; - } - NSString *newParams = [params substringToIndex:[params length]-1]; - - - - [manager GET:[NSString stringWithFormat:@"%@/%@?%@", googleAPIUri, googleAPIDirectionsURLPath, newParams] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { + [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { LPDirections *directions = [LPDirections directionsWithObjects:responseObject]; directions.requestTravelMode = travelMode; @@ -937,17 +918,14 @@ - (void)loadDirectionsForOrigin:(LPLocation *)origin forDestination:(LPLocation }]; } -- (void)loadStaticMapImageForFromLocationOld:(LPLocation *)location toLocation:(LPLocation *)location zoomLevel:(int)zoom imageSize:(CGSize)size imageScale:(int)scale mapType:(LPGoogleMapType)maptype markersArray:(NSArray *)markers path:(NSString *)path format:(NSString *)format successfulBlock:(void (^)(UIImage *image))successful failureBlock:(void (^)(NSError *error))failure -{ +- (void)loadStaticMapImageWithSize:(CGSize)size imageScale:(int)scale mapType:(LPGoogleMapType)maptype sourceMarker:(NSString *)sourceMarker destMarker:(NSString *)destMarker path:(NSString *)path format:(NSString *)format successfulBlock:(void (^)(UIImage *image))successful failureBlock:(void (^)(NSError *error))failure { + AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFImageResponseSerializer serializer]; - NSMutableDictionary *parameters = [NSMutableDictionary new]; + OrderedDictionary *parameters = [[OrderedDictionary alloc] init]; - // [parameters setObject:[NSString stringWithFormat:@"%f,%f", location.latitude, location.longitude] forKey:@"center"]; - // [parameters setObject:(self.sensor ? @"true" : @"false") forKey:@"sensor"]; - // [parameters setObject:[NSNumber numberWithInt:zoom] forKey:@"zoom"]; [parameters setObject:[NSNumber numberWithInt:scale] forKey:@"scale"]; [parameters setObject:[NSString stringWithFormat:@"%dx%d", (int)size.width, (int)size.height] forKey:@"size"]; [parameters setObject:[LPGoogleFunctions getMapType:maptype] forKey:@"maptype"]; @@ -955,13 +933,32 @@ - (void)loadStaticMapImageForFromLocationOld:(LPLocation *)location toLocation:( [parameters setObject:format forKey:@"format"]; NSMutableSet *parametersMarkers = [[NSMutableSet alloc] init]; - for (int i=0; i<[markers count]; i++) { - LPMapImageMarker *marker = (LPMapImageMarker *)[markers objectAtIndex:i]; - [parametersMarkers addObject:[marker getMarkerURLString]]; - } + [parametersMarkers addObject:sourceMarker]; + [parametersMarkers addObject:destMarker]; + [parameters setObject:parametersMarkers forKey:@"markers"]; - [manager GET:[NSString stringWithFormat:@"%@/%@?", googleAPIUri, googleAPIStaticMapImageURLPath] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { + NSMutableString *urlString = [NSMutableString stringWithFormat:@"%@/%@?", googleAPIUri, googleAPIStaticMapImageURLPath]; + for (NSString *key in parameters) { + if ([parameters[key] isKindOfClass:[NSMutableSet class]]) { + for (int i=0; i<[parameters[key] count]; i++) { + [urlString appendString:[NSString stringWithFormat:@"%@=%@&", key, [parameters[key] allObjects][i]]]; + } + } + else { + [urlString appendString:[NSString stringWithFormat:@"%@=%@&", key, parameters[key]]]; + } + } + + if (self.googleAPIBrowserKey) { + [urlString appendString:[NSString stringWithFormat:@"%@=%@", @"key", [NSString stringWithFormat:@"%@", self.googleAPIBrowserKey]]]; + } + else { + [urlString appendString:[NSString stringWithFormat:@"%@=%@", @"client", [NSString stringWithFormat:@"%@", self.googleAPIClientID]]]; + [urlString appendString:[NSString stringWithFormat:@"&%@=%@", @"signature", [self calculateSignatureForURLString:urlString]]]; + } + + [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { if(successful) successful(responseObject); @@ -974,87 +971,136 @@ - (void)loadStaticMapImageForFromLocationOld:(LPLocation *)location toLocation:( }]; } -- (void)loadStaticMapImageForFromLocation:(LPLocation *)location toLocation:(LPLocation *)location zoomLevel:(int)zoom imageSize:(CGSize)size imageScale:(int)scale mapType:(LPGoogleMapType)maptype sourceMarker:(NSString *)sourceMarker destMarker:(NSString *)destMarker path:(NSString *)path format:(NSString *)format successfulBlock:(void (^)(UIImage *image))successful failureBlock:(void (^)(NSError *error))failure -{ - AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; - manager.requestSerializer = [AFJSONRequestSerializer serializer]; - manager.responseSerializer = [AFImageResponseSerializer serializer]; - - // NSMutableDictionary *parameters = [NSMutableDictionary new]; - OrderedDictionary *parameters = [[OrderedDictionary alloc] init]; - - // [parameters setObject:[NSString stringWithFormat:@"%f,%f", location.latitude, location.longitude] forKey:@"center"]; - // [parameters setObject:(self.sensor ? @"true" : @"false") forKey:@"sensor"]; - // [parameters setObject:[NSNumber numberWithInt:zoom] forKey:@"zoom"]; - - [parameters setObject:[NSNumber numberWithInt:scale] forKey:@"scale"]; - [parameters setObject:[NSString stringWithFormat:@"%dx%d", (int)size.width, (int)size.height] forKey:@"size"]; - [parameters setObject:[LPGoogleFunctions getMapType:maptype] forKey:@"maptype"]; - [parameters setObject:path forKey:@"path"]; - [parameters setObject:format forKey:@"format"]; - - NSMutableSet *parametersMarkers = [[NSMutableSet alloc] init]; - [parametersMarkers addObject:sourceMarker]; - [parametersMarkers addObject:destMarker]; - - // for (int i=0; i<[markers count]; i++) { - // LPMapImageMarker *marker = (LPMapImageMarker *)[markers objectAtIndex:i]; - // [parametersMarkers addObject:[marker getMarkerURLString]]; - // } - [parameters setObject:parametersMarkers forKey:@"markers"]; - +- (void)loadPlacesAutocompleteForInput:(NSString *)input offset:(int)offset location:(LPLocation *)location placeType:(LPGooglePlaceType)placeType countryRestriction:(NSString *)countryRestriction forceBrowserKey:(NSString *)browserKey successfulBlock:(void (^)(NSArray *placesAutocomplete))successful failureBlock:(void (^)(LPGoogleStatus status))failure { + if ([self.delegate respondsToSelector:@selector(googleFunctionsWillLoadPlacesAutocomplete:forInput:)]) { + [self.delegate googleFunctionsWillLoadPlacesAutocomplete:self forInput:input]; + } + AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; + manager.responseSerializer = [AFJSONResponseSerializer serializer]; + NSMutableDictionary *parameters = [NSMutableDictionary new]; - if (self.googleAPIBrowserKey) { + if (browserKey) { + [parameters setObject:[NSString stringWithFormat:@"%@", browserKey] forKey:@"key"]; + } + else if (self.googleAPIBrowserKey) { [parameters setObject:[NSString stringWithFormat:@"%@", self.googleAPIBrowserKey] forKey:@"key"]; } else { [parameters setObject:[NSString stringWithFormat:@"%@", self.googleAPIClientID] forKey:@"client"]; - - NSMutableString* urlStr = [NSMutableString stringWithFormat:@"%@/%@?", googleAPIUri, googleAPIStaticMapImageURLPath]; - for (NSString* key in parameters) { - if ([parameters[key] isKindOfClass:[NSMutableSet class]]) { - for (int i=0; i<[parameters[key] count]; i++) { - [urlStr appendString:[NSString stringWithFormat:@"%@=%@&", key, [parameters[key] allObjects][i]]]; - } - } - else { - [urlStr appendString:[NSString stringWithFormat:@"%@=%@&", key, parameters[key]]]; - } - } - NSString *newUrlStr = [urlStr substringToIndex:[urlStr length]-1]; - NSURL* url = [NSURL URLWithString:[newUrlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; - NSString* signature = [[LPURLSigner sharedManager] createSignatureWithHMAC_SHA1:[NSString stringWithFormat:@"%@?%@", [url path], [url query]] key:self.googleAPICryptoKey]; - - [parameters setObject:[NSString stringWithFormat:@"%@", signature] forKey:@"signature"]; } - NSMutableString* params = [NSMutableString string]; - for (NSString* key in parameters) { + [parameters setObject:[NSString stringWithFormat:@"%@", input] forKey:@"input"]; + [parameters setObject:[LPPrediction getStringFromGooglePlaceType:placeType] forKey:@"types"]; + [parameters setObject:[NSString stringWithFormat:@"%d", offset] forKey:@"offset"]; + [parameters setObject:[NSString stringWithFormat:@"%f,%f",location.latitude, location.longitude] forKey:@"location"]; + [parameters setObject:[NSString stringWithFormat:@"%@", self.languageCode] forKey:@"language"]; + + if(countryRestriction) { + [parameters setObject:[NSString stringWithFormat:@"country:%@", countryRestriction] forKey:@"components"]; + } + + [manager GET:[NSString stringWithFormat:@"%@/%@?", googleAPIUri, googleAPIPlacesAutocompleteURLPath] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { - if ([parameters[key] isKindOfClass:[NSMutableSet class]]) { - for (int i=0; i<[parameters[key] count]; i++) { - [params appendString:[NSString stringWithFormat:@"%@=%@&", key, [parameters[key] allObjects][i]]]; - } + NSDictionary *dictionary = responseObject; + + if ([dictionary isKindOfClass:[NSNull class]] + || [[dictionary objectForKey:@"predictions"] isKindOfClass:[NSNull class]] + || [[dictionary objectForKey:@"status"] isKindOfClass:[NSNull class]]) { + + if (failure) + failure(LPGoogleStatusUnknownError); } else { - [params appendString:[NSString stringWithFormat:@"%@=%@&", key, parameters[key]]]; + + NSString *statusCode = dictionary[@"status"]; + + if ([statusCode isEqualToString:@"OK"]) { + if ([self.delegate respondsToSelector:@selector(googleFunctions:didLoadPlacesAutocomplete:)]) { + [self.delegate googleFunctions:self didLoadPlacesAutocomplete:dictionary[@"predictions"]]; + } + + if (successful) + successful(dictionary[@"predictions"]); + } else { + LPGoogleStatus status = [LPGoogleFunctions getGoogleStatusFromString:statusCode]; + + if ([self.delegate respondsToSelector:@selector(googleFunctions:errorLoadingPlacesAutocompleteWithStatus:)]) { + [self.delegate googleFunctions:self errorLoadingPlacesAutocompleteWithStatus:status]; + } + + if (failure) + failure(status); + } } + } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + + if ([self.delegate respondsToSelector:@selector(googleFunctions:errorLoadingPlacesAutocompleteWithStatus:)]) { + [self.delegate googleFunctions:self errorLoadingPlacesAutocompleteWithStatus:LPGoogleStatusUnknownError]; + } + + if (failure) + failure(LPGoogleStatusUnknownError); + + }]; +} + +- (void)loadPlaceDetailsForPlaceID:(NSString *)placeID forceBrowserKey:(NSString *)browserKey successfulBlock:(void (^)(LPPlaceDetailsResults *placeDetailsResults))successful failureBlock:(void (^)(LPGoogleStatus status))failure { + if ([self.delegate respondsToSelector:@selector(googleFunctionsWillLoadPlaceDetailsResult:forPlaceID:)]) { + [self.delegate googleFunctionsWillLoadPlaceDetailsResult:self forPlaceID:placeID]; } - NSString *newParams = [params substringToIndex:[params length]-1]; + AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; + manager.responseSerializer = [AFJSONResponseSerializer serializer]; + + NSMutableDictionary *parameters = [NSMutableDictionary new]; + if (browserKey) { + [parameters setObject:[NSString stringWithFormat:@"%@", browserKey] forKey:@"key"]; + } + else if (self.googleAPIBrowserKey) { + [parameters setObject:[NSString stringWithFormat:@"%@", self.googleAPIBrowserKey] forKey:@"key"]; + } + else { + [parameters setObject:[NSString stringWithFormat:@"%@", self.googleAPIClientID] forKey:@"client"]; + } - [manager GET:[NSString stringWithFormat:@"%@/%@?%@", googleAPIUri, googleAPIStaticMapImageURLPath, [newParams stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { + [parameters setObject:[NSString stringWithFormat:@"%@", placeID] forKey:@"placeid"]; + [parameters setObject:[NSString stringWithFormat:@"%@", self.languageCode] forKey:@"language"]; + + [manager GET:[NSString stringWithFormat:@"%@/%@?", googleAPIUri, googleAPIPlaceDetailsURLPath] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { - if(successful) - successful(responseObject); + LPPlaceDetailsResults *placeDetailsResults = [LPPlaceDetailsResults placeDetailsResultsWithObjects:responseObject]; + + NSString *statusCode = placeDetailsResults.statusCode; + + if ([statusCode isEqualToString:@"OK"]) { + if ([self.delegate respondsToSelector:@selector(googleFunctions:didLoadPlaceDetailsResult:)]) { + [self.delegate googleFunctions:self didLoadPlaceDetailsResult:placeDetailsResults]; + } + + if (successful) + successful(placeDetailsResults); + } else { + LPGoogleStatus status = [LPGoogleFunctions getGoogleStatusFromString:statusCode]; + + if ([self.delegate respondsToSelector:@selector(googleFunctions:errorLoadingPlaceDetailsResultWithStatus:)]) { + [self.delegate googleFunctions:self errorLoadingPlaceDetailsResultWithStatus:status]; + } + + if (failure) + failure(status); + } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - if(failure) - failure(error); + if ([self.delegate respondsToSelector:@selector(googleFunctions:errorLoadingPlacesAutocompleteWithStatus:)]) { + [self.delegate googleFunctions:self errorLoadingPlaceDetailsResultWithStatus:LPGoogleStatusUnknownError]; + } + + if (failure) + failure(LPGoogleStatusUnknownError); }]; }