iOS 自定义大头针

一、自定义大头针

需求

  • 需求:鼠标拖拽是在地图的哪个位置, 就在对应的位置加一个大头针,并反地理编码大头针所在城市等信息
iOS 自定义大头针_第1张图片
效果图.png
  • 0.导入MapKit框架

  • 1.自定义大头针模型,遵守MKAnnotation协议

#import "ViewController.h"
#import 
#import "JPAnnotation.h"
#import 
@interface ViewController ()

@property (weak, nonatomic) IBOutlet MKMapView *mapView;

/** 位置管理者 */
@property (nonatomic, strong) CLLocationManager *locationM;

/** 地理编码 */
@property (nonatomic, strong) CLGeocoder *geoC;
@end

@implementation ViewController

-(CLLocationManager *)locationM
{
    if (!_locationM) {
        _locationM = [[CLLocationManager alloc] init];
        
        [_locationM requestAlwaysAuthorization];
    }
    return _locationM;
}

#pragma mark -懒加载
-(CLGeocoder *)geoC
{
    if (!_geoC) {
        _geoC = [[CLGeocoder alloc] init];
    }
    return _geoC;
}

/**
 *  在地图上操作大头针, 就等于操作大头针数据模型
 *  添加大头针: 添加大头针数据模型
 *  删除大头针: 删除大头针数据模型
 */

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 要求, 鼠标点击地图的哪个位置, 就在对应的位置加一个大头针
    
    // 1. 获取当前用户在屏幕上点击的点坐标
//    UITouch *touch = [touches anyObject];
//    CGPoint center = [touch locationInView:self.mapView];
//    
//    // 2. 把点坐标 转换成 在地图对应的地理坐标
//    CLLocationCoordinate2D centerCoordinate = [self.mapView convertPoint:center toCoordinateFromView:self.mapView];
//    
//    // 3. 设置弹框标题和子标题
//    NSString *title = @"标题";
//    NSString *subTitle = @"子标题";
//    
//    // 4. 调用自定义方法, 添加大头针数据模型
//    JPAnnotation *annotation = [self addAnnotationWithCoordinate:centerCoordinate title:title andSubtitle:subTitle];
//
//    // 反地理编码
//    CLLocation *location = [[CLLocation alloc] initWithLatitude:centerCoordinate.latitude longitude:centerCoordinate.longitude];
//    [self.geoC reverseGeocodeLocation:location completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {
//        // 地标对象
//        CLPlacemark *pl = [placemarks firstObject];
//        
//        // 取详细地址
//        NSString *name = pl.name;
//        
//        // 获取城市
//        NSString *city = pl.locality;
//        
//        annotation.title = city;
//        annotation.subtitle = name;
//  
//    }];
    [self locationM];
    
    // 在地图上根据当前用户位置, 添加了一个大头针视图
    self.mapView.showsUserLocation = YES;
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 获取地图上所有的大头针数据模型
//    NSArray *annotations = self.mapView.annotations;
    
    // 移除大头针
//    [self.mapView removeAnnotations:annotations];
    
       [self addAnnotation];
}

/**
 *  自定义方法,添加大头针
 *
 *  @param center   大头针扎在哪
 *  @param title    弹框标题
 *  @param subTitle 弹框子标题
 */
- (JPAnnotation *)addAnnotationWithCoordinate:(CLLocationCoordinate2D)center title:(NSString *)title andSubtitle:(NSString *)subTitle
{
 
    JPAnnotation *annotation = [[JPAnnotation alloc] init];
    annotation.coordinate = center;
    
    annotation.title = title;
    annotation.subtitle = subTitle;
    
    [self.mapView addAnnotation:annotation];
    return annotation;
}

- (void)addAnnotation
{
    // 添加大头针 == 添加大头针数据模型
    
    // 1. 创建大头针数据模型
    JPAnnotation *annotation = [[JPAnnotation alloc] init];
    // 1.1 设置经纬度
    annotation.coordinate = self.mapView.centerCoordinate;
    // 1.2 弹框标题
    annotation.title = @"标题";
    // 1.3 弹框子标题
    annotation.subtitle = @"子标题";
    
    // 添加大头针数据模型
    [self.mapView addAnnotation:annotation];
}


/**
 *  系统的大头针视图返回如果为nil , 实现方案
 *
 *  @param mapView    地图
 *  @param annotation 大头针数据模型
 *
 *  @return 大头针视图
 */
-(MKAnnotationView *)xitongmapView:(MKMapView *)mapView viewForAnnotation:(id)annotation
{
    
    // -------模拟当次方返回nil , 系统的实现方案
    
    // 系统默认的大头这视图 对应的类 MKPinAnnotationView
    // 大头针标识
    static NSString *pinID = @"pinID";
    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinID];
    // 如果从缓存池取出的大头针视图为空, 就创建
    if (pinView == nil) {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID];
    }
    
    // 重新设置数据模型 (一定要记得!!!), 为了防止循环利用时, 数据混乱
    pinView.annotation = annotation;
    
    // 控制大头针是否弹框
    pinView.canShowCallout = YES;
    
    // 设置大头针颜色
    //    pinView.pinColor = MKPinAnnotationColorGreen;
    pinView.pinTintColor = [UIColor blackColor]; // iOS9.0
    
    // 设置大头针下落动画
    pinView.animatesDrop = YES;
    
    // 设置大头针显示图片(如果是系统的大头针视图, 那么就无法进行自定义)
    //    pinView.image = [UIImage imageNamed:@"category_1.png"];
    
    return pinView;
}

#pragma mark -MKMapViewDelegate

/**
 *  地图定位到之后调用
 *
 *  @param mapView      地图
 *  @param userLocation 大头针数据模型
 */
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    
}

/**
 *  当我们添加大头针数据模型时, 就会调用这个方法, 查找对应的大头针视图
 *
 *  @param mapView    地图
 *  @param annotation 大头针数据模型
 *
 *  @return 大头针视图
 *  注意: 如果这个方法, 没有实现, 或者, 这个方法返回nil, 那么系统就会调用系统默认的大头针视图
 */
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation
{
    // 如果是系统的大头针数据模型, 那么使用系统默认的大头针视图,
    if([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;
    }
    
    // 如果想要自定义大头针视图, 必须使用MKAnnotationView 或者 继承 MKAnnotationView 的子类
    
    // 设置循环利用标识
    static NSString *pinID = @"pinID";
    
    // 从缓存池取出大头针数据视图
    MKAnnotationView *customView = [mapView dequeueReusableAnnotationViewWithIdentifier:pinID];
    
    // 如果取出的为nil , 那么就手动创建大头针视图
    if (customView == nil) {
        customView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID];
    }
    
    // 1. 设置大头针图片
    customView.image = [UIImage imageNamed:@"category_5.png"];

    // 2. 设置弹框
    customView.canShowCallout = YES;
    
    // 2.1 设置大头针偏移量
//    customView.centerOffset = CGPointMake(100, -100);
    // 2.2 设置弹框的偏移量
//    customView.calloutOffset = CGPointMake(100, 100);
    
    
    // 3. 自定义弹框
    // 3.1 设置弹框左侧的视图
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    imageView.image = [UIImage imageNamed:@"htl.jpeg"];
    customView.leftCalloutAccessoryView = imageView;
    
    // 3.2 设置弹框右侧视图
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    imageView2.image = [UIImage imageNamed:@"eason.jpg"];
    customView.rightCalloutAccessoryView = imageView2;
    
    // 3.3 设置弹框的详情视图(一定要注意,对应的版本)
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
        customView.detailCalloutAccessoryView = [UISwitch new];
    }
    
    // 设置大头针视图可以被拖拽
    customView.draggable = YES;

    return customView;
    return nil;
}

/**
 *  选中大头针视图时调用这个方法
 *
 *  @param mapView 地图
 *  @param view    大头针视图
 */
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    NSLog(@"选中---%@", view.annotation.title);
}

/**
 *  取消选中某个大头针视图
 *
 *  @param mapView 地图
 *  @param view    大头针视图
 */
-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
     NSLog(@"取消选中--%@", view.annotation.title);
}


/**
 *  改变大头针视图拖拽状态时调用
 *
 *  @param mapView  地图
 *  @param view     大头针视图
 *  @param newState 新状态
 *  @param oldState 老状态
 */
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState
{
    NSLog(@"%zd---%zd", oldState, newState);
}

@end
  • 3.注意:
    • 大头针title,subTitle占位问题
      • 设置大头针数据模型的时候,必须先设置title,subTitle,起到占位作用,否则,当你最后再设置大头针的标题与子标题的时候,你会发现大头针弹框显示不出来。就如设置Cell的占位图片,如果没有占位图片的话当你用SDImage下载图片后设置图片任然显示不出来一样。所以:要考虑占位.

你可能感兴趣的:(iOS 自定义大头针)