如何使用地图的注解功能
实现了协议MKAnnotation的对象才能够被用地图上的注解对象,一般都是如下定义的.
DisplayMap.h + DisplayMap.m
// // DisplayMap.h // MoreMapInfo // // Copyright (c) 2014年 Y.X. All rights reserved. // #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface DisplayMap : NSObject <MKAnnotation> @property (nonatomic, copy, readonly) NSString *title; @property (nonatomic, copy, readonly) NSString *subtitle; @property (nonatomic, assign, readonly) CLLocationCoordinate2D coordinate; - (id)initWithTitle:(NSString*)title SubTitle:(NSString*)subtitle Coordinate:(CLLocationCoordinate2D)coordinate; @end
// // DisplayMap.m // MoreMapInfo // // Copyright (c) 2014年 Y.X. All rights reserved. // #import "DisplayMap.h" @implementation DisplayMap - (id)initWithTitle:(NSString*)title SubTitle:(NSString*)subtitle Coordinate:(CLLocationCoordinate2D)coordinate { if (self = [super init]) { _title = title; _subtitle = subtitle; _coordinate = coordinate; } return self; } @end
RootViewController.m
// // RootViewController.m // MoreMapInfo // // Copyright (c) 2014年 Y.X. All rights reserved. // #import "RootViewController.h" #import <MapKit/MapKit.h> #import "DisplayMap.h" #import "LocationCoder.h" #import "YXLocationManager.h" @interface RootViewController ()<CLLocationManagerDelegate, MKMapViewDelegate> @property (nonatomic, strong) MKMapView *mapView; @end @implementation RootViewController - (void)viewDidLoad { [super viewDidLoad]; [YXLocationManager getCurrentLocation:^(CLLocation *location, NSError *error) { if (error == nil) { // 将经纬度解析为地址 LocationCoder *lCoder = [[LocationCoder alloc] initWithLocation:location]; [lCoder startAnalyseLocation]; lCoder.resultBlock = ^(NSArray *placemarks, NSError *error, LocationCoder *locationCoder) { NSLog(@"%@", locationCoder.addressLines); }; //地图 _mapView = [[MKMapView alloc]initWithFrame:self.view.bounds]; _mapView.delegate = self; _mapView.showsUserLocation = YES; _mapView.mapType = MKMapTypeHybrid; _mapView.region = MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(0.1, 0.1)); //加入大头针 DisplayMap *anno = [[DisplayMap alloc]initWithTitle:@"title" SubTitle:@"subtitle" Coordinate:CLLocationCoordinate2DMake(39.928168, 116.39328)]; [_mapView addAnnotation:anno]; [self.view addSubview:_mapView]; //给地图添加长按手势,插上大头针 UILongPressGestureRecognizer* longPress = \ [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [_mapView addGestureRecognizer:longPress]; } else { NSLog(@"%@", error); } }]; } #pragma mark - viewDidLoad UILongPressGestureRecognize longPress: - (void)longPress:(UILongPressGestureRecognizer*)longPress { //长按一次 只在开始插入一次大头针 否则能用大头针写字。。。 if (longPress.state == UIGestureRecognizerStateBegan) { CGPoint point = [longPress locationInView:_mapView]; CLLocationCoordinate2D coordinate = [_mapView convertPoint:point toCoordinateFromView:_mapView]; DisplayMap* an = [[DisplayMap alloc] initWithTitle:@"title" SubTitle:@"subtitle" Coordinate:coordinate]; [_mapView addAnnotation:an]; [_mapView setCenterCoordinate:coordinate animated:YES]; } } #pragma mark - MKMapViewDelegate - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { //如果是所在地,跳过(固定写法) if ([annotation isKindOfClass:[mapView.userLocation class]]) { return nil; } // 重用 MKPinAnnotationView* pinView = (MKPinAnnotationView*)\ [mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"]; if (pinView == nil) { pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID"]; } //能显示Call信息上面那些图字(很重要哦) pinView.canShowCallout = YES; //只有三种 pinView.pinColor = MKPinAnnotationColorPurple; //显示动画 - 从天上落下 pinView.animatesDrop = YES; pinView.image = [UIImage imageNamed:@"地图标点"]; // 左侧CalloutAccessoryView(使用了自定义view) UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]; view.backgroundColor = [UIColor redColor]; pinView.leftCalloutAccessoryView = view; // 右侧CalloutAccessoryView(使用了系统的按钮) UIButton* button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; pinView.rightCalloutAccessoryView = button; return pinView; } - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { NSLog(@"点击事件"); } @end
打印信息:
2014-05-26 10:24:46.005 MoreMapInfo[6138:60b] AKLocation - [Line 118]
Started locating
==============
Distance filter accuracy: -1.00
Desired accuracy: 100.00
Timeout: 10.00
May 26 10:24:47 Phoenix MoreMapInfo[6138] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 512 bytes/row.
2014-05-26 10:24:47.331 MoreMapInfo[6138:9c07] vImage decode failed, falling back to CG path.
May 26 10:24:47 Phoenix MoreMapInfo[6138] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 512 bytes/row.
2014-05-26 10:24:47.372 MoreMapInfo[6138:8f0b] vImage decode failed, falling back to CG path.
2014-05-26 10:24:47.462 MoreMapInfo[6138:60b] 中国北京市东城区东四街道东四朝阳门北小街2-1号
May 26 10:24:48 Phoenix MoreMapInfo[6138] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 512 bytes/row.
2014-05-26 10:24:48.363 MoreMapInfo[6138:a803] vImage decode failed, falling back to CG path.
May 26 10:24:48 Phoenix MoreMapInfo[6138] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 512 bytes/row.
2014-05-26 10:24:48.375 MoreMapInfo[6138:a603] vImage decode failed, falling back to CG path.
May 26 10:24:48 Phoenix MoreMapInfo[6138] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 512 bytes/row.
2014-05-26 10:24:48.402 MoreMapInfo[6138:9607] vImage decode failed, falling back to CG path.
May 26 10:24:48 Phoenix MoreMapInfo[6138] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 512 bytes/row.
May 26 10:24:48 Phoenix MoreMapInfo[6138] <Error>: CGBitmapContextCreate: unsupported parameter combination: 5 integer bits/component; 16 bits/pixel; 3-component color space; kCGImageAlphaNoneSkipLast; 512 bytes/row.
2014-05-26 10:24:48.420 MoreMapInfo[6138:910b] vImage decode failed, falling back to CG path.
2014-05-26 10:24:48.421 MoreMapInfo[6138:9e07] vImage decode failed, falling back to CG path.
2014-05-26 10:25:10.317 MoreMapInfo[6138:60b] 点击事件
要点:
// 能显示Call信息上面那些图字(很重要哦)
pinView.canShowCallout = YES