ViewController.m(声明文件中我没有写代码)
#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,形状跟棒棒糖类似,可以设置糖的颜色,和显示的时候是否有动画效果
* 2. MKAnnotationView:可以用指定的图片作为大头针的样式,但显示的时候没有动画效果,如果没有给图片的话会什么都不显示
* 3. mapview有个代理方法,当大头针显示在试图上时会调用,可以实现这个方法来自定义大头针的动画效果,我下面写有可以参考一下
* 4. 在这里我为了自定义大头针的样式,使用的是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.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.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