码迷,mamicode.com
首页 > 其他好文 > 详细

添加大头针

时间:2015-03-17 15:33:18      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

大头针的基本操作
添加一个大头针
- (void)addAnnotation:(id <MKAnnotation>)annotation;

添加多个大头针
- (void)addAnnotations:(NSArray *)annotations;

移除一个大头针
- (void)removeAnnotation:(id <MKAnnotation>)annotation;

移除多个大头针
- (void)removeAnnotations:(NSArray *)annotations;

(id <MKAnnotation>)annotation参数是什么东西?
大头针模型对象:用来封装大头针的数据,比如大头针的位置、标题、子标题等数据

自定义大头针,需要实现MKAnnotation协议

@interface MyAnnotation : NSObject <MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;

@end

 

控制器:

//
//  ViewController.m
//  05-添加大头针
//
//  Created by apple on 15/1/30.
//  Copyright (c) 2015年 apple. All rights reserved.
//

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

@interface ViewController () <MKMapViewDelegate>

// 显示地图的View
@property (weak, nonatomic) IBOutlet MKMapView *mapView;

// IOS8
@property (nonatomic,strong) CLLocationManager *mgr;

@end

@implementation ViewController

- (CLLocationManager *)mgr{
    if (!_mgr) {
        _mgr = [[CLLocationManager alloc] init];
        
        [_mgr requestAlwaysAuthorization];
        [_mgr requestWhenInUseAuthorization];
    }
    return _mgr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // iOS 8后需要请求授权,并在info.plist文件中添加
//        总是使用用户位置:NSLocationAlwaysUsageDescription
//        使用应用时定位:NSLocationWhenInUseDescription
    self.mgr;
    
    // 1.设置代理
    self.mapView.delegate = self;
    
    // 2.跟踪用户的位置
    self.mapView.userTrackingMode = MKUserTrackingModeFollow;
    
    // 3.添加两个大头针
    MyAnnotation *anno1 = [[MyAnnotation alloc] init];
    anno1.coordinate = CLLocationCoordinate2DMake(40.06, 116.39);
    anno1.title = @"北京市";
    anno1.subtitle = @"中国北京市昌平区";
    
    MyAnnotation *anno2 = [[MyAnnotation alloc] init];
    anno2.coordinate = CLLocationCoordinate2DMake(30.23, 120.23);
    anno2.title = @"杭州市";
    anno2.subtitle = @"浙江省杭州市萧山区";
    
    [self.mapView addAnnotation:anno1];
    [self.mapView addAnnotation:anno2];
}

/**
 *  定位到用户的位置会调用该方法
 *
 *  @param userLocation 大头针模型对象
 */
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    // 设置用户的位置为地图的中心点
    [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
}

/**
 *  点击屏幕任意位置添加打头针
 *
 *  @param touches <#touches description#>
 *  @param event   <#event description#>
 */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 1.获取用户点击的点
    CGPoint point = [[touches anyObject] locationInView:self.view];
    
    // 2.将该点转化成经纬度(地图上的坐标)
    CLLocationCoordinate2D coordinate = [self.mapView convertPoint:point toCoordinateFromView:self.view];
    
    // 3.添加大头针
    MyAnnotation *anno = [[MyAnnotation alloc] init];
    anno.coordinate = coordinate;
    anno.title = @"大头针";
    anno.subtitle = @"大头针大头针";
    [self.mapView addAnnotation:anno];
}

@end

 

添加大头针

标签:

原文地址:http://www.cnblogs.com/HJiang/p/4344398.html

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