紧接前一篇随笔:在地图中添加大头针 即MKAnnotation得使用

@font-face { font-family: "Wingdings"; }@font-face { font-family: "Cambria Math"; }@font-face { font-family: "Hannotate SC Bold"; }@font-face { font-family: "@Hannotate SC Bold"; }@font-face { font-family: "Heiti SC Light"; }@font-face { font-family: "@Heiti SC Light"; }@font-face { font-family: "Menlo Regular"; }p.MsoNormal, li.MsoNormal, div.MsoNormal { margin: 0cm 0cm 0.0001pt; text-align: justify; font-size: 9pt; font-family: "Hannotate SC Bold"; }p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph { margin: 0cm 0cm 0.0001pt; text-align: justify; text-indent: 21pt; font-size: 9pt; font-family: "Hannotate SC Bold"; }.MsoChpDefault { font-size: 9pt; font-family: "Hannotate SC Bold"; }div.WordSection1 { page: WordSection1; }ol { margin-bottom: 0cm; }ul { margin-bottom: 0cm; }

MKAnnotation(这个协议不需要执行方法,只需要添加属性)

              MKAnnotation类就是大头针类,它的实例就是一个个的大头针;

首先,我们可以自定义一个大头针类

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface TRAnnotation : NSObject<MKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

@property (nonatomic, copy) NSString *title;

@property (nonatomic, copy) NSString *subtitle;

@end

 

在创建时,我们可以切进去MKAnnotation这个类看到

#import <CoreGraphics/CoreGraphics.h>

#import <CoreLocation/CoreLocation.h>

#import <MapKit/MKFoundation.h>

NS_ASSUME_NONNULL_BEGIN

 

@protocol MKAnnotation <NSObject>

 

// Center latitude and longitude of the annotation view.

// The implementation of this property must be KVO compliant.

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

 

@optional

 

// Title and subtitle for use by selection UI.

@property (nonatomic, readonly, copy, nullable) NSString *title;

@property (nonatomic, readonly, copy, nullable) NSString *subtitle;

 

// Called as a result of dragging an annotation view.

-    (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate NS_AVAILABLE(10_9, 4_0);

@end

 

NS_ASSUME_NONNULL_END

 

这个类有三个属性,其中有一个是必须实现的,连个属性时可选择的.

所以在创建以后创建大头针的实例是就必须添加左边属性,同时必不可少的就是要遵守<MKAnnotation>协议

 

然后我们新建一个类作为mapView的代理控制器

注意:1.要手动导入MapKit.frameworw框架

         2.因为地图要结合定位受用,所以要添加plist文件,在info.plist文件中添加NSLocationWhenInUseUsageDescription (前提,是申请了开启GPS定位要求)

         3.因为要征求用户的同意,就必须创建CLLocationManager的对象,来管理定位操作

         4.遵守MKmapViewDelegate协议,既然都要遵守协议了方法就不能少了,常用方法如下:

//已经定位到用户的位置并且显示完

-(void)mapView:(MKMapView*)mapViewdidUpdateUserLocation:(MKUserLocation *)userLocation;

//确定地图发生移动

-    (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;

5.导入<MapKit\MapKit.h>

 

//

//  ViewController.m

//  Demo_1 MKMapView

//

//  Created by tareba on 15/12/16.

//  Copyright © 2015年 tanada. All rights reserved.

//

#import "ViewController.h"

#import <MapKit/MapKit.h>

#import "TRAnnotation.h"

@interface ViewController ()<MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *mapView;//(在sb中创建的mapView视图)

/**

 *  相同点:也要征求用户的同意

 */

@property (nonatomic,strong)CLLocationManager *manager;

- (IBAction)addAnnotation:(UIButton *)sender;//(sb中创建的按钮)

@end

 

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

  //初始化manger

    self.manager=[[CLLocationManager alloc]init];

    //询问 征求用户同意

    [self.manager requestWhenInUseAuthorization];

    //delegate

    self.mapView.delegate=self;

    //设置地图不行允许旋转

    self.mapView.rotateEnabled=NO;

    //设置地图的显示类型(卫星/彼岸准地图/混合)

    //self.mapView.mapType=MKMapTypeHybrid;

    //开始定位

    self.mapView.userTrackingMode=MKUserTrackingModeFollow;

}

#pragma mark - MapViewDelegate

//已经定位到用户的位置并且显示完

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{

    NSLog(@"维度%f,精度%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

    userLocation.title=@"用户位置";

    userLocation.subtitle=@"描述信息";

}

-  (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{

    NSLog(@"地图即将发生移动");

}

//确定地图发生移动

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{

    NSLog(@"地图已经发生移动");

}

 - (IBAction)addAnnotation:(UIButton *)sender {

    //创建模型类

    //遵循协议

    //.h中声明三个属性1个必须,两个可选

    //添加到地图视图上,(必须遵循MKAnnotation协议)

    CLLocationDegrees latitude = 35.123+arc4random_uniform(10);//0~10

    CLLocationDegrees longitude = 116.125+arc4random_uniform(20);

TRAnnotation *annotation=[TRAnnotation new];

    annotation.coordinate=CLLocationCoordinate2DMake(latitude, longitude);

    //设置地图视图的显示区域(大头针都显示到视图的中心位置)

    //跨度(越小精确度越高)

    MKCoordinateSpan span=MKCoordinateSpanMake(0.5, 0.5);

    //中心位置 和 跨度

MKCoordinateRegion region=MKCoordinateRegionMake(annotation.coordinate, span);

//将视图设置的显示区域是设定的大头针区域和跨度

    [self.mapView setRegion:region animated:YES];

    [self.mapView addAnnotation:annotation];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

CGPoint location = [touch locationInView:self.view];

}

@end

 

posted @ 2015-12-17 13:55  严_青  阅读(205)  评论(0编辑  收藏  举报