iOS打开常用的第三方地图

1、苹果地图
2、百度地图
3、高德地图
4、谷歌地图
5、腾讯地图
首先在设置中添加URL Schemes
在这里插入图片描述

1、苹果地图

// 苹果地图
- (void)appleMapWithEndLocation:(CLLocationCoordinate2D)endLocation {
    
    MKMapItem *currentLoc = [MKMapItem mapItemForCurrentLocation];
    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:endLocation addressDictionary:nil]];
    NSArray *items = @[currentLoc,toLocation];
    NSDictionary *dic = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving,
                          MKLaunchOptionsMapTypeKey : @(MKMapTypeStandard),
                          MKLaunchOptionsShowsTrafficKey : @(YES)};
    [MKMapItem openMapsWithItems:items launchOptions:dic];
}

2、百度地图

- (void)baiduMapWithEndLocation:(CLLocationCoordinate2D)endLocation {
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        NSString *str = [[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02", endLocation.latitude, endLocation.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    } else {
        NSLog(@"您未安装百度地图");
    }
}

3、高德地图

- (void)gaodeMapWithEndLocation:(CLLocationCoordinate2D)endLocation {
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        NSString *str = [[NSString stringWithFormat:@"iosamap://navi?sourceApplication=%@&backScheme=%@&lat=%f&lon=%f&dev=0&style=2", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"], [NSString stringWithFormat:@"%@_gaodeMap", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]], endLocation.latitude, endLocation.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    } else {
        NSLog(@"您未安装高德地图");
    }
}

4、谷歌地图

- (void)googleMapWithEndLocation:(CLLocationCoordinate2D)endLocation {
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
        NSString *str = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"], [NSString stringWithFormat:@"%@_googleMap", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]], endLocation.latitude, endLocation.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    } else {
        NSLog(@"您未安装谷歌地图");
    }
}

5、腾讯地图

- (void)tencentMapWithEndLocation:(CLLocationCoordinate2D)endLocation {
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
        NSString *str = [[NSString stringWithFormat:@"qqmap://map/routeplan?from=我的位置&type=drive&tocoord=%f,%f&to=终点&coord_type=1&policy=0", endLocation.latitude, endLocation.longitude] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    } else {
        NSLog(@"您未安装腾讯地图");
    }
}

猜你喜欢

转载自blog.csdn.net/qq_16804091/article/details/85320778