MKMapView+MKAnnotation

 

ViewController.h

1 #import <UIKit/UIKit.h>
2 #import "MapKit/MapKit.h"
3 
4 @interface ViewController : UIViewController
5 <CLLocationManagerDelegate,MKMapViewDelegate>
6 {
7     MKMapView *_mapView;
8 }
9 @end

ViewController.m

  1 #import "ViewController.h"
  2 #import "MyAnnotation.h"
  3 
  4 @implementation ViewController
  5 
  6 
  7 - (void)viewDidLoad
  8 {
  9     [super viewDidLoad];
 10     
 11     //初始纬度,经度
 12     CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(39.928168, 116.39328);
 13     //缩放比例
 14     MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
 15     //确定一个区域
 16     MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);
 17     
 18     //地图
 19     _mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
 20     _mapView.delegate = self;
 21     _mapView.mapType = MKMapTypeHybrid;
 22         //地图类型
 23         //MKMapTypeStandard = 0,  标准
 24         //MKMapTypeSatellite,  卫星
 25         //MKMapTypeHybrid    混合
 26     _mapView.region = region;//显示区域
 27     _mapView.showsUserLocation = YES;//显示当前位置
 28     [self.view addSubview:_mapView];
 29     [_mapView release];
 30     
 31     
 32     //定位
 33     CLLocationManager* manager = [[CLLocationManager alloc]init];
 34     manager.distanceFilter = 10;//每隔10米定位一次
 35     manager.desiredAccuracy = kCLLocationAccuracyBest;//精细程度 费电
 36     manager.delegate = self;
 37     [manager startUpdatingLocation];
 38     
 39     //加入大头针
 40     MyAnnotation *anno = [[MyAnnotation alloc]initWithTitle:@"title" SubTitle:@"subtitle" Coordinate:coordinate];
 41     [_mapView addAnnotation:anno];
 42     [anno release];
 43     
 44     //长按手势 插上大头针
 45     UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
 46     [_mapView addGestureRecognizer:longPress];
 47     [longPress release];
 48     
 49 }
 50 
 51 #pragma mark - viewDidLoad UILongPressGestureRecognize longPress:
 52 - (void)longPress:(UILongPressGestureRecognizer*)longPress
 53 {
 54     //长按一次 只在开始插入一次大头针   否则能用大头针写字。。。
 55     if (longPress.state == UIGestureRecognizerStateBegan) {
 56         CGPoint point = [longPress locationInView:_mapView];
 57         CLLocationCoordinate2D coordinate = [_mapView convertPoint:point toCoordinateFromView:_mapView];
 58         MyAnnotation* an = [[MyAnnotation alloc] initWithTitle:@"title" SubTitle:@"subtitle" Coordinate:coordinate];
 59         [_mapView addAnnotation:an];
 60         [an release];
 61     }
 62 }
 63 
 64 
 65 #pragma mark - CLLocationManagerDelegate
 66 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
 67 {
 68     //得到定位后的位置
 69     CLLocationCoordinate2D coordinate = newLocation.coordinate;
 70     MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);
 71     MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span);
 72     [_mapView setRegion:region animated:YES];
 73     //停止定位
 74     [manager stopUpdatingLocation];
 75     [manager release];
 76     
 77 }
 78 -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
 79 {
 80     NSLog(@"定位失败");
 81 }
 82 
 83 #pragma mark - MKMapViewDelegate
 84 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
 85 {
 86     //如果是所在地 跳过   固定写法
 87     if ([annotation isKindOfClass:[mapView.userLocation class]]) {
 88         return nil;
 89     }    
 90     MKPinAnnotationView* pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];
 91     if (pinView == nil) {
 92         pinView = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID"] autorelease];
 93     }
 94     
 95     pinView.canShowCallout = YES;//能显示Call信息 上面那些图字
 96     pinView.pinColor = MKPinAnnotationColorPurple;//只有三种
 97         //大头针颜色
 98         //MKPinAnnotationColorRed = 0,
 99         //MKPinAnnotationColorGreen,
100         //MKPinAnnotationColorPurple
101     pinView.animatesDrop = YES;//显示动画  从天上落下
102     
103     UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
104     view.backgroundColor = [UIColor redColor];
105     pinView.leftCalloutAccessoryView = view;
106     [view release];
107     
108     UIButton* button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
109     pinView.rightCalloutAccessoryView = button;
110     
111     return pinView;
112 }
113 
114 @end

 

 

MyAnnotation.h
 1 #import <Foundation/Foundation.h>
 2 #import <MapKit/MapKit.h>
 3 
 4 @interface MyAnnotation : NSObject
 5 <MKAnnotation>
 6 
 7 @property (nonatomic,retain)NSString *title2;
 8 @property (nonatomic,retain)NSString *subtitle2;
 9 @property (nonatomic,assign)CLLocationCoordinate2D coordinate;
10 
11 - (id)initWithTitle:(NSString*)title SubTitle:(NSString*)subtitle Coordinate:(CLLocationCoordinate2D)coordinate;
12 
13 @end

MyAnnotation.m

 1 #import "MyAnnotation.h"
 2 
 3 @implementation MyAnnotation
 4 @synthesize title2 = _title2;
 5 @synthesize subtitle2 = _subtitle2;
 6 @synthesize coordinate = _coordinate;
 7 
 8 - (id)initWithTitle:(NSString*)title SubTitle:(NSString*)subtitle Coordinate:(CLLocationCoordinate2D)coordinate
 9 {
10     if (self = [super init]) {
11         self.title2 = title;
12         self.subtitle2 = subtitle;
13         self.coordinate = coordinate;
14     }
15     return self;
16 }
17 
18 //不写你就死定了 直接崩
19 - (NSString *)title
20 {
21     return _title2;
22 }
23 - (NSString *)subtitle
24 {
25     return _subtitle2;
26 }
27 - (CLLocationCoordinate2D)coordinate
28 {
29     return _coordinate;
30 }
31 
32 //这个别忘了
33 - (void)dealloc
34 {
35     self.title2 = nil;
36     self.subtitle2 = nil;
37     [super dealloc];
38 }
39 
40 @end

 

 

posted on 2013-01-28 10:45  灰色的人  阅读(2999)  评论(0编辑  收藏  举报

导航