自定义大头针

  • (void)viewDidLoad {
    [super viewDidLoad];
    // 注意:在iOS8中, 如果想要追踪用户的位置, 必须自己主动请求隐私权限
    if ([[UIDevice currentDevice].systemVersion doubleValue] >= 8.0) {
    // 主动请求权限
    self.mgr = [[CLLocationManager alloc] init];
    [self.mgr requestAlwaysAuthorization];
    }
    // 设置不允许地图旋转
    self.customMapView.rotateEnabled = NO;
    // 成为mapVIew的代理
    self.customMapView.delegate = self;
    // 如果想利用MapKit获取用户的位置, 可以追踪
    self.customMapView.userTrackingMode = MKUserTrackingModeFollow;
    }

//MKMapViewDelegate
/**
* 每次更新到用户的位置就会调用(调用不频繁, 只有位置改变才会调用)
* @param mapView 促发事件的控件
* @param userLocation 大头针模型
*/
- (void)mapView:(MKMapView )mapView didUpdateUserLocation:(MKUserLocation )userLocation{
// 利用反地理编码获取位置之后设置标题
[self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks firstObject];
NSLog(@”获取地理位置成功 name = %@ locality = %@”, placemark.name, placemark.locality);
userLocation.title = placemark.name;
userLocation.subtitle = placemark.locality;
}];
/*
// 设置地图显示的区域
// 获取用户的位置
CLLocationCoordinate2D center = userLocation.location.coordinate;
// 指定经纬度的跨度
MKCoordinateSpan span = MKCoordinateSpanMake(5 ,5);
// 将用户当前的位置作为显示区域的中心点, 并且指定需要显示的跨度范围
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
// 设置显示区域
[self.customMapView setRegion:region animated:YES];
*/
[self.customMapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
}

// MKMapViewDelegate
/**
* 每次添加大头针就会调用(地图上有几个大头针就调用几次)
*
* @param mapView 地图
* @param annotation 大头针模型
*
* @return 大头针的view
*/
- (MKAnnotationView )mapView:(MKMapView )mapView viewForAnnotation:(id)annotation{
// NSLog(@”%s”, func);
NSLog(@”annotation === %@”, annotation);
// 对用户当前的位置的大头针特殊处理
if ([annotation isKindOfClass:[HMAnnotation class]] == NO) {
return nil;
}
// 注意: 如果返回nil, 系统会按照自己默认的方式显示
// return nil;

//**********************************
/*
static NSString *identifier = @”anno”;
// 1.从缓存池中取
// 注意: 默认情况下MKAnnotationView是无法显示的, 如果想自定义大头针可以使用MKAnnotationView的子类MKPinAnnotationView
// 注意: 如果是自定义的大头针, 默认情况点击大头针之后是不会显示标题的, 需要我们自己手动设置显示
// MKPinAnnotationView annoView = (MKPinAnnotationView )[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
// 2.如果缓存池中没有, 创建一个新的
if (annoView == nil) {
// annoView = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];
annoView = [[MKAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:identifier];
// 设置大头针的颜色
// annoView.pinColor = MKPinAnnotationColorPurple;
// 设置大头针从天而降
// annoView.animatesDrop = YES;
// 设置大头针标题是否显示
annoView.canShowCallout = YES;
// 设置大头针标题显示的偏移位
// annoView.calloutOffset = CGPointMake(-50, 0);
// 设置大头针左边的辅助视图
annoView.leftCalloutAccessoryView = [[UISwitch alloc] init];
// 设置大头针右边的辅助视图
annoView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
}
// 设置大头针的图片
// 注意: 如果你是使用的MKPinAnnotationView创建的自定义大头针, 那么设置图片无效, 因为系统内部会做一些操作, 覆盖掉我们自己的设置
// annoView.image = [UIImage imageNamed:@”category_4”];
HMAnnotation anno = (HMAnnotation )annotation;
annoView.image = [UIImage imageNamed:anno.icon];
// 3.给大头针View设置数据
annoView.annotation = annotation;
// 4.返回大头针View
return annoView;
*/
//**********************************
//如果自定义mapView封装 可以使用一下代码
// 1.创建大头针
HMAnnotationView *annoView = [HMAnnotationView annotationViewWithMap:mapView];
// 2.设置模型
annoView.annotation = annotation;
// 3.返回大头针
return annoView;
}

//pragma mark - 懒加载
- (CLGeocoder *)geocoder{
if (!_geocoder) {
_geocoder = [[CLGeocoder alloc] init];
}
return _geocoder;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值