前言
學習地圖,我們必須要接觸兩個框架:
Core Location,主要包含定位、地理編碼、反編碼功能,如需了解請移步iOS開發之地圖-定位/編碼與反編碼
MapKit,利用他可以對地圖進行精準的控制,比如,放置大頭針、地圖類型切換,導航等等。
本文我們主要介紹的是使用MapKit框架對地圖試圖進行精準的控制。
如果無法顯示自己的位置,檢查以下幾點:
是否將定位管理器設為全局變量
是否在項目中進行定位授權,是否在Info.plist中配置
是否將showsUserLocation設為YES。
是否配置模擬器 點擊模擬器 -> 菜單欄Dubug -> Location -> Apple來使模擬器定位,然后使用Custom Location配置模擬器的經緯度。
添加多個大頭針及自定義大頭針視圖代碼示例:
此鏈接下是自定義大頭針的Demo可自行下載
ViewController.m(.h聲明文件中沒有寫代碼)
//
// 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.m中
//
// 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