博客> iOS开发之地图-添加多个自定义的大头针及自定义大头针动画
iOS开发之地图-添加多个自定义的大头针及自定义大头针动画
2017-10-19 00:03 评论:0 阅读:271 孤雁南飞
ios

前言 编程之前,需要了解以下知识: iOS开发之地图-地图显示/大头针 iOS开发之地图-定位/编码与反编码

代码实现 下面直接将我建的MVC给大家,我已经尽可能的加上注释了,如有不清楚建议先看看上面推荐的两篇文章,如果还不清楚,请留言。

ViewController.m(声明文件中我没有写代码)

// // ViewController.m // AddManyCustomAnnotation // // Created by GG on 16/3/10. // Copyright © 2016年 GG. All rights reserved. //

import "ViewController.h"

import

import "MyAnnotation.h"

import

import "CustomAnnotationView.h"

@interface ViewController () { CLLocationManager *_locationManager;

MKMapView *_mapView;

}

@property (nonatomic,retain) NSMutableArray *locationArray;

@end

@implementation ViewController

  • (void)viewDidLoad { [super viewDidLoad];

    //定位授权 _locationManager = [[CLLocationManager alloc]init]; [_locationManager requestAlwaysAuthorization];

    //地图试图 _mapView = [[MKMapView alloc]initWithFrame:self.view.frame]; _mapView.showsUserLocation = YES; _mapView.delegate = self; [self.view addSubview:_mapView];

}

pragma mark private Method

//添加大头针

  • (void)loadData{

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"PinData" ofType:@"plist"];

    NSArray *tempArray = [NSArray arrayWithContentsOfFile:filePath];

    //将pist数据转换成大头针model for (NSDictionary *dict in tempArray) {

    MyAnnotation *myAnnotationModel = [[MyAnnotation alloc]initAnnotationModelWithDict:dict];

    [self.locationArray addObject:myAnnotationModel];

    }

    [_mapView addAnnotations:self.locationArray];

}

pragma mark delegate

  • (void)mapView:(MKMapView )mapView didUpdateUserLocation:(MKUserLocation )userLocation{

    userLocation.title = @"1234"; _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 )annotation{

    /*

    • 大头针分两种

      1. MKPinAnnotationView:他是系统自带的大头针,继承于MKAnnotationView,形状跟棒棒糖类似,可以设置糖的颜色,和显示的时候是否有动画效果
      1. MKAnnotationView:可以用指定的图片作为大头针的样式,但显示的时候没有动画效果,如果没有给图片的话会什么都不显示
      1. mapview有个代理方法,当大头针显示在试图上时会调用,可以实现这个方法来自定义大头针的动画效果,我下面写有可以参考一下
      1. 在这里我为了自定义大头针的样式,使用的是MKAnnotationView

      */ if ([annotation isKindOfClass:[MKUserLocation class]]) {

      MKAnnotationView *annotationView = [[MKAnnotationView alloc]init];

      annotationView.image = [UIImage imageNamed:@"user.png"];

      annotationView.canShowCallout = YES;

      return annotationView;

    }

    CustomAnnotationView annotationView = (CustomAnnotationView )[mapView dequeueReusableAnnotationViewWithIdentifier:@"otherAnnotationView"];

    if (annotationView == nil) {

    annotationView = [[CustomAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"otherAnnotationView"];

    }

    MyAnnotation *myAnnotation = annotation;

    switch ([myAnnotation.type intValue]) {

    case SUPER_MARKET:{
    
        annotationView.image = [UIImage imageNamed:@"buy.png"];
        annotationView.label.text = @"超市";
    
    }break;
    
    case CREMATORY:{
    
        annotationView.image = [UIImage imageNamed:@"fire.png"];
        annotationView.label.text = @"火葬场";
    
    }break;
    
    case INTEREST:{
    
        annotationView.image = [UIImage imageNamed:@"cammer.png"];
        annotationView.label.text = @"景点";
    
    }break;

    } return annotationView;

}

//大头针显示在试图上时调用,我在这里给大头针设置显示动画

  • (void) mapView:(MKMapView )mapView didAddAnnotationViews:(NSArray )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 PS: 大头针的显示跟cell的显示机制一样,都是采用复用机制,可以对比着来理解。

大头针数据模型 MyAnnotation.h

// // MyAnnotation.h // AddManyCustomAnnotation // // Created by GG on 16/3/13. // Copyright © 2016年 GG. All rights reserved. //

import

import

typedef NS_ENUM(NSInteger, PinType) { /**

  • 超市 */ SUPER_MARKET = 0, /**
  • 火化场 */ CREMATORY, /**
  • 景点 */ INTEREST };

//该模型是大头针模型,所以我必须实现MKAnnotation协议,而且必须实现CLLocationCoordinate2D coordinate属性 @interface MyAnnotation : NSObject

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;

@property (nonatomic, copy) NSString *name;

@property (nonatomic, copy) NSString *title;

@property (nonatomic, retain) NSNumber *type;

  • (instancetype)initAnnotationModelWithDict:(NSDictionary *)dict;

@end 大头针数据模型 MyAnnotation.m

// // MyAnnotation.m // AddManyCustomAnnotation // // Created by GG on 16/3/13. // Copyright © 2016年 GG. All rights reserved. //

import "MyAnnotation.h"

@implementation MyAnnotation

  • (instancetype)initAnnotationModelWithDict:(NSDictionary *)dict{

    self = [super init];

    if (self) {

    self.coordinate = CLLocationCoordinate2DMake([dict[@"coordinate"][@"latitude"] doubleValue], [dict[@"coordinate"][@"longitude"] doubleValue]);
    
    self.title = dict[@"detail"];
    
    self.name = dict[@"name"];
    
    self.type = dict[@"type"];

    }

    return self;

}

@end 大头针试图 CustomPinAnnotationView.h

// // CustomPinAnnotationView.h // AddManyCustomAnnotation // // Created by GG on 16/3/13. // Copyright © 2016年 GG. All rights reserved. //

import

import "MyAnnotation.h"

@interface CustomAnnotationView : MKAnnotationView

@property (nonatomic, retain)UILabel *label;

@end 大头针试图 CustomPinAnnotationView.m

// // CustomPinAnnotationView.m // AddManyCustomAnnotation // // Created by GG on 16/3/13. // Copyright © 2016年 GG. All rights reserved. //

import "CustomAnnotationView.h"

@implementation CustomAnnotationView

  • (instancetype)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier{

    if ([super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) { //在大头针旁边加一个label self.label = [[UILabel alloc] initWithFrame:CGRectMake(0, -15, 50, 20)]; self.label.textColor = [UIColor grayColor]; self.label.textAlignment = NSTextAlignmentCenter; self.label.font = [UIFont systemFontOfSize:10]; [self addSubview:self.label];

    }

    return self; }

@end

收藏
0
sina weixin mail 回到顶部