How to use installed GPS systems in iPhone
I want to ask a user which GPS application he wants to use in order to take him to specific location. I guess i h开发者_开发技巧ave to check first which GPS apps are installed... How can i do that? When user selects specific app, how can i activate it and tell it to route to specific destination?
It lacks of resources, but it's doable ... Copy & paste of my old source code ... Just replace Tip
with something else. It's simple class, which holds locLatitude
and locLongitude
values with some more details. TomTom isn't supported, because I wasn't able to find URL scheme for it. As I no longer need it, I didn't try to search for it again.
typedef enum {
ExternalNavigationAppGoogleMaps,
ExternalNavigationAppNavigon,
ExternalNavigationAppTomTom
} ExternalNavigationApp;
+ (BOOL)isSupported:(ExternalNavigationApp)navigationApp;
+ (BOOL)navigateTo:(Tip *)tip viaApp:(ExternalNavigationApp)navigationApp;
+ (BOOL)navigateTo:(Tip *)tip from:(CLLocationCoordinate2D)from viaApp:(ExternalNavigationApp)navigationApp;
... implementation parts ...
+ (NSString *)googleMapsUrl:(Tip *)tip from:(CLLocationCoordinate2D)from {
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",
from.latitude, from.longitude,
[tip.locLatitude floatValue], [tip.locLongitude floatValue]];
return urlString;
}
+ (NSString *)navigonAppUrl:(Tip *)tip {
NSString *urlString = [NSString stringWithFormat:@"navigon://%@|%@||||||%f|%f",
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"],
tip.name,
[tip.locLongitude floatValue],
[tip.locLatitude floatValue]];
return urlString;
}
+ (NSString *)tomtomAppUrl:(Tip *)tip {
return nil;
}
+ (NSURL *)urlForApp:(ExternalNavigationApp)navigationApp withTip:(Tip *)tip from:(CLLocationCoordinate2D)from {
NSString *urlString = nil;
if ( ExternalNavigationAppNavigon == navigationApp ) {
urlString = [self navigonAppUrl:tip];
} else if ( ExternalNavigationAppTomTom == navigationApp ) {
urlString = [self tomtomAppUrl:tip];
} else if ( ExternalNavigationAppGoogleMaps == navigationApp ) {
urlString = [self googleMapsUrl:tip from:from];
}
if ( urlString == nil )
return nil;
return [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
}
+ (BOOL)isSupported:(ExternalNavigationApp)navigationApp {
CLLocationCoordinate2D coord;
NSURL *url = [self urlForApp:navigationApp withTip:nil from:coord];
if ( url == nil )
return NO;
return [[UIApplication sharedApplication]canOpenURL:url];
}
+ (BOOL)navigateTo:(Tip *)tip viaApp:(ExternalNavigationApp)navigationApp {
CLLocationCoordinate2D coord;
NSURL *url = [self urlForApp:navigationApp withTip:tip from:coord];
if ( url == nil )
return NO;
return [[UIApplication sharedApplication] openURL:url];
}
+ (BOOL)navigateTo:(Tip *)tip from:(CLLocationCoordinate2D)from viaApp:(ExternalNavigationApp)navigationApp {
NSURL *url = [self urlForApp:navigationApp withTip:tip from:from];
if ( url == nil )
return NO;
return [[UIApplication sharedApplication] openURL:url];
}
精彩评论