ios项目中调用百度、高德、本机地图导航(二)

来源:互联网 发布:谷歌程序员平均工资 编辑:程序博客网 时间:2024/05/15 05:44

话不多说直接上代码:

本机地图

第一步 准备工作

导入<MapKit/MapKit.h>    <CoreLocation/CoreLocation.h>  两个库


第二步 代码部分

在需要调用的.h文件中加入
#import <MapKit/MapKit.h>#import <CoreLocation/CoreLocation.h>


然后调用代码
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提醒" message:@"启动本机地图" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {    //百度地图    [self startiosMap];    }];[alert addAction:cancel];[alert addAction:ok];[self presentViewController:alert animated:YES completion:nil];


//本机地图-(void) startiosMap{    CLLocationCoordinate2D myLocation = CLLocationCoordinate2DMake(29.569389,106.557978);    CLLocationCoordinate2D storeLoacation = CLLocationCoordinate2DMake(29.615133,106.605053);        //高德的坐标转换接口    myLocation = AMapCoordinateConvert(myLocation, AMapCoordinateTypeGPS);    storeLoacation =AMapCoordinateConvert(storeLoacation, AMapCoordinateTypeGPS);        MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];    MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:storeLoacation addressDictionary:nil]];    toLocation.name = @“美心洋人街”;        [MKMapItem openMapsWithItems:@[currentLocation, toLocation]                   launchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsShowsTrafficKey: [NSNumber numberWithBool:YES]}];}


阅读全文
1 0