地图 + 定位
1>
需要子类化协议 :
1 #import <Foundation/Foundation.h> 2 #import <MapKit/MapKit.h> 3 @interface WeiboAnnotation : NSObject<MKAnnotation> 4 5 @property (nonatomic,assign) CLLocationCoordinate2D coordinate; 6 @property (nonatomic, copy) NSString *title; 7 8 @end
2>
1 #import "NearbyWeiboViewController.h" 2 #import <MapKit/MapKit.h> 3 #import "WeiboAnnotation.h" 4 5 @interface NearbyWeiboViewController ()<CLLocationManagerDelegate,MKMapViewDelegate> 6 7 @property (nonatomic,strong)CLLocationManager *locationManger; 8 9 @property (nonatomic,strong)MKMapView *mapView; 10 11 @end 12 13 /** 14 *在地图上显示标注视图MKAnnotationView 15 16 1.创建MKAnnotation协议(model,用来记录标注视图的位置) 17 2.将MKAnnotation类创建的对象加到地图上 18 3.实现协议方法,自己创建标注视图 19 */ 20 21 @implementation NearbyWeiboViewController 22 23 - (void)viewDidLoad { 24 [super viewDidLoad]; 25 26 //创建地图 27 _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; 28 //地图的类型 MKMapTypeSatellite卫星地图 29 _mapView.mapType = MKMapTypeStandard; 30 31 //显示用户的位置(触发协议方法,显示标注视图) 32 _mapView.showsUserLocation = YES; 33 34 _mapView.delegate = self; 35 36 [self.view addSubview:_mapView]; 37 38 //定位 39 _locationManger = [[CLLocationManager alloc] init]; 40 41 //设置精确度 42 _locationManger.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; 43 _locationManger.delegate = self; 44 [_locationManger startUpdatingLocation]; 45 46 47 //创建MKAnnotation用来在地图上显示标注 48 WeiboAnnotation *annotatino = [[WeiboAnnotation alloc] init]; 49 annotatino.coordinate = CLLocationCoordinate2DMake(39.925398, 116.152992); 50 51 //设置标题 52 annotatino.title = @"无线互联"; 53 //将annotatino添加到地图,会触发mapView的协议方法,创建标注视图 54 [_mapView addAnnotation:annotatino]; 55 56 } 57 58 #pragma mark -CLLocationManagerDelegate 59 - (void)locationManager:(CLLocationManager *)manager 60 didUpdateLocations:(NSArray *)locations{ 61 //停止定位 62 [manager stopUpdatingLocation]; 63 64 //取得用户所在的位置 65 CLLocation *location = [locations lastObject]; 66 //取得经纬度 67 CLLocationCoordinate2D coordinate = location.coordinate; 68 69 /** 70 * MKCoordinateRegion{ 71 CLLocationCoordinate2D center;经纬度 72 MKCoordinateSpan span; 区域的详细程度 73 } 74 * 75 * 76 */ 77 78 //span控制区域的详细程度,值越小,地图越详细 79 MKCoordinateSpan span = MKCoordinateSpanMake(.1, .1); 80 81 MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span); 82 //设置地图的显示区域 83 [_mapView setRegion:region animated:YES]; 84 85 } 86 87 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{ 88 89 //如果是用户自己的位置,不需要显示大头针 90 if ([annotation isKindOfClass:[MKUserLocation class]]) { 91 92 return nil; 93 } 94 95 static NSString *identy = @"map"; 96 97 //创建标注视图MKAnnotationView ->子类:MKPinAnnotationView(大头针) 98 /** 99 *MKAnnotationView:指定显示的图片 100 *MKPinAnnotationView:会默认显示大头针 101 * 102 */ 103 MKPinAnnotationView *view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:identy]; 104 if (view == nil) { 105 106 view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identy]; 107 108 //显示图片 109 // view.image = [UIImage imageNamed:@"001.png"]; 110 111 //显示标题 112 view.canShowCallout = YES; 113 114 //设置大头针的颜色 115 view.pinColor = MKPinAnnotationColorGreen; 116 117 //设置辅助视图 118 view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeInfoDark]; 119 120 //从天而降 121 view.animatesDrop = YES; 122 } 123 124 return view; 125 } 126 127 @end