iOS学习_地图_添加大头针

首先要引入#import <MapKit/MapKit.h>框架

创建一个继承与NSObject的类;用于声明属性

接下来在ViewController中实现相关的操作

引入头文件

#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "MyAnnotation.h"

定义相应的属性

//定位管理器
@property(nonatomic,strong)CLLocationManager *locationManager;
//显示地图的视图
@property(nonatomic,strong)MKMapView *mapView;

代码操作

#pragma mark - 创建视图
- (void)createMapView{
    //创建地图,并添加到当前视图上
    self.mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:self.mapView];
    //设置代理
    _mapView.delegate = self;
    //定位
    self.locationManager = [[CLLocationManager alloc] init];
    //进行隐私判断
    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"当前设备定位不可用");
    }
    //判断授权状态
    if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
        //在授权请求之前需要在inforplist中设置允许定位的内容:NSLocationWhenInUseUsageDescription(NSLocationAlwaysUsageDescription这是另一种方式)

        [self.locationManager requestWhenInUseAuthorization];
    }
    //设置地图的定位追踪
    _mapView.userTrackingMode = MKUserTrackingModeFollow;
    //设置地图的显示类型
    _mapView.mapType = MKMapTypeStandard;
    
    //添加大头针
    [self addAnnotation];

}

添加大头针

#pragma mark - 添加大头针
- (void)addAnnotation{
    //设置位置
    CLLocationCoordinate2D location1 = CLLocationCoordinate2DMake(40, 116);
    MyAnnotation *annotation = [[MyAnnotation alloc] init];
    annotation.coordinate = location1;
    annotation.title = @"北京";
    annotation.subtitle = @"ANN";
#warning 添加图片
    annotation.image = [UIImage imageNamed:@"1"];
    [_mapView addAnnotation:annotation];
    
    CLLocationCoordinate2D location2 = CLLocationCoordinate2DMake(39, 121);
    MyAnnotation *annotation1 = [[MyAnnotation alloc] init];

    annotation1.coordinate = location2;
    annotation1.title = @"大连";
    annotation1.subtitle = @"abxa";
    annotation1.image = [UIImage imageNamed:@"1"];
    [_mapView addAnnotation:annotation1];
    
    
    CLLocationCoordinate2D location3 = CLLocationCoordinate2DMake(31, 121);
    MyAnnotation *annotation2 = [[MyAnnotation alloc] init];

    annotation2.coordinate = location3;
    annotation2.title = @"上海";
    annotation2.subtitle = @"gda";
    annotation2.image = [UIImage imageNamed:@"1"];
    [_mapView addAnnotation:annotation2];

}

实现代理方法

#pragma mark - 实现自定义大头针视图的代理方法

//显示大头针的时候才会调用的
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    //判断是否是当前自定义的大头针类
    if ([annotation isKindOfClass:[MyAnnotation class]]) {
        //定义一个重用标识
        static NSString *identifier = @"AnnotationOne";
        MKAnnotationView *annotationView = [_mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
        if (!annotationView) {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            //允许用户交互
            annotationView.canShowCallout = YES;
            //设置详情和大头针的头偏移量
            annotationView.calloutOffset = CGPointMake(0, 2);
            //设置详情的左视图
            annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
            
        }
        //修改大头针视图
        annotationView.annotation = annotation;
        annotationView.image = ((MyAnnotation *)annotation).image;
        return annotationView;
    } else {
        return nil;
    }
}

 

转载于:https://www.cnblogs.com/zhanglida/p/5547174.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值