代码语言:javascript复制
#pragma mark - 弹出选择地图alert
(void)popMapsAlertWithVC:(UIViewController *)vc toCoor:(CLLocationCoordinate2D)toCoor targetName:(NSString *)targetName {
NSArray *mapSchemeArr = @[@"iosamap://", @"baidumap://", @"qqmap://", @"comgooglemaps://"];
NSArray *mapName = @[@"高德地图", @"百度地图", @"腾讯地图", @"谷歌地图"];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"导航" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
for (int i = 0; i < mapSchemeArr.count; i ) {
NSURL *url = [NSURL URLWithString:mapSchemeArr[i]];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
UIAlertAction *action = [UIAlertAction actionWithTitle:mapName[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[WWTKTool openMapWith:mapName[i] toCoor:toCoor targetName:targetName];
}];
[alert addAction:action];
}
}
UIAlertAction *actionSystem = [UIAlertAction actionWithTitle:@"系统地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[WWTKTool openAppleMapWithToCoor:toCoor targetName:targetName];
}];
[alert addAction:actionSystem];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancelAction];
[vc presentViewController:alert animated:YES completion:nil];
}
#pragma mark - 打开对应的导航app
(void)openMapWith:(NSString *)mapName toCoor:(CLLocationCoordinate2D)toCoor targetName:(NSString *)targetName {
if ([mapName isEqualToString:@"高德地图"]) {
NSString *urlString = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=applicationName&backScheme=com.mobvoi.one&lat=%f&lon=%f&dev=0&style=2", toCoor.latitude, toCoor.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
} else if ([mapName isEqualToString:@"百度地图"]) {
NSString *urlString = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=%@&mode=driving&coord_type=gcj02", toCoor.latitude, toCoor.longitude, targetName] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [[NSURL alloc] initWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
} else if ([mapName isEqualToString:@"腾讯地图"]) {
NSString *urlString = [[NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=%f,%f&coord_type=1&policy=0", toCoor.latitude, toCoor.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [[NSURL alloc] initWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
} else if ([mapName isEqualToString:@"谷歌地图"]) {
NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?daddr=%@&sll=%.8f,%.8f&directionsmode=transit", targetName, toCoor.latitude, toCoor.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [[NSURL alloc] initWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
}
#pragma mark - 打开苹果地图
(void)openAppleMapWithToCoor:(CLLocationCoordinate2D)toCoor targetName:(NSString *)targetName {
MKMapItem *location = [MKMapItem mapItemForCurrentLocation];
CLLocationCoordinate2D gc02Coor = [TQLocationConverter transformFromBaiduToGCJ:toCoor];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:gc02Coor addressDictionary:nil];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:placemark];
toLocation.name = targetName;
NSArray *items = [NSArray arrayWithObjects:location, toLocation, nil];
NSDictionary *options = @{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeWalking,
MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard),
MKLaunchOptionsShowsTrafficKey:@YES}; //打开苹果自身地图应用,并呈现特定的item
[MKMapItem openMapsWithItems:items launchOptions:options];
}