//先去类库中导入CoreLocation 和mapKit库文件

 

#import "ViewController.h"
#import <MapKit/MapKit.h>//导入头文件
#import <CoreLocation/CoreLocation.h>//导入头文件
@interface ViewController ()<MKMapViewDelegate,CLLocationManagerDelegate>//实现代理
@property (weak, nonatomic) IBOutlet MKMapView *mapView;//storyboard中连线一个mapView将代理连线到self;
@property (nonatomic,strong)CLLocationManager *manager;

@end

@implementation ViewController




//可以在下方协议中返回自定义的大头针 ,大头针使用复用机制,类似于Cell一样
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    //如果是地图当前位置的点不应该显示大头针。
    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        return nil;//nil表示使用默认的蓝色点,表示用户位置那个点。蓝色。
    }
    
    
    
    
    static NSString * identify = @"annotation";
    MKPinAnnotationView * annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identify];
    if (!annotationView) {
        annotationView = [[MKPinAnnotationView alloc]initWithAnnotation:nil reuseIdentifier:identify];
    //定义点击之后弹出的泡泡图左侧显示咖啡图片。
        annotationView.leftCalloutAccessoryView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_classify_cafe"]];
        //右侧则显示开关控件
        annotationView.rightCalloutAccessoryView = [UISwitch new];
        //允许点击后弹出泡泡视频。
        annotationView.canShowCallout = YES;
        //改变大头针的颜色
        annotationView.pinColor = MKPinAnnotationColorGreen;
        //大头针动画
        annotationView.animatesDrop =YES;
    }
    
    annotationView.annotation = annotation;
    return annotationView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (!_manager) {
        _manager = [CLLocationManager new];
        if ([UIDevice currentDevice].systemVersion.floatValue>=8.0) {
            [_manager requestAlwaysAuthorization];
        }
    }
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)addAnnotation:(id)sender {
    //添加之前要去掉以前加的大头针,不然越加越多。
    [self.mapView removeAnnotations:self.mapView.annotations];
    //添加一个默认风格的大头钉
    MKPointAnnotation *point1 = [MKPointAnnotation new];
    //设置大头钉的位置(经纬度),大头钉的题目,子题目。
    point1.coordinate =CLLocationCoordinate2DMake(25, 120);
    point1.title = @"这里是题目";
    point1.subtitle = @"这里是子题目";
    //[self.mapView addAnnotation:point1];//左边方法是一个个加大头钉。
    
    
    //再加一个大头钉
    MKPointAnnotation * point2 = [MKPointAnnotation new];
    point2.coordinate = CLLocationCoordinate2DMake(25, 110);
    point2.title = @"第二个大头钉题目";
    point2.subtitle = @"第二个大头钉子题目";
    
    [self.mapView addAnnotations:@[point2,point1]];//左边方法按数组加一堆大头钉。
    
    
    //把地图绽放到大头钉所在处。提升用户体验
    [self.mapView setRegion:MKCoordinateRegionMake(point1.coordinate, MKCoordinateSpanMake(0.01, 0.01)) animated:YES];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

**********下面例子与上面例子极其相似。上面对大头针的泡泡图进行了自定义,下面则是对大头针本身图片进行了修改。用的类不同。************

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic,strong) CLLocationManager * manager;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _manager = [CLLocationManager new];
    if ([UIDevice currentDevice].systemVersion.floatValue >=8.0) {
        [_manager requestAlwaysAuthorization];
    }
    
}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    if ([annotation isKindOfClass:[MKUserLocation class]]) {//把用户自身的位置取消变更。
        return nil;
    }
    static NSString * identify = @"annotionView";
    MKAnnotationView *  view = [mapView dequeueReusableAnnotationViewWithIdentifier:identify];
    if (!view) {
        view = [[MKAnnotationView alloc]initWithAnnotation:nil reuseIdentifier:identify];
        view.canShowCallout = YES;
    }
    //只有MKAnnotationView类型,才能够设置特殊的大头针图片
    view.image = [UIImage imageNamed:@"icon_mark1"];
    return view;
}

- (IBAction)addannotation:(id)sender {
    MKPointAnnotation * point = [MKPointAnnotation new];
    point.title = @"自定义位置";
    point.subtitle = @"子标题";
    point.coordinate = CLLocationCoordinate2DMake(25, 115);
    [self.mapView addAnnotation:point];
    
    
    [self.mapView setRegion:MKCoordinateRegionMake(point.coordinate, MKCoordinateSpanMake(0.01, 0.01)) animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
*********************以下是地图导航功能。*******************************

 #import "ViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (weak, nonatomic) IBOutlet UITextField *beginTF;
@property (weak, nonatomic) IBOutlet UITextField *endTF;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)beginRouting:(id)sender {
    //需要两个地点的placeMark
    [[CLGeocoder new] geocodeAddressString:self.beginTF.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark * beginMark = placemarks.firstObject;
        [[CLGeocoder new] geocodeAddressString:self.endTF.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            CLPlacemark * endMark = placemarks.firstObject;
            [self naviWithStart:beginMark endMark:endMark];
        }];
        
    }];
    
    
}
- (void)naviWithStart:(CLPlacemark *)startMark endMark:(CLPlacemark *)endMark{
    //以下内容固定,可以永久保存。复制使用即可。
    MKPlacemark * beginMark = [[MKPlacemark alloc]initWithPlacemark:startMark];
    MKMapItem * beginItem = [[MKMapItem alloc]initWithPlacemark:beginMark];
    
    MKPlacemark * Markend = [[MKPlacemark alloc]initWithPlacemark:endMark];
    MKMapItem * endItem = [[MKMapItem alloc]initWithPlacemark:Markend];
    //打开系统自带的地图应用,并进入到导航模式
    //第二个参数是一个字典,里面做一些设置
    //我们设置为开车,设置地图模式为混合模式
    [MKMapItem openMapsWithItems:@[beginItem,endItem] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,MKLaunchOptionsMapTypeKey:@(MKMapTypeHybrid)}];

    


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end