zoukankan      html  css  js  c++  java
  • iOS开发之地图-添加多个自定义的大头针及自定义大头针动画

    前言

    学习地图,我们必须要接触两个框架:

    1. Core Location,主要包含定位、地理编码、反编码功能,如需了解请移步iOS开发之地图-定位/编码与反编码

    2. MapKit,利用他可以对地图进行精准的控制,比如,放置大头针、地图类型切换,导航等等。

        本文我们主要介绍的是使用MapKit框架对地图试图进行精准的控制。


    如果无法显示自己的位置,检查以下几点:

    1. 是否将定位管理器设为全局变量

    2. 是否在项目中进行定位授权,是否在Info.plist中配置

    3. 是否将showsUserLocation设为YES。

    4. 是否配置模拟器 点击模拟器 -> 菜单栏Dubug -> Location -> Apple来使模拟器定位,然后使用Custom Location配置模拟器的经纬度。


    ViewController.m(声明文件中没有写代码)
    //
    //  ViewController.m
    //  CustomeAnnotationView
    //
    //  Created by 王龙 on 16/3/19.
    //  Copyright © 2016年 Larry(Lawrence). All rights reserved.
    //
    
    #import "ViewController.h"
    #import <MapKit/MapKit.h>
    #import <CoreLocation/CoreLocation.h>
    #import "MyAnnotation.h"
    #import "CustomPinAnnotationView.h"
    
    @interface ViewController ()<MKMapViewDelegate>
    {
        CLLocationManager *_locationManager;
        
        MKMapView *_mapView;
    }
    
    @property (nonatomic,retain) NSMutableArray *locationArray;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
    //    定位授权
        
        _locationManager = [[CLLocationManager alloc]init];
        [_locationManager requestWhenInUseAuthorization];
        
        
    //    地图视图
        _mapView = [[MKMapView alloc]initWithFrame:self.view.frame];
        _mapView.showsUserLocation = YES;
        _mapView.delegate = self;
        [self.view addSubview:_mapView];
        
        
    }
    
    - (void)loadData{
        NSString *filePath = [[NSBundle mainBundle]pathForResource:@"PinData" ofType:@"plist"];
        NSArray *tempArray = [NSArray arrayWithContentsOfFile:filePath];
        
    //    把plist数据转换成大头针model
        for (NSDictionary *dict in tempArray) {
            MyAnnotation *myAnnotationModel = [[MyAnnotation alloc]initWithAnnotationModelWithDict:dict];
            
            [self.locationArray addObject:myAnnotationModel];
        }
        
    //    核心代码
        [_mapView addAnnotations:self.locationArray];
        
    }
    
    #pragma mark ------ delegate
    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
        userLocation.title  =@"你好";
        
        _mapView.centerCoordinate = userLocation.coordinate;
        
        [_mapView setRegion:MKCoordinateRegionMake(userLocation.coordinate, MKCoordinateSpanMake(0.3, 0.3)) animated:YES];
        
        
    //    如果在ViewDidLoad中调用  添加大头针的话会没有掉落效果  定位结束后再添加大头针才会有掉落效果
        [self loadData];
        
    }
    
    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
        
        /*
         
         * 大头针分两种
         
         * 1. MKPinAnnotationView:他是系统自带的大头针,继承于MKAnnotationView,形状跟棒棒糖类似,可以设置糖的颜色,和显示的时候是否有动画效果
         
         * 2. MKAnnotationView:可以用指定的图片作为大头针的样式,但显示的时候没有动画效果,如果没有给图片的话会什么都不显示
         
         * 3. mapview有个代理方法,当大头针显示在试图上时会调用,可以实现这个方法来自定义大头针的动画效果,我下面写有可以参考一下
         
         * 4. 在这里我为了自定义大头针的样式,使用的是MKAnnotationView
         
         */
        
        
        //    判断是不是用户的大头针数据模型
        if ([annotation isKindOfClass:[MKUserLocation class]]) {
            MKAnnotationView *annotationView = [[MKAnnotationView alloc]init];
            annotationView.image = [UIImage imageNamed:@"acc"];
            
    //        是否允许显示插入视图*********
            annotationView.canShowCallout = YES;
            
            return annotationView;
        }
        
        CustomPinAnnotationView *annotationView = (CustomPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"otherAnnotationView"];
        if (annotationView == nil) {
            annotationView = [[CustomPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"otherAnnotationView"];
        }
        MyAnnotation *myAnnotation = annotation;
        switch ([myAnnotation.type intValue]) {
            case SUPER_MARKET:
                annotationView.image = [UIImage imageNamed:@"super"];
                annotationView.label.text = @"超市";
                break;
            case CREMATORY:
                annotationView.image = [UIImage imageNamed:@"chang"];
                annotationView.label.text = @"火场";
                break;
            case INTEREST:
                annotationView.image = [UIImage imageNamed:@"jingqu"];
                annotationView.label.text = @"风景区";
                break;
                
            default:
                break;
        }
        
        return annotationView;
    }
    
    
    //大头针显示在视图上时调用,在这里给大头针设置显示动画
    - (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray<MKAnnotationView *> *)views{
        
        
    //    获得mapView的Frame
        CGRect visibleRect = [mapView annotationVisibleRect];
        
        for (MKAnnotationView *view in views) {
            
            CGRect endFrame = view.frame;
            CGRect startFrame = endFrame;
            startFrame.origin.y = visibleRect.origin.y - startFrame.size.height;
            view.frame = startFrame;
            [UIView beginAnimations:@"drop" context:NULL];
            [UIView setAnimationDuration:1];
            view.frame = endFrame;
            [UIView commitAnimations];
            
            
        }
        
        
    }
    
    
    
    #pragma mark lazy load
    
    - (NSMutableArray *)locationArray{
        
        if (_locationArray == nil) {
            
            _locationArray = [NSMutableArray new];
            
        }
        return _locationArray;
    }
    
    @end

    MyAnnotation.h中

    //
    //  MyAnnotation.h
    //  CustomeAnnotationView
    //
    //  Created by 王龙 on 16/3/19.
    //  Copyright © 2016年 Larry(Lawrence). All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    
    
    //注意导入框架
    
    #import <MapKit/MapKit.h>
    
    /**
     *  大头针枚举
     */
    typedef NS_ENUM(NSInteger,PinType) {
        /**
         *  超市
         */
        SUPER_MARKET = 0,
        /**
         *  火场
         */
        CREMATORY,
        /**
         *  景点
         */
        INTEREST,
    };
    
    //该模型是大头针模型 所以必须实现协议MKAnnotation协议 和CLLocationCoordinate2D中的属性coordinate
    @interface MyAnnotation : NSObject<MKAnnotation>
    
    @property (nonatomic,assign) CLLocationCoordinate2D coordinate;
    @property (nonatomic,copy) NSString *name;
    @property (nonatomic,copy) NSString *title;
    @property (nonatomic,retain) NSNumber *type;
    
    - (instancetype)initWithAnnotationModelWithDict:(NSDictionary *)dict;
    
    
    @end

    MyAnnotation.m中

    //
    //  MyAnnotation.m
    //  CustomeAnnotationView
    //
    //  Created by 王龙 on 16/3/19.
    //  Copyright © 2016年 Larry(Lawrence). All rights reserved.
    //
    
    #import "MyAnnotation.h"
    
    @implementation MyAnnotation
    
    
    - (instancetype)initWithAnnotationModelWithDict:(NSDictionary *)dict{
        self = [super init];
        if (self) {
            
            
            self.coordinate = CLLocationCoordinate2DMake([dict[@"coordinate"][@"latitute"] doubleValue], [dict[@"coordinate"][@"longitude"] doubleValue]);
            self.title = dict[@"detail"];
            self.name = dict[@"name"];
            self.type = dict[@"type"];
            
        }
        return self;
    }
    
    @end

    CustomPinAnnotationView.h中

    //
    //  CustomPinAnnotationView.h
    //  CustomeAnnotationView
    //
    //  Created by 王龙 on 16/3/19.
    //  Copyright © 2016年 Larry(Lawrence). All rights reserved.
    //
    
    #import <MapKit/MapKit.h>
    #import "MyAnnotation.h"
    
    @interface CustomPinAnnotationView : MKAnnotationView
    
    
    @property (nonatomic,strong) UILabel *label;
    
    
    @end

    CustomPinAnnotationView.h中

    //
    //  CustomPinAnnotationView.m
    //  CustomeAnnotationView
    //
    //  Created by 王龙 on 16/3/19.
    //  Copyright © 2016年 Larry(Lawrence). All rights reserved.
    //
    
    #import "CustomPinAnnotationView.h"
    
    @implementation CustomPinAnnotationView
    
    
    - (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{
        self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
        if (self) {
            
    //        在大头针旁边加一个label
            self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, -15, 50, 20)];
            self.label.textColor = [UIColor redColor];
            self.label.textAlignment = NSTextAlignmentCenter;
            self.label.font = [UIFont systemFontOfSize:20];
            [self addSubview:self.label];
            
        }
        return self;
        
    }
    
    
    
    @end
  • 相关阅读:
    zeplin使用教程
    如何卸载命令行全局安装的包
    webstrom快捷键
    更新npm至最新版本
    mac环境下安装react项目环境
    横向滚动条布局
    JAVA语法基础——动手动脑
    JAVA语言课堂测试
    暑假第八周进度报告
    暑假第七周进度报告
  • 原文地址:https://www.cnblogs.com/ios-wanglong/p/5295520.html
Copyright © 2011-2022 走看看