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