iOS调用外部地图导航

App Store 上架软件《相册地图》的源码,自己写的一个类,可以调用系统地图、百度地图、高德地图。

为保持代码简洁,没有添加是否安装百度地图、高德地图的判断词句,如有需要可自行添加。

原创作品,请勿转载。如有建议,欢迎交流 :[email protected]

#import 

#define GCMapsCompanyName @"CTP Technology Co.,Ltd"
#define GCMapsAppName @"AlbumMaps"

#define BaiduMapDirectionsModeWalking @"walking"
#define BaiduMapDirectionsModeTransit @"transit"
#define BaiduMapDirectionsModeDriving @"driving"

/**
 *  高德地图交通方式
 */
typedef NS_ENUM(NSUInteger, AMapTMode) {
    /**
     *  驾车
     */
    AMapTModeDriving = 0,
    /**
     *  公交
     */
    AMapTModeTransit,
    /**
     *  步行
     */
    AMapTModeWalking,
};

/**
 *  高德地图驾车选项
 */
typedef NS_ENUM(NSUInteger, AMapDrivingMOption) {
    /**
     *  速度最快
     */
    AMapDrivingMOption0 = 0,
    /**
     *  费用最少
     */
    AMapDrivingMOption1,
    /**
     *  距离最短
     */
    AMapDrivingMOption2,
    /**
     *  不走高速
     */
    AMapDrivingMOption3,
    /**
     *  躲避拥堵
     */
    AMapDrivingMOption4,
    /**
     *  不走高速且避免收费
     */
    AMapDrivingMOption5,
    /**
     *  不走高速且躲避拥堵
     */
    AMapDrivingMOption6,
    /**
     *  躲避收费和拥堵
     */
    AMapDrivingMOption7,
    /**
     *  不走高速躲避收费和拥堵
     */
    AMapDrivingMOption8
};

/**
 *  高德地图公交选项
 */
typedef NS_ENUM(NSUInteger, AMapTransitMOption) {
    /**
     *  最快捷
     */
    AMapTransitMOption0 = 0,
    /**
     *  最少换乘
     */
    AMapTransitMOption2 = 2,
    /**
     *  最少步行
     */
    AMapTransitMOption3 = 3,
    /**
     *  不乘地铁
     */
    AMapTransitMOption5 = 5,
    /**
     *  只坐地铁
     */
    AMapTransitMOption7 = 7,
    /**
     *  时间短
     */
    AMapTransitMOption8 = 8
};

/**
 *  iOS调用外部地图导航
 */
@interface GCMaps : NSObject

/**
 *  调用百度地图导航
 *
 *  @param origin        起点座标(需要使用百度地图座标)
 *  @param destination   终点座标(需要使用百度地图座标)
 *  @param directionsMode 交通方式
 */
+ (void)baidumapDirectionFromOrigin:(CLLocationCoordinate2D)origin toDestination:(CLLocationCoordinate2D)destination directionsMode:(NSString *)directionsMode;

/**
 *  调用百度地图导航
 *
 *  @param origin        起点座标(需要使用百度地图座标)
 *  @param destination   终点座标(需要使用百度地图座标)
 *  @param directionsMode 交通方式
 *  @param companyName   可留空,调用百度地图的公司名称
 *  @param appName       可留空,调用百度地图的应用名称
 */
+ (void)baidumapDirectionFromOrigin:(CLLocationCoordinate2D)origin toDestination:(CLLocationCoordinate2D)destination directionsMode:(NSString *)directionsMode companyName:(NSString *)companyName appName:(NSString *)appName;

/**
 *  调用高德地图导航
 *
 *  @param source      起点座标(需要使用GCJ02基准座标)
 *  @param destination 终点座标(需要使用GCJ02基准座标)
 *  @param t           交通方式
 *  @param m           导航选项(依据交通方式不同选项不同)
 */
+ (void)iosamapPathFromSource:(CLLocationCoordinate2D)source toDestination:(CLLocationCoordinate2D)destination tMode:(enum AMapTransitMOption)t mOption:(enum AMapDrivingMOption)m;

/**
 *  调用高德地图导航
 *
 *  @param source      起点座标(需要使用GCJ02基准座标)
 *  @param destination 终点座标(需要使用GCJ02基准座标)
 *  @param t           交通方式
 *  @param m           导航选项(依据交通方式不同选项不同)
 *  @param appName     可留空,调用高德地图的应用名称
 */
+ (void)iosamapPathFromSource:(CLLocationCoordinate2D)source toDestination:(CLLocationCoordinate2D)destination tMode:(enum AMapTransitMOption)t mOption:(enum AMapDrivingMOption)m appName:(NSString *)appName;

/**
 *  调用iOS系统地图导航
 *
 *  @param source         起点座标(需要使用GCJ02基准座标
 *  @param destination    终点座标(需要使用GCJ02基准座标)
 *  @param directionsMode 交通方式(三选一 MKLaunchOptionsDirectionsModeDriving MKLaunchOptionsDirectionsModeTransit MKLaunchOptionsDirectionsModeWalking)
 */
+ (void)mkmapFromSource:(CLLocationCoordinate2D)source toDestination:(CLLocationCoordinate2D)destination directionsMode:(NSString *)directionsMode;
@end

#import "GCMaps.h"
@import MapKit;

@implementation GCMaps

+ (void)baidumapDirectionFromOrigin:(CLLocationCoordinate2D)origin toDestination:(CLLocationCoordinate2D)destination directionsMode:(NSString *)directionsMode{
    [GCMaps baidumapDirectionFromOrigin:origin toDestination:destination directionsMode:directionsMode companyName:GCMapsCompanyName appName:GCMapsAppName];
}

+ (void)baidumapDirectionFromOrigin:(CLLocationCoordinate2D)origin toDestination:(CLLocationCoordinate2D)destination directionsMode:(NSString *)directionsMode companyName:(NSString *)companyName appName:(NSString *)appName{
    //URI格式示例
    //@"baidumap://map/direction?origin=34.26,108.95&destination=40.00,116.36&mode=driving&src=yourCompanyName|yourAppName";
    NSString *requestString = [NSString stringWithFormat:@"baidumap://map/direction?origin=%.10f,%.10f&destination=%.10f,%.10f&mode=%@&src=%@|%@",origin.latitude,origin.longitude,destination.latitude,destination.longitude,directionsMode,companyName,appName];
    requestString = [requestString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:requestString]];
}

+ (void)iosamapPathFromSource:(CLLocationCoordinate2D)source toDestination:(CLLocationCoordinate2D)destination tMode:(enum AMapTransitMOption)t mOption:(enum AMapDrivingMOption)m{
    [GCMaps iosamapPathFromSource:source toDestination:destination tMode:t mOption:m appName:GCMapsAppName];
}

+ (void)iosamapPathFromSource:(CLLocationCoordinate2D)source toDestination:(CLLocationCoordinate2D)destination tMode:(enum AMapTransitMOption)t mOption:(enum AMapDrivingMOption)m appName:(NSString *)appName{
    //URI格式示例
    //iosamap://path?sourceApplication=applicationName&sid=BGVIS1&slat=39.92848272&slon=116.39560823&sname=A&did=BGVIS2&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&m=0&t=0
    NSString *requestString = [NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&sid=BGVIS1&slat=%.10f&slon=%.10f&sname=&did=BGVIS2&dlat=%.10f&dlon=%.10f&dname=&dev=0&m=%ld&t=%ld",appName,source.latitude,source.longitude,destination.latitude,destination.longitude,(long)m,(long)t];
    requestString = [requestString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:requestString]];
}

+ (void)mkmapFromSource:(CLLocationCoordinate2D)source toDestination:(CLLocationCoordinate2D)destination directionsMode:(NSString *)directionsMode{
    MKMapItem *sourceItem = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:source addressDictionary:nil]];
    MKMapItem *destinationItem = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:destination addressDictionary:nil]];
    NSDictionary *options = @{ MKLaunchOptionsDirectionsModeKey:directionsMode,
                               MKLaunchOptionsMapTypeKey: [NSNumber numberWithInteger:MKMapTypeStandard],
                               MKLaunchOptionsShowsTrafficKey:@YES };
    [MKMapItem openMapsWithItems:@[sourceItem,destinationItem] launchOptions:options];
}

@end



你可能感兴趣的:(iOS源码)