iOS调起地图导航(百度、高德、腾讯、苹果自带)

//查看线路

- (void)clickLine:(UIButton *)sender{

NSMutableArray *mapArr = [NSMutableArray arrayWithCapacity:0];
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]){
    [mapArr addObject:@"百度地图"];
}
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]){
    [mapArr addObject:@"高德地图"];
}
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]){
    [mapArr addObject:@"腾讯地图"];
}
[mapArr addObject:@"苹果地图"];
if (mapArr.count == 1) {
    [self JumpToMap:mapArr[0]];
}else if(mapArr.count > 0){
    UIAlertController *mapAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    for (NSString *mapName in mapArr) {
        UIAlertAction *Action = [UIAlertAction actionWithTitle:mapName style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self JumpToMap:action.title];
        }];
        [mapAlert addAction:Action];
    }
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [mapAlert addAction:cancelAction];
    [self presentViewController:mapAlert animated:YES completion:nil];
}else{
    [self JumpToMap:@"苹果地图"];
}

}

//选择地图

- (void)JumpToMap:(NSString *)mapName{

if ([mapName isEqualToString:@"苹果地图"]) {
    [self appleMap];
}else if ([mapName isEqualToString:@"百度地图"]){
    [self BaiduMap];
}else if ([mapName isEqualToString:@"高德地图"]){
    [self iosMap];
}else if ([mapName isEqualToString:@"腾讯地图"]){
    [self qqMap];
}

}

//百度地图

- (void)BaiduMap{

float lat = [self.detailsDataSource[@"latitude"] doubleValue];
float lng = [self.detailsDataSource[@"longitude"] doubleValue];
CLLocationCoordinate2D gcj02Coord = [self BD09FromGCJ02:CLLocationCoordinate2DMake(lat, lng)];
float latitude = gcj02Coord.latitude;
float longitude = gcj02Coord.longitude;

//mode=driving(驾车), transit(公交) ...
NSString *urlString = [NSString stringWithFormat:@"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&mode=driving&coord_type= bd09ll", latitude, longitude];

if (latitude != 0 && longitude != 0) {
    urlString = [NSString stringWithFormat:@"%@&destination=latlng:%f,%f|name:%@", urlString, latitude, longitude, self.detailsDataSource[@"parkName"]];
}else{
    urlString = [NSString stringWithFormat:@"%@&destination=%@|name:%@", urlString, self.detailsDataSource[@"parkName"], self.detailsDataSource[@"parkName"]];
}

urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
    
}];

}

//高德地图

- (void)iosMap{

float latitude = [self.detailsDataSource[@"latitude"] doubleValue];
float longitude = [self.detailsDataSource[@"longitude"] doubleValue];
NSString *urlString = [NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&backScheme=%@&dev=0&&style=2", ZBLocalized(@"易泊车", nil), @"yiboche"];
if (latitude != 0 && longitude != 0) {
    urlString = [NSString stringWithFormat:@"%@&dlat=%f&dlon=%f&dname=%@", urlString, latitude, longitude ,self.detailsDataSource[@"parkName"]];
}else{
    urlString = [NSString stringWithFormat:@"%@&dname=%@",urlString, self.detailsDataSource[@"parkName"]];
}
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
    
}];

}

//腾讯地图

- (void)qqMap{

float lat = [self.detailsDataSource[@"latitude"] doubleValue];
float lng = [self.detailsDataSource[@"longitude"] doubleValue];
CLLocationCoordinate2D gcj02Coord = [self BD09FromGCJ02:CLLocationCoordinate2DMake(lat, lng)];
float latitude = gcj02Coord.latitude;
float longitude = gcj02Coord.longitude;
NSString *urlString = [NSString stringWithFormat:@"qqmap://map/routeplan?type=drive&fromcoord=%f,%f&from=我的位置&referer=jikexiu",latitude, longitude];
urlString = [NSString stringWithFormat:@"%@&tocoord=%f,%f&to=%@",urlString, latitude, longitude, self.detailsDataSource[@"parkName"]];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {

}];

}

//苹果原生地图

- (void)appleMap{

float lat = [self.detailsDataSource[@"latitude"] doubleValue];
float lng = [self.detailsDataSource[@"longitude"] doubleValue];
CLLocationCoordinate2D desCoordinate = CLLocationCoordinate2DMake(lat, lng);
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
currentLocation.name = @"我的位置";
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:desCoordinate addressDictionary:nil]];
toLocation.name = [NSString stringWithFormat:@"%@", self.detailsDataSource[@"parkName"]];
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
               launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];

}

// 高德坐标转百度坐标(vc内)

- (CLLocationCoordinate2D)BD09FromGCJ02:(CLLocationCoordinate2D)coor {

CLLocationDegrees x_pi = 3.14159265358979324 * 3000.0 / 180.0;
CLLocationDegrees x = coor.longitude, y = coor.latitude;
CLLocationDegrees z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
CLLocationDegrees theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
CLLocationDegrees bd_lon = z * cos(theta) + 0.0065;
CLLocationDegrees bd_lat = z * sin(theta) + 0.006;
return CLLocationCoordinate2DMake(bd_lat, bd_lon);

}

//百度地图坐标经纬度与苹果地图相互转换:

// 百度转高德(扩展)

+ (CLLocationCoordinate2D)GCJ02FromBD09:(CLLocationCoordinate2D)coor {

CLLocationDegrees x_pi = 3.14159265358979324 * 3000.0 / 180.0;
CLLocationDegrees x = coor.longitude - 0.0065, y = coor.latitude - 0.006;
CLLocationDegrees z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
CLLocationDegrees theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
CLLocationDegrees gg_lon = z * cos(theta);
CLLocationDegrees gg_lat = z * sin(theta);
return CLLocationCoordinate2DMake(gg_lat, gg_lon);

}

// 高德转百度

+ (CLLocationCoordinate2D)BD09FromGCJ02:(CLLocationCoordinate2D)coor {

CLLocationDegrees x_pi = 3.14159265358979324 * 3000.0 / 180.0;
CLLocationDegrees x = coor.longitude, y = coor.latitude;
CLLocationDegrees z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
CLLocationDegrees theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
CLLocationDegrees bd_lon = z * cos(theta) + 0.0065;
CLLocationDegrees bd_lat = z * sin(theta) + 0.006;
return CLLocationCoordinate2DMake(bd_lat, bd_lon);

}

推荐阅读更多精彩内容