Skip to content

Commit

Permalink
Prevent unnecessary geocoding of addresses to coordinates in iOS. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dpa99c committed Sep 14, 2016
1 parent 7abd0d6 commit 516ffd7
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 73 deletions.
19 changes: 17 additions & 2 deletions src/ios/CMMapLauncher.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ typedef NS_ENUM(NSUInteger, CMMapApp) {

};

/**
Indicates an empty address
*/
NSString* CMEmptyAddress;

/**
Indicates an empty coordinate
*/
CLLocationCoordinate2D CMEmptyCoord;

/**
Indicates an empty latitude or longitude component
*/
static const CLLocationDegrees CMEmptyLocation = -1000.0;

@interface CMMapLauncher : NSObject

/**
Expand Down Expand Up @@ -186,7 +201,7 @@ typedef NS_ENUM(NSUInteger, CMMapApp) {
/**
Gives an MKMapItem corresponding to this map point object.
*/
@property (nonatomic, retain) MKMapItem *MKMapItem;
@property (nonatomic, retain) MKMapItem *mapItem;

/**
Creates a new CMMapPoint that signifies the current location.
Expand Down Expand Up @@ -235,4 +250,4 @@ typedef NS_ENUM(NSUInteger, CMMapApp) {
address:(NSString *)address
coordinate:(CLLocationCoordinate2D)coordinate;

@end
@end
90 changes: 42 additions & 48 deletions src/ios/CMMapLauncher.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ @implementation CMMapLauncher

+ (void)initialize {
debugEnabled = FALSE;
CMEmptyCoord = CLLocationCoordinate2DMake(CMEmptyLocation, CMEmptyLocation);
CMEmptyAddress = nil;
}

+ (void)enableDebugLogging{
Expand Down Expand Up @@ -182,11 +184,12 @@ + (BOOL)launchMapApp:(CMMapApp)mapApp
if (![CMMapLauncher isMapAppInstalled:mapApp]) {
return NO;
}

Class mapItemClass = [MKMapItem class];
bool gte_iOS6 = mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)];

if (mapApp == CMMapAppAppleMaps) {
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
if (mapApp == CMMapAppAppleMaps && gte_iOS6) {

NSDictionary* launchOptions;
if (directionsMode) {
if([directionsMode isEqual: @"walking"]){
Expand All @@ -208,34 +211,32 @@ + (BOOL)launchMapApp:(CMMapApp)mapApp
launchOptions = @{key: [extras objectForKey:key]};
}
}

if(start.mapItem == nil){
start.mapItem = [MKMapItem mapItemForCurrentLocation];
}

[self logDebug:[NSString stringWithFormat:@"Launching Apple Maps: destAddress=%@; destLatLon=%f,%f; destName=%@; startAddress=%@; startLatLon=%f,%f; startName=%@; directionsMode=%@; extras=%@",
end.address,
end.coordinate.latitude, end.coordinate.longitude,
end.name,
start.address,
start.coordinate.latitude, start.coordinate.longitude,
start.name,
directionsMode,
extras]];

return [MKMapItem openMapsWithItems:@[start.MKMapItem, end.MKMapItem] launchOptions:launchOptions];
} else { // iOS 5
NSMutableString* url = [NSMutableString stringWithFormat:@"http://maps.google.com/maps?saddr=%@&daddr=%@",
[CMMapLauncher googleMapsStringForMapPoint:start],
[CMMapLauncher googleMapsStringForMapPoint:end]
];
if(extras){
[url appendFormat:@"%@", [self extrasToQueryParams:extras]];
}
[self logDebugURI:url];
return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
return [MKMapItem openMapsWithItems:@[start.mapItem, end.mapItem] launchOptions:launchOptions];

} else if (mapApp == CMMapAppGoogleMaps || (mapApp == CMMapAppAppleMaps && !gte_iOS6)) {
NSString* startStr;
if([self isEmptyCoordinate:start.coordinate]){
startStr = [start.address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}else{
startStr = [CMMapLauncher googleMapsStringForMapPoint:start];
}
} else if (mapApp == CMMapAppGoogleMaps) {

NSString* endStr;
if([self isEmptyCoordinate:end.coordinate]){
endStr = [end.address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}else{
endStr = [CMMapLauncher googleMapsStringForMapPoint:end];
}

NSMutableString* url = [[NSString stringWithFormat:@"%@?saddr=%@&daddr=%@",
[self urlPrefixForMapApp:CMMapAppGoogleMaps],
[CMMapLauncher googleMapsStringForMapPoint:start],
[CMMapLauncher googleMapsStringForMapPoint:end]
startStr,
endStr
] mutableCopy];
if (directionsMode) {
[url appendFormat:@"&directionsmode=%@", directionsMode];
Expand All @@ -249,7 +250,9 @@ + (BOOL)launchMapApp:(CMMapApp)mapApp
} else if (mapApp == CMMapAppCitymapper) {
NSMutableArray* params = [NSMutableArray arrayWithCapacity:10];
if (start && !start.isCurrentLocation) {
[params addObject:[NSString stringWithFormat:@"startcoord=%f,%f", start.coordinate.latitude, start.coordinate.longitude]];
if(![self isEmptyCoordinate:start.coordinate]){
[params addObject:[NSString stringWithFormat:@"startcoord=%f,%f", start.coordinate.latitude, start.coordinate.longitude]];
}
if (start.name) {
[params addObject:[NSString stringWithFormat:@"startname=%@", [CMMapLauncher urlEncode:start.name]]];
}
Expand All @@ -258,7 +261,9 @@ + (BOOL)launchMapApp:(CMMapApp)mapApp
}
}
if (end && !end.isCurrentLocation) {
[params addObject:[NSString stringWithFormat:@"endcoord=%f,%f", end.coordinate.latitude, end.coordinate.longitude]];
if(![self isEmptyCoordinate:end.coordinate]){
[params addObject:[NSString stringWithFormat:@"endcoord=%f,%f", end.coordinate.latitude, end.coordinate.longitude]];
}
if (end.name) {
[params addObject:[NSString stringWithFormat:@"endname=%@", [CMMapLauncher urlEncode:end.name]]];
}
Expand Down Expand Up @@ -372,14 +377,10 @@ + (BOOL)launchMapApp:(CMMapApp)mapApp
}else{
directionsMode = @"drive";
}
NSString* separator = @"%7C";
NSMutableString* url = [NSMutableString stringWithFormat:@"%@coordinate%@%f%@%f%@%@",
NSMutableString* url = [NSMutableString stringWithFormat:@"%@coordinate|%f|%f|%@",
[self urlPrefixForMapApp:CMMapAppSygic],
separator,
end.coordinate.longitude,
separator,
end.coordinate.latitude,
separator,
directionsMode];
[self logDebugURI:url];
return [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
Expand Down Expand Up @@ -425,7 +426,6 @@ + (BOOL)launchMapApp:(CMMapApp)mapApp
[url appendFormat:@"&dest_name=%@", [end.name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}

NSMutableString* startParam;
if (!start.isCurrentLocation) {
[url appendFormat:@"&orig_lat=%f&orig_lon=%f",
start.coordinate.latitude, start.coordinate.longitude];
Expand All @@ -444,6 +444,11 @@ + (BOOL)launchMapApp:(CMMapApp)mapApp
return NO;
}

+ (bool) isEmptyCoordinate:(CLLocationCoordinate2D)coordinate
{
return coordinate.latitude == CMEmptyLocation && coordinate.longitude == CMEmptyLocation;
}

@end


Expand Down Expand Up @@ -498,24 +503,13 @@ - (NSString*)name {
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;
}

+ (CMMapPoint*)mapPointWithMapItem:(MKMapItem*)mapItem
name:(NSString*)name
address:(NSString*)address
coordinate:(CLLocationCoordinate2D)coordinate{
CMMapPoint* mapPoint = [[CMMapPoint alloc] init];
mapPoint.MKMapItem = mapItem;
mapPoint.mapItem = mapItem;
mapPoint.name = name;
mapPoint.address = address;
mapPoint.coordinate = coordinate;
Expand Down
Loading

0 comments on commit 516ffd7

Please sign in to comment.