地图2大头针

    前面的地图与定位中介绍了基础定位和地理编码下面介绍一下大头针的内容

  

 MKAnnotationView:大头针视图

 MKAnnotation:大头针数据模型


1)大头针在地图上的展示

     1MKAnnotationView类似于Cell

     2MKAnnotation类似表视图里面的model

     3- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation类似与初始化cell的地方。

     4》如果只添加了Annotation会自动添加一个大头针视图pinAnnotationView

 2)大头针的介绍

    1》大头针数据模型:MKAnnotation是一个协议

   2》大头针视图:MKAnnotationView自己可以随意定义大头针的样式,

        MKPinAnnotationMKAnnotationView的子类不能改变大头针视图的图片,可以改变大头针的颜色

        大头针视图用于显示内容的View



MKANnotationView 可以自定义

 1)初始化:initWithAnnotation: reuseIdentifier:

 2)属性:image设置大头针图片

 3centerOffset

 中心点的偏移量 x正右 y正下

 4calloutOffset插图的偏移量

 5enabled是否激活默认YES

 6highlighted是否高亮默认NO

 7selected是否选中

  *****8canShowCallout设置是否可以显示插入视图

 9leftCalloutAccessoryView左侧插入视图的附加视图

 10rightCalloutAccessoryView:右侧插入视图的附加视图

 11detailCalloutAccessoryView iOS9之后出现 插入视图的详细视图

 12draggable是否可以拖拽

 13dragState拖拽的状态


具体代码

#import "ViewController.h"

#import <MapKit/MapKit.h>

#import <CoreLocation/CoreLocation.h>

@interfaceViewController ()<MKMapViewDelegate>

{

    CLLocationManager *locationManager;

    MKMapView *mymapView;

    

   NSMutableArray *allAnnotations;//全部的大头针数据模型


}

@property(nonatomic,strong)CLGeocoder *geoceder;

@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

//    初始化locationManager

    locationManager = [[CLLocationManageralloc]init];

//    请求用户授权

    [locationManagerrequestAlwaysAuthorization];

//    初始化mapView

    mymapView = [[MKMapViewalloc]initWithFrame:[UIScreenmainScreen].bounds];

//    地图的样式

       mymapView.mapType =MKMapTypeStandard;

//    显示用户位置

       mymapView.showsUserLocation =YES;

//    挂上代理

       mymapView.delegate =self;

    

    [self.viewaddSubview:mymapView];

    

    

/*

 需求:长按地图上的某一个点,添加一个大头针,大头针显示在地图上的真实位置

 1、地图上添加一个长按手势

 2、添加一个大头针数据模型到地图上

 1)视图上的point需要转换成经纬度

 2)数据模型添加到地图上

 addAnnotation:添加一个大头针数据模型的方法

 addAnnotations:添加一组大头针数据模型

 removeAnnotation:移除一个大头针

 removeAnnotations移除一组大头针

 经纬度和point转换的方法

 把经纬度转成point

convertCoordinate: toPointToView:

 point转换成经纬度

convertPoint:t toCoordinateFromView:

 */

    

    UILongPressGestureRecognizer *addPoint = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(addPoint:)];

    [mymapViewaddGestureRecognizer:addPoint];

}


- (void)addPoint:(UILongPressGestureRecognizer *)sender{

    if (sender.state !=UIGestureRecognizerStateBegan) {

        return;

    }

    

    MKPointAnnotation *pointAnnotation = [[MKPointAnnotationalloc]init];

    

//    通过手势,得到手指触摸的电->point

    CGPoint point = [senderlocationInView:self.view];

//   通过地图类把点转换成经纬度

    CLLocationCoordinate2D coordinate = [mymapViewconvertPoint:pointtoCoordinateFromView:sender.view];

    NSLog(@"%f %f",coordinate.latitude,coordinate.longitude);

    

    

    [self.geocederreverseGeocodeLocation:[[CLLocationalloc]initWithLatitude:coordinate.latitudelongitude:coordinate.longitudecompletionHandler:^(NSArray<CLPlacemark *> *_Nullable placemarks, NSError * _Nullable error) {

        pointAnnotation.coordinate = coordinate;

        pointAnnotation.title = placemarks.firstObject.name;

       [mymapViewaddAnnotation:pointAnnotation];

   //3、添加大头针的时候,同时把大头针的数据存到数组

        [allAnnotationsaddObject:@{@"coordinate":@{@"latitude":@(coordinate.latitude),@"longitude":@(coordinate.longitude)},@"title":placemarks.firstObject.name}];


    }];

    

   

}


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

//   设置用户位置为视图中心点

    [mapView setCenterCoordinate:userLocation.location.coordinateanimated:YES];

    

//   设置地图显示的范围

   MKCoordinateSpan span =MKCoordinateSpanMake(0.1,0.1);

    MKCoordinateRegion region =MKCoordinateRegionMake(userLocation.location.coordinate, span);

    [mapView setRegion:regionanimated:YES];

    

//    userLocation:是一个MKUserLocation-》是一个大头针数据模型(用于表示用户位置)——》遵守MKAnnotation的协议->协议里有经纬度、标题、副标题

//   MKUserLocation:经纬度、标题、副标题航向location

     userLocation.title =@"四叶草";

     userLocation.subtitle =@"幸运符号";

//    MKAnnotationView

  

}


#pragma mark------关于大头针-------

- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{

//   判断是不是用户的大头针模型

    if ([annotationisKindOfClass:[MKUserLocationclass]]) {

//        初始化annotationView大头针视图

        MKAnnotationView *annotationView = [[MKAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"user"];

//        设置大头针视图的图片

        annotationView.image = [UIImageimageNamed:@"四叶草6"];

//        是否允许插入视图

        annotationView.canShowCallout =YES;

//       设置大头针视图中心点偏移

//        x为正是向右

//        Y为正的时候向下

//        annotationView.centerOffset = CGPointMake(100, 0);

//       设置大头针插入视图的偏移量

//        annotationView.calloutOffset = CGPointMake(100, 0);

//       设置大头针视图是否可以拖动

        annotationView.draggable =YES;

//        annotationView.dragState 拖动的状态

//        设置左侧配件视图

        UIImageView *imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,30, 30)];

        imageView.image = [UIImageimageNamed:@"zhangsan"];

//        annotationView.rightCalloutAccessoryView = imageView;

        

//        annotationView.detailCalloutAccessoryView = imageView;

        

        return annotationView;

    }

    

    returnnil;

}


- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    

   //    作业1、判断有没有可以读取的大头针数据模型

    if ([[NSUserDefaultsstandardUserDefaults]boolForKey:@"isSave"]) {

        

        allAnnotations = [[[NSUserDefaultsstandardUserDefaults]arrayForKey:@"allAnnotations"]mutableCopy];

        NSMutableArray *lit = [NSMutableArrayarray];

        

        for (NSDictionary *infoinallAnnotations) {

            MKPointAnnotation *an = [[MKPointAnnotationalloc]init];

            an.coordinate =CLLocationCoordinate2DMake([info[@"coordinate"][@"latitude"]doubleValue], [info[@"coordinate"][@"longitude"]doubleValue]);

            an.title = info[@"title"];

            [lit addObject:an];

        }

        

        //       作业2、如果添加到地图上

        [mymapViewaddAnnotations:lit];

        

    }else{

        allAnnotations = [NSMutableArrayarray];

    }

}

- (void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];

    [self save];

 

}

                                

    - (void)save{

        NSUserDefaults *defaults = [NSUserDefaultsstandardUserDefaults];

        

        if (allAnnotations.count!=0) {

            [defaults setObject:allAnnotationsforKey:@"allAnnotations"];

            [defaults setBool:YESforKey:@"isSave"];

            //    同步保存

            [defaults synchronize];

        }

        

    }

    

- (CLGeocoder *)geoceder{


    if (!_geoceder) {

        _geoceder = [[CLGeocoderalloc]init];

    }

    return_geoceder;

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end




1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REaDME.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 、资源1项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值