MKMapView+MKAnnotationView
地图+大头针
程序包涵MapKit.framework+CoreLocation.framework两个库
入口函数接口
1 // ViewController.h 2 // maps 3 4 #import <UIKit/UIKit.h> 5 #import <MapKit/MapKit.h> 6 7 @interface ViewController : UIViewController<CLLocationManagerDelegate,MKMapViewDelegate> 8 { 9 MKMapView *_mapView; 10 } 11 12 @end
入口函数实现
1 // ViewController.m 2 // maps 3 4 #import "ViewController.h" 5 #import "MyAnnotation.h" 6 7 @implementation ViewController 8 9 #pragma mark - View lifecycle 10 11 - (void)viewDidLoad 12 { 13 [super viewDidLoad]; 14 // Do any additional setup after loading the view, typically from a nib. 15 16 CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(40.035599, 116.351137);//经纬度 17 MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);//比例 18 MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);//地域 19 20 _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)]; 21 [self.view addSubview:_mapView]; 22 [_mapView release]; 23 _mapView.region = region;//设置地域 24 _mapView.mapType = MKMapTypeStandard;//默认 25 _mapView.mapType = MKMapTypeSatellite;//卫星 26 _mapView.mapType = MKMapTypeHybrid; //混合 27 _mapView.showsUserLocation = YES;//显示用户位置(定位后) 28 _mapView.delegate = self;//<MKMapViewDelegate> 29 30 CLLocationManager *manager = [[CLLocationManager alloc]init];//定位器 31 manager.distanceFilter = 10;//每移动10米定位一次 32 manager.desiredAccuracy = kCLLocationAccuracyBest;//定位精准度 33 manager.delegate = self;//<CLLocationManagerDelegate> 34 //[manager startUpdatingLocation];//开始定位 35 36 //加入大头针 37 MyAnnotation *anno = [[MyAnnotation alloc]initWithTitle:@"title" SubTitle:@"subtitle" Coordinate:coordinate]; 38 [_mapView addAnnotation:anno]; 39 [anno release]; 40 41 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];//长按触发手势. 42 [_mapView addGestureRecognizer:longPress]; 43 [longPress release]; 44 } 45 46 //<MKMapViewDelegate>包涵下面一个 47 -(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation//设置所有的大头针_____其实根本不叫大头针,叫注释!!!!!!! 48 { 49 if ([annotation isKindOfClass:[mapView.userLocation class]]) 50 return nil; 51 //标示用户位置的大头针不执行.意思就是用默认的啦 52 53 MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"]; 54 if (pinView ==nil) 55 pinView = [[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"ID"]autorelease]; 56 57 pinView.canShowCallout = YES;//点击大头针可以出现信息 58 pinView.pinColor = MKPinAnnotationColorPurple;//大头针的颜色 59 pinView.animatesDrop = YES;//大头针出场动画 60 61 UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)]; 62 view.backgroundColor = [UIColor redColor]; 63 pinView.leftCalloutAccessoryView = view; 64 65 UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 66 pinView.rightCalloutAccessoryView = button; 67 68 return pinView; 69 } 70 71 -(void)longPress:(UILongPressGestureRecognizer *)longPress//长按触发 72 { 73 if (longPress.state == UIGestureRecognizerStateBegan)//点住不放只执行一次 74 { 75 CGPoint point = [longPress locationInView:_mapView];//点击的坐标 76 CLLocationCoordinate2D coordinate = [_mapView convertPoint:point toCoordinateFromView:_mapView];//坐标转为经纬度 77 MyAnnotation *an = [[MyAnnotation alloc]initWithTitle:@"titile" SubTitle:@"subtitle" Coordinate:coordinate]; 78 [_mapView addAnnotation:an]; 79 [an release]; 80 } 81 } 82 83 //<CLLocationManagerDelegate>下面2个 84 -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 85 { 86 NSLog(@"定位成功"); 87 CLLocationCoordinate2D coordinate = newLocation.coordinate;//得到定位后的位置 88 MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1); 89 MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span); 90 [_mapView setRegion:region animated:YES];//跳到定位后的区域 91 92 //[manager stopUpdatingLocation];//停止定位 93 //[manager release];//停止定位以后再释放 94 } 95 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 96 { 97 NSLog(@"定位失败"); 98 } 99 100 @end
大头针接口
1 // MyAnnotation.h 2 // maps 3 4 #import <Foundation/Foundation.h> 5 #import <MapKit/MapKit.h> 6 7 @interface MyAnnotation : NSObject<MKAnnotation> 8 { 9 NSString * _title2; 10 NSString *_subtitle2; 11 CLLocationCoordinate2D _coordinate; 12 } 13 14 @property(nonatomic,retain)NSString* title2; 15 @property(nonatomic,retain)NSString* subtitle2; 16 @property(nonatomic,assign)CLLocationCoordinate2D coordinate; 17 18 -(id)initWithTitle:(NSString*)title SubTitle:(NSString*)subtitle Coordinate:(CLLocationCoordinate2D)coordinate; 19 20 @end
大头针实现
1 // MyAnnotation.m 2 // maps 3 4 #import "MyAnnotation.h" 5 6 @implementation MyAnnotation 7 @synthesize title2 = _title2; 8 @synthesize subtitle2 = _subtitle2; 9 @synthesize coordinate = _coordinate; 10 11 -(id)initWithTitle:(NSString *)title SubTitle:(NSString *)subtitle Coordinate:(CLLocationCoordinate2D)coordinate 12 { 13 if (self = [super init]) { 14 self.title2 = title; 15 self.subtitle2 = subtitle; 16 self.coordinate = coordinate; 17 } 18 return self; 19 } 20 21 - (NSString *)title 22 { 23 return _title2; 24 } 25 26 -(NSString *)subtitle 27 { 28 return _subtitle2; 29 } 30 31 -(CLLocationCoordinate2D)coordinate 32 { 33 return _coordinate; 34 } 35 36 -(void)dealloc 37 { 38 self.title2 = nil; 39 self.subtitle2 = nil; 40 [super dealloc]; 41 } 42 43 @end