ios項目中調用百度、高德、本機地圖導航(二)

話不多說直接上代碼:

本機地圖

第一步 準備工作

導入<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]}];
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章