forked from googlearchive/OpenInGoogleMaps-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenInGoogleMapsController.m
More file actions
544 lines (458 loc) · 17.4 KB
/
OpenInGoogleMapsController.m
File metadata and controls
544 lines (458 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//
// OpenInGoogleMapsController.m
//
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import <CoreLocation/CoreLocation.h>
#import "OpenInGoogleMapsController.h"
// Constants for URL schemes and arguments defined by Google Maps and Chrome.
static NSString * const kGoogleMapsScheme = @"comgooglemaps://";
static NSString * const kGoogleMapsCallbackScheme = @"comgooglemaps-x-callback://";
static NSString* const kGoogleChromeOpenLink =
@"googlechrome-x-callback://x-callback-url/open/?url=";
static NSString * const kGoogleMapsStringTraffic = @"traffic";
static NSString * const kGoogleMapsStringTransit = @"transit";
static NSString * const kGoogleMapsStringSatellite = @"satellite";
/*
* Helper method that percent-escapes a string so it can safely be used in a URL.
*/
static NSString *encodeByAddingPercentEscapes(NSString *input) {
NSString *encodedValue = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
kCFAllocatorDefault, (CFStringRef) input, NULL, (CFStringRef) @"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
return encodedValue;
}
/*
* The GoogleMapsURLSchemable protocol states that any definition can be exported into an
* array of URL arguments that can then be used to open Google Maps, Apple Maps, or a web page.
* It's through these methods that the definitions do most of the "heavy lifting" required to
* convert themselves into a URL that can be opened in the appropriate app.
*
* The first three methods return arrays of strings that represent URL arguments in the form of
* "foo=bar". These can then by joined by ampersands (and prepended by a question mark) to create
* a full URL.
*/
@protocol GoogleMapsURLSchemable
@required
- (NSArray *)URLArgumentsForGoogleMaps;
- (NSArray *)URLArgumentsForAppleMaps;
- (NSArray *)URLArgumentsForWeb;
- (BOOL)anythingToSearchFor;
@end
/*
* GoogleMapDefinition - A definition for opening up a location in a map.
*/
@interface GoogleMapDefinition() <GoogleMapsURLSchemable>
@end
@implementation GoogleMapDefinition
- (instancetype)init {
self = [super init];
if (self) {
_center = kCLLocationCoordinate2DInvalid;
}
return self;
}
- (BOOL)anythingToSearchFor {
return (CLLocationCoordinate2DIsValid(self.center) || self.queryString);
}
- (NSArray *)URLArgumentsForGoogleMaps {
NSMutableArray *urlArguments = [NSMutableArray array];
if (self.queryString) {
[urlArguments addObject:
[NSString stringWithFormat:@"q=%@", encodeByAddingPercentEscapes(self.queryString)]];
}
if (CLLocationCoordinate2DIsValid(self.center)) {
[urlArguments addObject:[NSString stringWithFormat:@"center=%f,%f",
self.center.latitude, self.center.longitude]];
}
if (self.zoomLevel > 0) {
[urlArguments addObject:[NSString stringWithFormat:@"zoom=%f", self.zoomLevel]];
}
if (self.viewOptions) {
NSMutableArray *viewsToShow = [NSMutableArray arrayWithCapacity:3];
if (self.viewOptions & kGoogleMapsViewOptionSatellite) {
[viewsToShow addObject:kGoogleMapsStringSatellite];
}
if (self.viewOptions & kGoogleMapsViewOptionTraffic) {
[viewsToShow addObject:kGoogleMapsStringTraffic];
}
if (self.viewOptions & kGoogleMapsViewOptionTransit) {
[viewsToShow addObject:kGoogleMapsStringTransit];
}
[urlArguments addObject:
[NSString stringWithFormat:@"views=%@", [viewsToShow componentsJoinedByString:@","]]];
}
return urlArguments;
}
- (NSArray *)URLArgumentsForAppleMaps {
NSMutableArray *urlArguments = [NSMutableArray array];
if (self.queryString) {
[urlArguments addObject:
[NSString stringWithFormat:@"q=%@", encodeByAddingPercentEscapes(self.queryString)]];
}
if (CLLocationCoordinate2DIsValid(self.center)) {
[urlArguments addObject:[NSString stringWithFormat:@"ll=%f,%f",
self.center.latitude, self.center.longitude]];
}
if (self.zoomLevel > 0) {
[urlArguments addObject:[NSString stringWithFormat:@"z=%d", (int)self.zoomLevel]];
}
// Apple Map's "Hybrid" view is closest to what Google's "Satellite" view looks like
if (self.viewOptions & kGoogleMapsViewOptionSatellite) {
[urlArguments addObject:@"t=h"];
}
// TODO: Figure out what URL scheme argument enables traffic information.
return urlArguments;
}
- (NSArray *)URLArgumentsForWeb {
NSMutableArray *urlArguments = [NSMutableArray array];
if (self.queryString) {
[urlArguments addObject:
[NSString stringWithFormat:@"q=%@", encodeByAddingPercentEscapes(self.queryString)]];
}
if (CLLocationCoordinate2DIsValid(self.center)) {
[urlArguments addObject:[NSString stringWithFormat:@"ll=%f,%f",
self.center.latitude, self.center.longitude]];
}
if (self.zoomLevel > 0) {
[urlArguments addObject:[NSString stringWithFormat:@"z=%d", (int)self.zoomLevel]];
}
if (self.viewOptions & kGoogleMapsViewOptionSatellite) {
[urlArguments addObject:@"t=h"];
}
if (self.viewOptions & kGoogleMapsViewOptionTraffic) {
[urlArguments addObject:@"layer=t"];
}
if (self.viewOptions & kGoogleMapsViewOptionTransit) {
[urlArguments addObject:@"lci=transit_comp"];
}
return urlArguments;
}
@end
/*
* GoogleStreetViewDefinition - A definition for opening up a location in Street View.
*/
@interface GoogleStreetViewDefinition() <GoogleMapsURLSchemable>
@end
@implementation GoogleStreetViewDefinition
- (instancetype)init {
self = [super init];
if (self) {
_center = kCLLocationCoordinate2DInvalid;
}
return self;
}
- (BOOL)anythingToSearchFor {
return CLLocationCoordinate2DIsValid(self.center);
}
- (NSArray *)URLArgumentsForGoogleMaps {
NSMutableArray *urlArguments = [NSMutableArray array];
[urlArguments addObject:
[NSString stringWithFormat:@"center=%f,%f", self.center.latitude, self.center.longitude]];
[urlArguments addObject:@"mapmode=streetview"];
return urlArguments;
}
/*
* Apple Maps doesn't support Street View, but we can zoom in to the general location with
* satellite view. That's pretty close.
*/
- (NSArray *)URLArgumentsForAppleMaps {
NSMutableArray *urlArguments = [NSMutableArray array];
[urlArguments addObject:
[NSString stringWithFormat:@"ll=%f,%f", self.center.latitude, self.center.longitude]];
[urlArguments addObject:@"z=19"];
[urlArguments addObject:@"t=k"];
return urlArguments;
}
/*
* Currently, we are unable to open a link to Street View in our mobile web browser. But we
* can zoom in with satellite view just like we do in Apple Maps
*/
- (NSArray *)URLArgumentsForWeb {
return [self URLArgumentsForAppleMaps];
}
@end
/*
* GoogleDirectionsWaypoint - A point defined by either a set of coordinates or a search string.
* Used by the GoogleDirectionsDefinition classs.
*/
@interface GoogleDirectionsWaypoint()
@end
@implementation GoogleDirectionsWaypoint
- (instancetype)init {
self = [super init];
if (self) {
_location = kCLLocationCoordinate2DInvalid;
}
return self;
}
/*
* Since waypoints should contain a location or a query string (but not both),
* these static helper methods can be quite handy.
*/
+ (instancetype)waypointWithLocation:(CLLocationCoordinate2D)location {
GoogleDirectionsWaypoint *waypoint = [[GoogleDirectionsWaypoint alloc] init];
waypoint.location = location;
return waypoint;
}
+ (instancetype)waypointWithQuery:(NSString *)queryString {
GoogleDirectionsWaypoint *waypoint = [[GoogleDirectionsWaypoint alloc] init];
waypoint.queryString = queryString;
return waypoint;
}
- (BOOL)anythingToSearchFor {
return (CLLocationCoordinate2DIsValid(self.location) || self.queryString);
}
/*
* Since a waypoint could be the start address ('saddr' in the URL) or the end address ('daddr'),
* we need to pass in the proper key when retrieving the URL argument for this waypoint.
*/
- (NSString *)URLArgumentUsingKey:(NSString *)key {
if (CLLocationCoordinate2DIsValid(self.location)) {
return [NSString stringWithFormat:@"%@=%f,%f",
key, self.location.latitude, self.location.longitude];
} else if (self.queryString) {
return [NSString stringWithFormat:@"%@=%@",
key, encodeByAddingPercentEscapes(self.queryString)];
} else {
return @"";
}
}
@end
/*
* GoogleDirectionsWaypoint - A point defined by either a set of coordinates or a search string.
* Used by the GoogleDirectionsDefinition classs.
*/
@interface GoogleDirectionsDefinition() <GoogleMapsURLSchemable>
@end
@implementation GoogleDirectionsDefinition
- (BOOL)anythingToSearchFor {
return ([self.startingPoint anythingToSearchFor] || [self.destinationPoint anythingToSearchFor]);
}
/*
* Retrieving the "travel mode" argument in Google Maps.
*/
- (NSString *)urlArgumentValueForTravelMode {
switch (self.travelMode) {
case kGoogleMapsTravelModeBiking:
return @"bicycling";
case kGoogleMapsTravelModeDriving:
return @"driving";
case kGoogleMapsTravelModeTransit:
return @"transit";
case kGoogleMapsTravelModeWalking:
return @"walking";
}
return nil;
}
/*
* Retrieving the "travel mode" argument for the web.
*/
- (NSString *)urlArgumentValueForTravelModeWeb {
switch (self.travelMode) {
case kGoogleMapsTravelModeBiking:
return @"b";
case kGoogleMapsTravelModeDriving:
return @"c";
case kGoogleMapsTravelModeTransit:
return @"r";
case kGoogleMapsTravelModeWalking:
return @"w";
}
return nil;
}
/*
* Retrieving the start and end waypoint arguments is the same in Google Maps, Apple Maps, and
* the web.
*/
- (NSMutableArray *)waypointArguments {
NSMutableArray *waypointArguments = [NSMutableArray array];
if ([self.startingPoint anythingToSearchFor]) {
[waypointArguments addObject:[self.startingPoint URLArgumentUsingKey:@"saddr"]];
}
if ([self.destinationPoint anythingToSearchFor]) {
[waypointArguments addObject:[self.destinationPoint URLArgumentUsingKey:@"daddr"]];
}
return waypointArguments;
}
- (NSArray *)URLArgumentsForGoogleMaps {
NSMutableArray *urlArguments = [self waypointArguments];
NSString *travelMode = [self urlArgumentValueForTravelMode];
if (travelMode) {
[urlArguments addObject:[NSString stringWithFormat:@"directionsmode=%@", travelMode]];
}
return urlArguments;
}
- (NSArray *)URLArgumentsForAppleMaps {
NSMutableArray *urlArguments = [self waypointArguments];
if (self.travelMode == kGoogleMapsTravelModeDriving) {
[urlArguments addObject:@"dirflg=d"];
} else if (self.travelMode == kGoogleMapsTravelModeWalking) {
[urlArguments addObject:@"dirflg=w"];
}
return urlArguments;
}
- (NSArray *)URLArgumentsForWeb {
NSMutableArray *urlArguments = [self waypointArguments];
NSString *travelMode = [self urlArgumentValueForTravelModeWeb];
if (travelMode) {
[urlArguments addObject:[NSString stringWithFormat:@"dirflg=%@", travelMode]];
}
return urlArguments;
}
@end
/*
* OpenInGoogleMapsController - The main class that creates and opens a URL to display a particular
* map in Google Maps (or an alternative application is a fallback strategy is specified).
*
* Most of the work required to take a definition and open it up in the proper application
* is done by the definitions themselves. This class creates the "outer bits" of the URL (like
* the base URL and any x-callback-url data at the end), and takes care of the logic for performing
* the various fallback strategies.
*/
@implementation OpenInGoogleMapsController {
UIApplication *_sharedApplication;
}
+ (OpenInGoogleMapsController *)sharedInstance {
static OpenInGoogleMapsController *_sharedInstance;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
- (instancetype)init {
self = [super init];
if (self) {
_sharedApplication = [UIApplication sharedApplication];
}
return self;
}
- (BOOL)isGoogleMapsInstalled {
NSURL *simpleURL = [NSURL URLWithString:kGoogleMapsScheme];
NSURL *callbackURL = [NSURL URLWithString:kGoogleMapsCallbackScheme];
return ([_sharedApplication canOpenURL:simpleURL] ||
[_sharedApplication canOpenURL:callbackURL]);
}
- (BOOL)fallBackToAppleMapsWithDefinition:(id<GoogleMapsURLSchemable>)definition {
NSMutableString *mapURL = [@"https://maps.apple.com/" mutableCopy];
[mapURL appendString:[NSString stringWithFormat:@"?%@",
[[definition URLArgumentsForAppleMaps] componentsJoinedByString:@"&"]]];
#if DEBUG
NSLog(@"Opening up URL: %@", mapURL);
#endif
NSURL *URLToOpen = [NSURL URLWithString:mapURL];
return [_sharedApplication openURL:URLToOpen];
}
- (BOOL)fallbackToChromeFirstWithDefinition:(id<GoogleMapsURLSchemable>)definition {
NSMutableString *mapURL = [kGoogleChromeOpenLink mutableCopy];
NSString *embedURL = @"https://maps.google.com/maps/";
NSString *urlArgumentsAsString = [NSString stringWithFormat:@"?%@",
[[definition URLArgumentsForWeb] componentsJoinedByString:@"&"]];
NSString *fullEmbedURL = [embedURL stringByAppendingString:urlArgumentsAsString];
[mapURL appendString:encodeByAddingPercentEscapes(fullEmbedURL)];
#if DEBUG
NSLog(@"Opening up URL: %@", mapURL);
NSLog(@"Embedded URL of: %@", fullEmbedURL);
#endif
[self appendMapURLString:mapURL withCallbackArgumentsFromURL:self.callbackURL];
NSURL *URLToOpen = [NSURL URLWithString:mapURL];
if ([_sharedApplication openURL:URLToOpen]) {
return YES;
} else if (self.fallbackStrategy == kGoogleMapsFallbackChromeThenAppleMaps) {
return [self fallBackToAppleMapsWithDefinition:definition];
} else if (self.fallbackStrategy == kGoogleMapsFallbackChromeThenSafari) {
return [self fallbackToSafariWithDefinition:definition];
}
return NO;
}
- (BOOL)fallbackToSafariWithDefinition:(id<GoogleMapsURLSchemable>)definition {
NSMutableString *mapURL = [@"https://maps.google.com/maps" mutableCopy];
[mapURL appendString:[NSString stringWithFormat:@"?%@",
[[definition URLArgumentsForWeb] componentsJoinedByString:@"&"]]];
#if DEBUG
NSLog(@"Opening up URL: %@", mapURL);
#endif
NSURL *URLToOpen = [NSURL URLWithString:mapURL];
return [_sharedApplication openURL:URLToOpen];
}
/*
* Since the definitions themselves do most of the work required to generate the URL arguments
* appropriate for their type of map request, the same method can be used to open up the correct
* URL, no matter what definition gets passed in. We chose not to make this method public,
* simply because the more explicit methods below eliminate potential confusion.
*/
- (BOOL)openInGoogleMapsWithDefinition:(id<GoogleMapsURLSchemable>)definition {
// Did we define anything to search for in our map?
if (![definition anythingToSearchFor]) {
return NO;
}
// Can we open this in google maps?
if (![self isGoogleMapsInstalled]) {
switch (self.fallbackStrategy) {
case kGoogleMapsFallbackNone:
return NO;
case kGoogleMapsFallbackAppleMaps:
return [self fallBackToAppleMapsWithDefinition:definition];
case kGoogleMapsFallbackChromeThenSafari:
case kGoogleMapsFallbackChromeThenAppleMaps:
return [self fallbackToChromeFirstWithDefinition:definition];
case kGoogleMapsFallbackSafari:
return [self fallbackToSafariWithDefinition:definition];
}
}
NSMutableString *mapURL = [[self baseURLStringUsingCallback:self.callbackURL] mutableCopy];
[mapURL appendString:[NSString stringWithFormat:@"?%@",
[[definition URLArgumentsForGoogleMaps] componentsJoinedByString:@"&"]]];
[self appendMapURLString:mapURL withCallbackArgumentsFromURL:self.callbackURL];
#if DEBUG
NSLog(@"Opening up URL: %@", mapURL);
#endif
NSURL *URLToOpen = [NSURL URLWithString:mapURL];
return [_sharedApplication openURL:URLToOpen];
}
- (BOOL)openMap:(GoogleMapDefinition *)definition {
return [self openInGoogleMapsWithDefinition:definition];
}
- (BOOL)openStreetView:(GoogleStreetViewDefinition *)definition {
return [self openInGoogleMapsWithDefinition:definition];
}
- (BOOL)openDirections:(GoogleDirectionsDefinition *)definition {
return [self openInGoogleMapsWithDefinition:definition];
}
# pragma mark - Map URL fragment methods
/*
* Returns the correct URL scheme (comgooglemaps vs comgooglemaps-x-callback),
* depending on whether or not the callback URL can be opened.
*/
- (NSString *)baseURLStringUsingCallback:(NSURL *)callbackURL {
BOOL usingCallback = callbackURL && [_sharedApplication canOpenURL:callbackURL];
return (usingCallback) ? kGoogleMapsCallbackScheme : kGoogleMapsScheme;
}
/*
* Add the x-success and x-source arguments to the end of an URL string, if the callback URL
* exists and is supported by the target app.
*/
- (void)appendMapURLString:(NSMutableString *)mapURL
withCallbackArgumentsFromURL:(NSURL *)callbackURL {
BOOL usingCallback = callbackURL && [_sharedApplication canOpenURL:callbackURL];
if (usingCallback) {
[mapURL appendFormat:@"&x-success=%@",
encodeByAddingPercentEscapes([callbackURL absoluteString])];
[mapURL appendFormat:@"&x-source=%@",
encodeByAddingPercentEscapes(
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"])];
}
}
@end