地圖功能的實現git
由於有個項目要在地圖中顯示位置,因此用到了MapKit。api
記錄下來,以避免之後忘記。服務器
加入MapKit library函數
首先得在項目中加入MapKit,如圖ui
MapViewthis
先增長一個ViewController,我這裏用的storyboard,這個玩意仍是挺好用的,比之前用xib好多了。spa
而後拖一個mapview上去,如:3d
給新增長的ViewController綁定一個class。首先得增長一個class,從uiViewController繼承下來。這個很簡單,如圖code
把新增長的ViewController綁定到這個class,也很easy,發現Xcode仍是挺牛的。就是在右邊Identity inspector裏面的custom class裏面改爲新增長的類,原來是UIViewController。blog
而後給map view控件綁定一個變量,類型是MKMapView
而後就初始化mapview,顯示。代碼以下:
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 // Do any additional setup after loading the view. 5 6 _mapView.mapType = MKMapTypeStandard;//標準模式 7 _mapView.showsUserLocation = YES;//顯示本身 8 9 _mapView.zoomEnabled = YES;//支持縮放 10 11 12 CLLocationCoordinate2D pos = {39.931203, 116.395573};//找個座標,我是用百度座標抓取弄的。http://api.map.baidu.com/lbsapi/getpoint/ 13 14 MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos爲中心,顯示2000米 15 MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//適配map view的尺寸 16 [_mapView setRegion:adjustedRegion animated:YES]; 17 18 19 }
我這裏使用百度座標,找了個座標(直接搜索「百度 座標」),而後在咱們本身的地圖裏顯示。這樣運行一下就能夠看到:
Map view delegate 回調
能夠實現協議MKMapViewDelegate, 這樣就會有幾個回調。
1 - (void) mapViewWillStartLoadingMap:(MKMapView *)mapView//開始從服務器獲取地圖數據 2 { 3 4 } 5 6 -(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView//獲取數據結束 7 { 8 9 } 10 11 - (void) mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error//獲取數據失敗了。 12 { 13 14 }
獲取設備當前位置而且在地圖中顯示
增長一個按鈕,點擊這個按鈕,將顯示設備當前位置。點擊上面的按鈕將顯示某個固定位置。
CLLocationManager,首先使用CLLocationManager來獲取設備的當前位置。
代碼也是很簡單
1 //得到本身的當前的位置信息 2 - (void) getCurPosition 3 { 4 //開始探測本身的位置 5 if (locationManager==nil) 6 { 7 locationManager =[[CLLocationManager alloc] init]; 8 } 9 10 11 if ([CLLocationManager locationServicesEnabled]) 12 { 13 locationManager.delegate=self; 14 locationManager.desiredAccuracy=kCLLocationAccuracyBest; 15 locationManager.distanceFilter=10.0f; 16 [locationManager startUpdatingLocation]; 17 } 18 }
而後實現回調函數
1 #pragma mark -- CLLocationManagerDelegate 2 - (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 3 { 4 if ([locations count] > 0) { 5 CLLocation* loc = [locations objectAtIndex:0]; 6 CLLocationCoordinate2D pos = [loc coordinate]; 7 8 NSLog(@"locationManager, longitude: %f, latitude: %f", pos.longitude, pos.latitude); 9 10 if (show == NO) { 11 MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos爲中心,顯示2000米 12 MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//適配map view的尺寸 13 [_mapView setRegion:adjustedRegion animated:YES]; 14 15 show = YES; 16 } 17 } 18 }
當設備位置變化時,這個函數會被調用。這樣咱們就能夠根據位置來作一些事情了。這個例子裏就在第一次獲取位置的時候更新一下地圖顯示。以設備當前位置爲中心,顯示2000米。
完了。貼一下mapview所在的controller代碼:
1 // 2 // KMapViewController.m 3 // MapDemo 4 // 5 // Created by Kevin on 14-2-10. 6 // Copyright (c) 2014年 Kevin. All rights reserved. 7 // 8 9 #import "KMapViewController.h" 10 11 @interface KMapViewController () 12 13 @end 14 15 @implementation KMapViewController 16 17 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 18 { 19 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 20 if (self) { 21 // Custom initialization 22 } 23 return self; 24 } 25 26 - (void)viewDidLoad 27 { 28 [super viewDidLoad]; 29 // Do any additional setup after loading the view. 30 31 show = NO; 32 33 _mapView.mapType = MKMapTypeStandard;//標準模式 34 _mapView.showsUserLocation = YES;//顯示本身 35 _mapView.delegate = self; 36 _mapView.zoomEnabled = YES;//支持縮放 37 38 39 NSString* i = self.Index; 40 41 if([i isEqualToString:@"1"]) 42 { 43 CLLocationCoordinate2D pos = {39.931203, 116.395573};//找個座標,我是用百度座標抓取弄的。http://api.map.baidu.com/lbsapi/getpoint/ 44 45 MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(pos,2000, 2000);//以pos爲中心,顯示2000米 46 MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];//適配map view的尺寸 47 [_mapView setRegion:adjustedRegion animated:YES]; 48 49 } 50 else 51 { 52 [self getCurPosition]; 53 } 54 55 } 56 57 - (void)didReceiveMemoryWarning 58 { 59 [super didReceiveMemoryWarning]; 60 // Dispose of any resources that can be recreated. 61 } 62 63 - (void) dealloc 64 { 65 66 // [super dealloc]; 67 } 68 69 //得到本身的當前的位置信息 70 - (void) getCurPosition 71 { 72 //開始探測本身的位置 73 if (locationManager==nil) 74 { 75 locationManager =[[CLLocationManager alloc] init]; 76 } 77 78 79 if ([CLLocationManager locationServicesEnabled]) 80 { 81 locationManager.delegate=self; 82 locationManager.desiredAccuracy=kCLLocationAccuracyBest; 83 locationManager.distanceFilter=10.0f; 84 [locationManager startUpdatingLocation]; 85 } 86 } 87 88 #pragma mark -- MPMapViewDelegate 89 90 - (void) mapViewWillStartLoadingMap:(MKMapView *)mapView 91 { 92 93 } 94 95 -(void)mapViewDidFinishLoadingMap:(MKMapView *)mapView 96 { 97 98 } 99 100 - (void) mapViewDidFailLoadingMap:(MKMapView *)mapView withError:(NSError *)error 101 { 102 103 } 104 105 #pragma mark -- CLLocationManagerDelegate 106 - (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 107 { 108 if ([locations count] > 0) { 109 CLLocation* loc = [locations objectAtIndex:0]; 110 CLLocationCoordinate2D pos = [loc coordinate]; 111 112 NSLog(@"locationManager, longitude: %f, latitude: %f", pos.longitude, pos.latitude);