ViewController.m(聲明文件中我沒(méi)有寫(xiě)代碼)
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import "MyAnnotation.h"
#import <CoreLocation/CoreLocation.h>
#import "CustomAnnotationView.h"
@interface ViewController ()<MKMapViewDelegate>
{
CLLocationManager *_locationManager;
MKMapView *_mapView;
}
@property (nonatomic,retain) NSMutableArray *locationArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//定位授權(quán)
_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數(shù)據(jù)轉(zhuǎn)換成大頭針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方法中添加大頭針,大頭針顯示時(shí)會(huì)沒(méi)有掉落效果,定位結(jié)束以后添加大頭針才會(huì)有掉落效果
[self loadData];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{
/*
* 大頭針?lè)謨煞N
* 1. MKPinAnnotationView:他是系統(tǒng)自帶的大頭針,繼承于MKAnnotationView,形狀跟棒棒糖類(lèi)似,可以設(shè)置糖的顏色,和顯示的時(shí)候是否有動(dòng)畫(huà)效果
* 2. MKAnnotationView:可以用指定的圖片作為大頭針的樣式,但顯示的時(shí)候沒(méi)有動(dòng)畫(huà)效果,如果沒(méi)有給圖片的話(huà)會(huì)什么都不顯示
* 3. mapview有個(gè)代理方法,當(dāng)大頭針顯示在試圖上時(shí)會(huì)調(diào)用,可以實(shí)現(xiàn)這個(gè)方法來(lái)自定義大頭針的動(dòng)畫(huà)效果,我下面寫(xiě)有可以參考一下
* 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 = @"火葬場(chǎng)";
}break;
case INTEREST:{
annotationView.image = [UIImage imageNamed:@"cammer.png"];
annotationView.label.text = @"景點(diǎn)";
}break;
}
return annotationView;
}
//大頭針顯示在試圖上時(shí)調(diào)用,我在這里給大頭針設(shè)置顯示動(dòng)畫(huà)
- (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的顯示機(jī)制一樣,都是采用復(fù)用機(jī)制,可以對(duì)比著來(lái)理解。
大頭針數(shù)據(jù)模型 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<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{
if ([super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
//在大頭針旁邊加一個(gè)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