目录[-]
MapKit框架的使用:
MapKit框架的使用前提:
导入框架 : MapKit.framework; 导入主头文件 : #import<MapKit/MapKit.h>.
MapKit框架使用须知:
MapKit框架中所有数据类型的前缀都是MK;
MapKit有一个比较重要的UI控件 : MKMapView, 专门用于地图显示.
跟踪显示用户的位置:
设置MKMapView的userTrackingMode属性可以跟踪显示用户的当前位置:
MKUserTrackingModeNone : 不跟踪用户的位置;
MKUserTrackingModeFollow : 跟踪并在地图上显示用户的当前位置;
MKUserTrackingModeFollowWithHeading : 跟踪并在地图上显示用户的当前位置,地图会跟随用户的前进方向进行旋转.
地图的类型:
可以通过设置MKMapView的mapViewType设置地图的类型;
MKMapTypeStandard : 普通地图;
MKMapTypeSatellite : 卫星云图;
MKMapTypeHybrid : 普通地图覆盖于卫星云图之上;
MKMapView的代理:
MKMapView可以设置一个代理对象,用来监听地图的相关行为;
常见的代理方法有:
1
|
-(
void
)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation;
|
调用频繁,不断监测用户的当期位置;
每次调用,都会把用户的最新位置(userLocation参数)传进来;
1
|
-(
void
)mapView:(MKMapView *)mapView regionWillChangeAnimated:(
BOOL
)animated;
|
地图的显示区域即将发生改变的时候调用;
1
|
-(
void
)mapView:(MKMapView *)mapView regionDidChangeAnimated:(
BOOL
)animated;
|
地图的显示区域已经发生改变的时候调用;
MKUserLocation:
MKUserLocation其实是个大头针模型,包括以下属性:
1
2
3
|
@property (nonatomic, copy) NSString *title;
//显示在大头针上的标题
@property (nonatomic, copy) NSString *subtitle;
//显示在大头针上的子标题
@property (readonly, nonatomic) CLLocation *location;
//地理位置信息
|
设置地图的显示:
通过MKMapView的下列方法,可以设置地图显示的位置和区域:
1> 设置地图的中心点位置:
1
2
|
@property (nonatomic) CLLocationCoordinate2D centerCoordinate;
-(
void
)setCenterCoordinate:(CLLocationCoordinate2D)coordinate animated:(
BOOL
)animated;
|
2>设置地图的显示区域:
1
2
|
@property (nonatomic) MKCoordinateRegion region;
-(
void
)setRegion:(MKCoordinateRegion)region animated:(
BOOL
)animated;
|
MKCoordinateRegion:
MKCoordinateRegion是一个用来表示区域的结构体,定义如下:
1
2
3
4
5
6
7
8
9
10
11
|
// MKCoordinateRegion的定义
typedef
struct
{
CLLocationCoordinate2D center ;
// 区域的中心点位置
MKCoordinateSpan apan ;
// 区域的跨度
} MKCoordinateRegion ;
// MKCoordinateSpan的定义
typedef
struct
{
CLLocationDegrees latitudeDelta ;
// 纬度跨度
CLLocationDegrees longitudeDelta ;
// 经度跨度
}MKCoordinateSpan ;
|
大头针Annotation:
钉在某个具体位置,用来标识这个位置上有特定的事物(比如这家位置上有家餐馆).
大头针的基本操作:
1
2
3
4
5
6
7
8
9
10
|
/** 添加一个大头针 */
-(
void
)addAnnotation:(id <MKAnnotation>)annotation;
/** 添加多个大头针 */
-(
void
)addAnnotations:(NSArray *)annotations;
/** 移除一个大头针 */
-(
void
)removeAnnotation:(id <MKAnnotation>)annotation;
/** 移除多个大头针 */
-(
void
)removeAnnotations:(NSArray *)annotations;
// (id <MKAnnotation>)annotation参数 :
大头针模型对象:用来封装大头针的数据,比如大头针的位置、标题、子标题等数据
//
|
大头针模型:
新建一个大头针模型:
1
2
3
4
5
6
7
8
9
|
#import <MapKit/MapKit.h>
@interface JNLvtuAnnotation : NSObject <MKAnnotation>
/** 坐标位置 */
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
/** 标题 */
@property (nonatomic, copy) NSString *title;
/** 子标题 */
@property (nonatomic, copy) NSString *subtitle;
@end
|
添加大头针:
1
2
3
4
5
|
JNLvtuAnnotation *anno = [[JNLvtuAnnotation alloc] init];
anno.title = @
"旅途风光"
;
anno.subtitle = @“风光无限好,只是近黄昏";
anno.coordinate = CLLocationCoordinate2DMake(40, 116);
[self.mapView addAnnotation:anno];
|
自定义大头针:
很多情况下,需要自定义大头针的显示样式,比如显示一张图片;
如何自定义大头针:
1> 设置MKMapView的代理 ;
2> 实现下面的代理方法, 返回大头针控件 ;
1
2
|
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation;
// 根据传进来的 (id <MKAnnotation>) annotation 参数创建并返回对应的大头针控件.
|
代理方法的使用注意:
1> 如果返回nil,显示出来的大头针就采用系统的默认样式;
2> 标识用户位置的蓝色发光圆点,它也是一个大头针,当显示这个大头针时,也会调用代理方法;
3> 因此,需要在代理方法中分清楚(id <MKAnnotation>) annotation 参数代表的是自定义大头针还是蓝色发光圆点.
自定义大头针具体方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
// 判断annotation的类型
if
(![annotation isKindOfClass:[JNLvtuAnnotation
class
]])
return
nil;
// 创建MKAnnotationView
static
NSString *ID = @
"Lvtu"
;
MKAnnotationView *annoView = [mapView dequeueReusableAnnotationViewWithIdentifier:ID];
if
(annoView == nil) {
annoView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
annoView.canShowCallout = YES;
}
// 传递模型数据
annoView.annotation = annotation;
// 设置图片
JNLvtuAnnotation *lvtuAnnotation = annotation;
annoView.image = [UIImage imageNamed:lvtuAnnotation.icon];
return
annoView;
}
|
MKAnnotationView
地图上的大头针控件是MKAnnotationView;
MKAnnotationView的属性:
1
2
3
4
5
6
|
@property (nonatomic, strong) id <MKAnnotation> annotation;
//大头针模型
@property (nonatomic, strong) UIImage *image;
//显示的图片
@property (nonatomic)
BOOL
canShowCallout;
//是否显示标注
@property (nonatomic) CGPoint calloutOffset;
//标注的偏移量
@property (strong, nonatomic) UIView *rightCalloutAccessoryView;
//标注右边显示什么控件
@property (strong, nonatomic) UIView *leftCalloutAccessoryView;
//标注左边显示什么控件
|
MKPinAnnotationView:
MKPinAnnotationView是MKAnnotationView的子类 ;
MKPinAnnotationView 比 MKAnnotationView多了2个属性:
1
2
|
@property (nonatomic) MKPinAnnotationColor pinColor;
// 大头针颜色
@property (nonatomic)
BOOL
animatesDrop;
//大头针第一次显示时是否从天而降
|