MapKit自定义大头阵(iOS9下没有成功)

本文介绍如何在iOS应用中使用自定义的大头针模型为地图添加标注,并展示了如何通过代码实现地图上的位置标注及其外观定制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
//  Annotation.h
//  自定义大头针模型

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface Annotation : NSObject<MKAnnotation>
@property(nonatomic,assign)CLLocationCoordinate2D coordinate;
@property(nonatomic,copy)NSString *title;
@property(nonatomic,copy)NSString *subtitle;
/**
 *  图标
 */
@property(nonatomic,copy)NSString *icon;
//
//  ViewController.m

//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "Annotation.h"

@interface ViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
/**
 *  位置管理者对象
 */
@property(nonatomic,strong) CLLocationManager *locMgr;

- (IBAction)addAnnotations:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end

@implementation ViewController

/**
 *  延迟创建定位管理者对象
 */
- (CLLocationManager *)locMgr
{
    //如果定位服务不可用,加一个判断
    if(![CLLocationManager locationServicesEnabled]) return nil;
    
    if (_locMgr == nil) {
        //创建定位管理者对象
        self.locMgr =[[CLLocationManager alloc]init];
        //设置代理
        self.locMgr.delegate = self;
    }
    return _locMgr;
}

- (IBAction)addAnnotations:(UIButton *)sender {
    //添加大头针
    Annotation *anno1 = [[Annotation alloc] init];
    anno1.title = @"大饭店";
    anno1.subtitle = @"全场9折,会员8折";
    anno1.icon = @"category_1";
    anno1.coordinate = CLLocationCoordinate2DMake(39.9, 115);
    [self.mapView addAnnotation:anno1];
    
    Annotation *anno2 = [[Annotation alloc] init];
    anno2.title = @"xxx影院";
    anno2.subtitle = @"最新大片:xxxxxxx";
    anno2.icon = @"category_5";
    anno2.coordinate = CLLocationCoordinate2DMake(39, 115);
    [self.mapView addAnnotation:anno2];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //用户授权
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
        [self.locMgr requestWhenInUseAuthorization];  //调用了这句,就会弹出允许框了.
    }
    
    //1.设置追踪用户的位置(这个设置了就回显示蓝色发光的圆点)
//    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    
    //2.设置代理
    self.mapView.delegate = self;
    

}


#pragma mark - MKMapViewDelegate
/**
 *  返回大头针的View
 */
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    //0.如果不是自定义的大头针模型就排除(系统默认的大头针还是使用系统的view样式)
    if (![annotation isKindOfClass:[Annotation class]]) {
        return nil;
    }
    
    //1.创建MKAnnotationView(从缓存池取)
    static NSString *ID = @"tg";
    MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];
    if (annotation == nil) {
        annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
        //大头针上显示描述(标题和子标题)
        annoView.canShowCallout = YES;
        annoView.opaque = NO;
        //设置大头针描述的偏移量
        annoView.calloutOffset = CGPointMake(0, -10);
        //设置大头针描述右边控件
        annoView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeContactAdd];
        //设置大头针描述左边控件
        annoView.leftCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        
//        UIImageView *imageView = [[UIImageView alloc] init];
//        imageView.image = [UIImage imageNamed:@"category_1"];
//        annoView.detailCalloutAccessoryView = imageView;
    }
    
    //2.传递模型
    annoView.annotation = annotation;
    
    //设置图片
    Annotation *tgAnnotation = annotation;
    annoView.image = [UIImage imageNamed:tgAnnotation.icon];

    
    //3.返回
    return annoView;
}


/**
 *  当用户位置更新就会调用
 */
//- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
//{
//    //设置地图的显示范围
//    MKCoordinateSpan span = MKCoordinateSpanMake(0.5, 0.5);
//    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
//    [mapView setRegion:region];
//}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值