Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions CMMapLauncher/CMMapLauncher.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,27 @@ typedef NS_ENUM(NSUInteger, CMMapApp) {
CMMapAppTheTransitApp, // The Transit App
CMMapAppWaze, // Waze
CMMapAppYandex, // Yandex Navigator
CMMapAppUber, // Uber
};

@interface CMMapLauncher : NSObject

/**
Determines whether the given mapping app is installed.

@param mapApp An enumeration value identifying a mapping application.

@return YES if the specified app is installed, NO otherwise.
*/
+ (BOOL)isMapAppInstalled:(CMMapApp)mapApp;

/**
Launches the specified mapping application with directions
from the user's current location to the specified endpoint.

@param mapApp An enumeration value identifying a mapping application.
@param end The destination of the desired directions.

@return YES if the mapping app could be launched, NO otherwise.
*/
+ (BOOL)launchMapApp:(CMMapApp)mapApp
Expand All @@ -79,11 +80,11 @@ typedef NS_ENUM(NSUInteger, CMMapApp) {
/**
Launches the specified mapping application with directions
between the two specified endpoints.

@param mapApp An enumeration value identifying a mapping application.
@param start The starting point of the desired directions.
@param end The destination of the desired directions.

@return YES if the mapping app could be launched, NO otherwise.
*/
+ (BOOL)launchMapApp:(CMMapApp)mapApp
Expand Down Expand Up @@ -131,14 +132,14 @@ typedef NS_ENUM(NSUInteger, CMMapApp) {

/**
Creates a new CMMapPoint with the given geographical coordinate.

@param coordinate The geographical coordinate of the new map point.
*/
+ (CMMapPoint *)mapPointWithCoordinate:(CLLocationCoordinate2D)coordinate;

/**
Creates a new CMMapPoint with the given name and coordinate.

@param name The user-visible name of the new map point.
@param coordinate The geographical coordinate of the new map point.
*/
Expand All @@ -147,7 +148,7 @@ typedef NS_ENUM(NSUInteger, CMMapApp) {

/**
Creates a new CMMapPoint with the given name, address, and coordinate.

@param name The user-visible name of the new map point.
@param address The address string of the new map point.
@param coordinate The geographical coordinate of the new map point.
Expand All @@ -158,7 +159,7 @@ typedef NS_ENUM(NSUInteger, CMMapApp) {

/**
Creates a new CMMapPoint with the given name, address, and coordinate.

@param address The address string of the new map point.
@param coordinate The geographical coordinate of the new map point.
*/
Expand Down
54 changes: 32 additions & 22 deletions CMMapLauncher/CMMapLauncher.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,25 @@ + (NSString *)urlPrefixForMapApp:(CMMapApp)mapApp {
switch (mapApp) {
case CMMapAppCitymapper:
return @"citymapper://";

case CMMapAppGoogleMaps:
return @"comgooglemaps://";

case CMMapAppNavigon:
return @"navigon://";

case CMMapAppTheTransitApp:
return @"transit://";

case CMMapAppWaze:
return @"waze://";

case CMMapAppYandex:
return @"yandexnavi://";


case CMMapAppUber:
return @"uber://";

default:
return nil;
}
Expand All @@ -61,40 +64,40 @@ + (NSString *)urlEncode:(NSString *)queryParam {
// Encode all the reserved characters, per RFC 3986
// (<http://www.ietf.org/rfc/rfc3986.txt>)
NSString *newString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)queryParam, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8);

if (newString) {
return newString;
}

return @"";
}

+ (NSString *)googleMapsStringForMapPoint:(CMMapPoint *)mapPoint {
if (!mapPoint) {
return @"";
}

if (mapPoint.isCurrentLocation && mapPoint.coordinate.latitude == 0.0 && mapPoint.coordinate.longitude == 0.0) {
return @"";
}

if (mapPoint.name) {
return [NSString stringWithFormat:@"%f,%f+(%@)", mapPoint.coordinate.latitude, mapPoint.coordinate.longitude, [CMMapLauncher urlEncode:mapPoint.name]];
return [NSString stringWithFormat:@"%f,%f", mapPoint.coordinate.latitude, mapPoint.coordinate.longitude];
}

return [NSString stringWithFormat:@"%f,%f", mapPoint.coordinate.latitude, mapPoint.coordinate.longitude];
}

+ (BOOL)isMapAppInstalled:(CMMapApp)mapApp {
if (mapApp == CMMapAppAppleMaps) {
return YES;
}

NSString *urlPrefix = [CMMapLauncher urlPrefixForMapApp:mapApp];
if (!urlPrefix) {
return NO;
}

return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlPrefix]];
}

Expand All @@ -108,12 +111,12 @@ + (BOOL)launchMapApp:(CMMapApp)mapApp
if (![CMMapLauncher isMapAppInstalled:mapApp]) {
return NO;
}

if (mapApp == CMMapAppAppleMaps) {
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving};
NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeWalking};
return [MKMapItem openMapsWithItems:@[start.MKMapItem, end.MKMapItem] launchOptions:launchOptions];
} else { // iOS 5
NSString *url = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@",
Expand Down Expand Up @@ -152,7 +155,7 @@ + (BOOL)launchMapApp:(CMMapApp)mapApp
return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
} else if (mapApp == CMMapAppTheTransitApp) {
// http://thetransitapp.com/developers

NSMutableArray *params = [NSMutableArray arrayWithCapacity:2];
if (start && !start.isCurrentLocation) {
[params addObject:[NSString stringWithFormat:@"from=%f,%f", start.coordinate.latitude, start.coordinate.longitude]];
Expand All @@ -164,7 +167,7 @@ + (BOOL)launchMapApp:(CMMapApp)mapApp
return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
} else if (mapApp == CMMapAppNavigon) {
// http://www.navigon.com/portal/common/faq/files/NAVIGON_AppInteract.pdf

NSString *name = @"Destination"; // Doc doesn't say whether name can be omitted
if (end.name) {
name = end.name;
Expand All @@ -182,8 +185,15 @@ + (BOOL)launchMapApp:(CMMapApp)mapApp
url = [NSString stringWithFormat:@"yandexnavi://build_route_on_map?lat_to=%f&lon_to=%f&lat_from=%f&lon_from=%f", end.coordinate.latitude, end.coordinate.longitude, start.coordinate.latitude, start.coordinate.longitude];
}
return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
} else if (mapApp == CMMapAppUber) {
NSString *url = nil;
if (start.isCurrentLocation) {
url = [NSString stringWithFormat:@"uber://?action=setPickup&pickup=my_location&dropoff[latitude]=%f&dropoff[longitude]=%f&dropoff[nickname]=%@", end.coordinate.latitude, end.coordinate.longitude, [end.name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
} else {
url = [NSString stringWithFormat:@"uber://?action=setPickup&pickup[latitude]=%f&pickup[longitude]=%f&dropoff[latitude]=%f&dropoff[longitude]=%f&dropoff[nickname]=%@", start.coordinate.latitude, start.coordinate.longitude, end.coordinate.latitude, end.coordinate.longitude, [end.name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}
return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

return NO;
}

Expand Down Expand Up @@ -237,17 +247,17 @@ - (NSString *)name {
if (_isCurrentLocation) {
return @"Current Location";
}

return _name;
}

- (MKMapItem *)MKMapItem {
if (_isCurrentLocation) {
return [MKMapItem mapItemForCurrentLocation];
}

MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:_coordinate addressDictionary:nil];

MKMapItem *item = [[MKMapItem alloc] initWithPlacemark:placemark];
item.name = self.name;
return item;
Expand Down