码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2016-05-31 20:34:44      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

首先要引入#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;
    }
}

 技术分享

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

标签:

原文地址:http://www.cnblogs.com/zhanglida/p/5547174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!