話不多說直接上代碼:
本機地圖
第一步 準備工作
第二步 代碼部分
#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]}];
}