地图(有界面 自定义大头针)

创建一个继承与NSObject的类
.h中的代码如下: 引入框架 遵循协议 重写协议中的三个属性

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MyAnnotation : NSObject<MKAnnotation>
//重写协议中的三个属性 coordinate title subtitle
//坐标
@property (nonatomic) CLLocationCoordinate2D coordinate;
//标题
@property (nonatomic, copy) NSString *title;
//子标题
@property (nonatomic, copy) NSString *subtitle;

//自定义大头针的图片
@property (nonatomic, strong) UIImage *image;



@end

接着回到viewController.m 具体操作如下:

#import "ViewController.h"
//引入框架
#import <CoreAudioKit/CoreAudioKit.h>
#import "MyAnnotation.h"
//地图使用的框架是MapKit
/*
 大头针:
 在iOS开发中经常会标记某个位置,需要使用地图标注,也就是大家俗称的“大头针”。只要一个NSObject类实现MKAnnotation协议就可以作为一个大头针,通常会重写协议中coordinate(标记位置)、title(标题)、subtitle(子标题)三个属性,然后在程序中创建大头针对象并调用addAnnotation:方法添加大头针即可(之所以iOS没有定义一个基类实现这个协议供开发者使用,多数原因应该是MKAnnotation是一个模型对象,对于多数应用模型会稍有不同,例如后面的内容中会给大头针模型对象添加其他属性)。
 */



@interface ViewController ()<CLLocationManagerDelegate, MKMapViewDelegate>
//定位管理器
@property (nonatomic, strong) CLLocationManager *manager;
//显示地图的视图
@property (nonatomic, strong) MKMapView *mapView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createMapView];
}
#pragma mark-  创建视图
- (void)createMapView{
    self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.mapView];
    //设置代理
    self.mapView.delegate = self;
    //定位
    self.manager = [[CLLocationManager alloc] init];
    if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
        [self.manager requestAlwaysAuthorization];
    }

    //设置地图的定位追踪
    _mapView.userTrackingMode = MKUserTrackingModeFollow;
    //设置地图的显示类型
    _mapView.mapType = MKMapTypeStandard;

    //添加大头针
    [self addAnnotation];
}
#pragma mark - 添加大头针
- (void)addAnnotation{
    //设置位置
    CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(29.5, 116);
    CLLocationCoordinate2D location2 = CLLocationCoordinate2DMake(40, 116);
    CLLocationCoordinate2D location3 = CLLocationCoordinate2DMake(20, 110.5);
    MyAnnotation *annotation1 = [[MyAnnotation alloc] init];
    annotation1.coordinate = location1;
    annotation1.title = @"九江";
    annotation1.subtitle = @"呵呵哒";
    annotation1.image = [UIImage imageNamed:@"1.png"];
    [self.mapView addAnnotation:annotation1];

    MyAnnotation *annotation2 = [[MyAnnotation alloc] init];
    annotation2.coordinate = location2;
    annotation2.title = @"北京";
    annotation2.subtitle = @"北京欢迎您";
    [self.mapView addAnnotation:annotation2];

    MyAnnotation *annotation3 = [[MyAnnotation alloc] init];
    annotation3.coordinate = location3;
    annotation3.title = @"呵呵";
    annotation3.subtitle = @"呵呵";
    [self.mapView addAnnotation:annotation3];
}

//显示大头针的时候才会调用
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    //判断是否是当前自定义的大头针类
    if ([annotation isKindOfClass:[MyAnnotation class]]) {
        //先定义一个重用标识
        static NSString *identifier = @"AnnotationOne";
        MKAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if (!annotationView) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            //允许用户交互
            annotationView.canShowCallout = YES;
            //设置详情和大头针的头偏移量
            annotationView.calloutOffset = CGPointMake(0, 1);

            //设置详情的左视图
            annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];
        }
        //修改大头针视图
        annotationView.annotation = annotation;

        annotationView.image = ((MyAnnotation *)annotation).image;

        return annotationView;
    }
    else{
        return nil;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值