Skip to content

Commit

Permalink
Merge pull request jverkoey#323 from mishagray/master
Browse files Browse the repository at this point in the history
native Google Maps support for NIInterapp
  • Loading branch information
jverkoey committed Dec 15, 2012
2 parents ad606e8 + 8b9d9da commit f836c05
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 19 deletions.
25 changes: 21 additions & 4 deletions src/interapp/src/NIInterapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
*/
@interface NIInterapp : NSObject

#pragma mark Chrome vs Safari

+ (void)setPreferGoogleChrome:(BOOL)preferGoogleChromeOverSafari;
+ (BOOL)preferGoogleChrome;
+ (BOOL)openPreferredBrowserWithURL:(NSURL *)url;

#pragma mark Safari

+ (BOOL)safariWithURL:(NSURL *)url;
Expand All @@ -38,11 +44,22 @@

#pragma mark Google Maps

+ (BOOL)googleMapsIsInstalled;
+ (BOOL)googleMaps;
+ (NSString *)googleMapsAppStoreId;

+ (BOOL)googleMapAtLocation:(CLLocationCoordinate2D)location;
+ (BOOL)googleMapAtLocation: (CLLocationCoordinate2D)location
title: (NSString *)title;
+ (BOOL)googleMapDirectionsFromLocation: (CLLocationCoordinate2D)fromLocation
toLocation: (CLLocationCoordinate2D)toLocation;
+ (BOOL)googleMapAtLocation: (CLLocationCoordinate2D)location title: (NSString *)title;
+ (BOOL)googleMapDirectionsFromLocation: (CLLocationCoordinate2D)fromLocation toLocation: (CLLocationCoordinate2D)toLocation;

// directionsMode can be nil. @"driving", @"transit", or @"walking".
+ (BOOL)googleMapDirectionsFromLocation: (CLLocationCoordinate2D)fromLocation toLocation: (CLLocationCoordinate2D)toLocation withMode:(NSString*)directionsMode;
+ (BOOL)googleMapDirectionsFromSourceAddress: (NSString*)srcAddr toDestAddress: (NSString*)destAddr withMode:(NSString*)directionsMode;

// these just use the user's current location (even if your application doesn't have locations services on, the google maps site/app MIGHT
+ (BOOL)googleMapDirectionsToDestAddress: (NSString*)destAddr withMode:(NSString*)directionsMode;
+ (BOOL)googleMapDirectionsToLocation: (CLLocationCoordinate2D)toLocation withMode:(NSString*)directionsMode;

+ (BOOL)googleMapWithQuery:(NSString *)query;

#pragma mark Phone
Expand Down
135 changes: 120 additions & 15 deletions src/interapp/src/NIInterapp.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
@implementation NIInterapp



///////////////////////////////////////////////////////////////////////////////////////////////////
+ (NSString *)sanitizedPhoneNumberFromString:(NSString *)string {
if (nil == string) {
Expand All @@ -41,6 +42,26 @@ + (NSString *)sanitizedPhoneNumberFromString:(NSString *)string {

}

#pragma mark Chrome vs Safari

BOOL sPreferGoogleChrome = NO;
+ (void)setPreferGoogleChrome:(BOOL)preferGoogleChrome
{
sPreferGoogleChrome = preferGoogleChrome;
}
+ (BOOL)preferGoogleChrome {
return sPreferGoogleChrome;
}

+ (BOOL)openPreferredBrowserWithURL:(NSURL *)url
{
if (sPreferGoogleChrome && [NIInterapp googleChromeIsInstalled]) {
return [NIInterapp googleChromeWithURL:url];
}
else {
return [NIInterapp safariWithURL:url];
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -113,43 +134,127 @@ + (NSString *)googleChromeAppStoreId {
* Source for URL information: http://mapki.com/wiki/Google_Map_Parameters
*/

static NSString* const sGoogleMapsScheme = @"comgooglemaps:";


///////////////////////////////////////////////////////////////////////////////////////////////////
+ (BOOL)googleMapAtLocation:(CLLocationCoordinate2D)location {
NSString* urlPath = [NSString stringWithFormat:
@"http://maps.google.com/maps?q=%f,%f",
location.latitude, location.longitude];
return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlPath]];
+ (BOOL)googleMapsIsInstalled {
return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:sGoogleMapsScheme]];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
+ (BOOL)googleMaps {
BOOL didOpen = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:sGoogleMapsScheme]];

if (!didOpen) {
didOpen = [self appStoreWithAppId:[self googleMapsAppStoreId]];
}

return didOpen;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
+ (NSString *)googleMapsAppStoreId {
return @"585027354";
}


///////////////////////////////////////////////////////////////////////////////////////////////////
+ (BOOL)openBestGoogleMapUrl:(NSString*)urlString{

if ([NIInterapp googleMapsIsInstalled]) {
NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"comgooglemaps://%@",urlString]];
return [[UIApplication sharedApplication] openURL:url];
}
else {
NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps%@",urlString]];
return [NIInterapp openPreferredBrowserWithURL:url];
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////
+ (BOOL)googleMapAtLocation:(CLLocationCoordinate2D)location {

NSString* urlPath = [NSString stringWithFormat:
@"?q=%f,%f",
location.latitude, location.longitude];
return [NIInterapp openBestGoogleMapUrl:urlPath];

}

///////////////////////////////////////////////////////////////////////////////////////////////////
+ (BOOL)googleMapAtLocation: (CLLocationCoordinate2D)location
title: (NSString *)title {

NSString* urlPath = [NSString stringWithFormat:
@"http://maps.google.com/maps?q=%@@%f,%f",
[title stringByAddingPercentEscapesForURLParameter], location.latitude, location.longitude];
return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlPath]];
@"?q=%@@%f,%f",
[title stringByAddingPercentEscapesForURLParameter], location.latitude, location.longitude];
return [NIInterapp openBestGoogleMapUrl:urlPath];

}


///////////////////////////////////////////////////////////////////////////////////////////////////
+ (BOOL)googleMapDirectionsFromLocation: (CLLocationCoordinate2D)fromLocation
toLocation: (CLLocationCoordinate2D)toLocation {
NSString* urlPath = [NSString stringWithFormat:
@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",
fromLocation.latitude, fromLocation.longitude,
toLocation.latitude, toLocation.longitude];
return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlPath]];
return [NIInterapp googleMapDirectionsFromLocation:fromLocation toLocation:toLocation withMode:nil];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
+ (BOOL)googleMapDirectionsFromLocation: (CLLocationCoordinate2D)fromLocation
toLocation: (CLLocationCoordinate2D)toLocation
withMode:(NSString *)directionsMode
{
NSString *saddr = [NSString stringWithFormat:@"%f,%f",fromLocation.latitude, fromLocation.longitude];
NSString *daddr = [NSString stringWithFormat:@"%f,%f",toLocation.latitude, toLocation.longitude];

return [NIInterapp googleMapDirectionsFromSourceAddress:saddr toDestAddress:daddr withMode:directionsMode];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
+ (BOOL)googleMapDirectionsToLocation: (CLLocationCoordinate2D)toLocation
withMode:(NSString *)directionsMode
{
NSString *daddr = [NSString stringWithFormat:@"%f,%f",toLocation.latitude, toLocation.longitude];

return [NIInterapp googleMapDirectionsFromSourceAddress:nil toDestAddress:daddr withMode:directionsMode];
}



///////////////////////////////////////////////////////////////////////////////////////////////////
+ (BOOL)googleMapDirectionsToDestAddress: (NSString*)destAddr withMode:(NSString *)directionsMode {
return [NIInterapp googleMapDirectionsFromSourceAddress:nil toDestAddress:destAddr withMode:directionsMode];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
+ (BOOL)googleMapDirectionsFromSourceAddress: (NSString*)srcAddr toDestAddress: (NSString*)destAddr withMode:(NSString *)directionsMode {

NSString* urlPath;
// source can be left blank == get current users location
if ([srcAddr length] > 0) {
urlPath = [NSString stringWithFormat:
@"?saddr=%@&daddr=%@",
srcAddr,destAddr];
}
else {
urlPath = [NSString stringWithFormat:
@"?daddr=%@",
destAddr];
}
if ([directionsMode length] > 0) {
urlPath = [NSString stringWithFormat:@"%@&directionsmode=%@",urlPath,directionsMode];
}
return [NIInterapp openBestGoogleMapUrl:urlPath];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
+ (BOOL)googleMapWithQuery:(NSString *)query {
NSString* urlPath = [NSString stringWithFormat:
@"http://maps.google.com/maps?q=%@",
@"?q=%@",
[query stringByAddingPercentEscapesForURLParameter]];
return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlPath]];
return [NIInterapp openBestGoogleMapUrl:urlPath ];
}


Expand Down

0 comments on commit f836c05

Please sign in to comment.