新建一個SingleView工程,代碼以下:git
#import "ViewController.h" #import <CoreLocation/CoreLocation.h> #import <MapKit/MapKit.h> @interface ViewController () <MKMapViewDelegate> { CLLocationManager *_manager; MKMapView *_mapView; } @property (nonatomic,strong) CLLocationManager *manager; @property (nonatomic,strong) MKMapView *mapView;; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self creatMapView]; [self creatAnnoations]; } - (void)creatMapView { //定位管理 self.manager = [[CLLocationManager alloc] init]; CGFloat v = [[[UIDevice currentDevice] systemVersion] doubleValue]; if (v >= 8.0) { //申請驗證 [self.manager requestAlwaysAuthorization]; } //實例化地圖 self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];//bounds是相對於自身視圖 //設置地圖的類型 /* MKMapTypeStandard = 0,//標準的 MKMapTypeSatellite,衛星地圖 */ self.mapView.mapType = MKMapTypeStandard; //設置 地圖顯示的區域範圍(地圖在視圖上的中心點) /* typedef struct { CLLocationCoordinate2D center;//經緯度 MKCoordinateSpan span;//縮放比例(0.01--0.05) } MKCoordinateRegion; */ self.mapView.region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(34.77274892, 113.67591140), MKCoordinateSpanMake(0.01, 0.01)); //是否顯示用戶位置 self.mapView.showsUserLocation = YES; //設置代理 self.mapView.delegate = self; //粘貼到視圖上 [self.view addSubview:self.mapView]; NSArray *titles = @[@"move",@"回到用戶位置"]; for (NSInteger i = 0; i < titles.count; i++) { UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; button.frame = CGRectMake(200*i, 100, 100, 30); [button setTitle:titles[i] forState:UIControlStateNormal]; button.tag = 101+i; [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } } - (void)btnClick:(UIButton *)button { if (button.tag == 101) { //移動-->更改 地圖的中心點位置 //獲取地理位置 CLLocationCoordinate2D coordinate = self.mapView.centerCoordinate; coordinate.latitude += 0.01; coordinate.longitude += 0.01; //更改中心點區域 [self.mapView setCenterCoordinate:coordinate animated:YES]; }else{ //回到用戶原來當前位置 //獲取用戶位置 CLLocation * userLocation = self.mapView.userLocation.location; self.mapView.region = MKCoordinateRegionMake(userLocation.coordinate, MKCoordinateSpanMake(0.01, 0.01)); } } #pragma mark - 大頭針/點標註 - (void)creatAnnoations { //先建立地圖再建立大頭針 /* 點標註:1.點標註視圖(視圖) 2.點標註數據(數據模型) //咱們在地圖上看到的是點標註視圖 點標註的視圖數據 在點標註中 */ //建立點標註數據(大頭針數據) MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init]; //設置大頭針的經緯度 annotation.coordinate = CLLocationCoordinate2DMake(34.77274892, 113.67591140); annotation.title = @"千鋒教育"; annotation.subtitle = @"緯五路21號"; //把大頭針數據添加到地圖上 增長 一個遵照 MKAnnotation的對象 [self.mapView addAnnotation:annotation]; } #pragma mark - MKMapViewDelegate /** * 當大頭針視圖顯示的時候調用 *建立的都是大頭針視圖 * @param mapView 地圖 * @param annotation 傳入的大頭針數據模型 * * @return 大頭針視圖 */ /* 全部的點標註視圖的父類 MKAnnotationView MKPinAnnotationView 是 MKAnnotationView 的子類 */ - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { //採用 複用機制 //和咱們以前講的tableView 相似的 /** * annotation 和 數據模型相似 * MKAnnotationView UITableViewCell相似 * mapView 和UITableView相似 咱們在地圖上加了多少 annotation 數據 當前方法就會調用多少,就會傳入多少個annotation */ //判斷是哪一類大頭針數據 if ([annotation isKindOfClass:[MKPointAnnotation class]]) { //從複用隊列獲取 大頭針視圖MKPinAnnotationView MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"MKPinAnnotationView"]; if (!pinView) { //沒有那麼建立 //根據 點標註模型 annotation 顯示 大頭針的內容 pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MKPinAnnotationView"]; } //設置大頭針視圖的屬性 pinView.animatesDrop = YES;//是否有掉落的效果 /* MKPinAnnotationColorRed = 0, MKPinAnnotationColorGreen, MKPinAnnotationColorPurple */ //設置顏色 pinView.pinColor = MKPinAnnotationColorRed; } return nil; } //大頭針視圖被選中 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { NSLog(@"大頭針被選中"); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end