说明:
MKMapView是地图控件,支持在地图上画各种标记图标。
一、创建自定义地图标记:
1.创建普通类,继承NSObject,实现MKAnnotation:
PopAnotation.h:
// 地图标记
// PopAnotation.h
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface PopAnotation : NSObject <MKAnnotation>
//经纬度值
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
//标题
@property (nonatomic, copy) NSString *title;
//描述
@property (nonatomic, copy) NSString *desc;
//图标
@property (nonatomic, copy) UIImage *image;
@end
PopAnotation.m:
// 地图标记
// PopAnotation.m
#import "PopAnotation.h"
@implementation PopAnotation
@end
2.根据经纬度值增加自定义MKAnnotation,需要手动调用:
/*
自定义方法:根据经纬度值增加自定义MKAnnotation标记图标
*/
- (void)addItemAnotation2{
//创建有经纬度值的标记图标
PopAnotation *popA = [[PopAnotation alloc] init];
popA.title = @"pop标题";
popA.desc = @"pop描述";
popA.image = [UIImage imageNamed:@"pop1"];
//根据经纬度创建点
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(116.4, 39.9);
popA.coordinate = coordinate;
//将标记图标加入mapView中
[self.mapView addAnnotation:popA];
}
4.重写mapView-viewForAnnotation方法,创建自定义MKAnnotation,替换系统MKAnnotationView中的annotation:
/*
MKMapViewDelegate方法:改变地图标记样式
*/
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
if([annotation isKindOfClass:[PopAnotation class]]