iOS开发——高级技术地图功能的实现
地圖功能的實現
因為有個項目要在地圖中顯示位置,所以用到了MapKit。
記錄下來,以免以后忘記。
加入MapKit library
首先得在項目中加入MapKit,如圖
?
MapView
先增加一個ViewController,我這里用的storyboard,這個玩意還是挺好用的,比以前用xib好多了。
然后拖一個mapview上去,如:
給新增加的ViewController綁定一個class。首先得增加一個class,從uiViewController繼承下來。這個很簡單,如圖
把新增加的ViewController綁定到這個class,也很easy,發現Xcode還是挺牛的。就是在右邊Identity inspector里面的custom class里面改成新增加的類,原來是UIViewController。
然后給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(轉載于:https://www.cnblogs.com/LifeTechnologySupporter/p/4762747.html
總結
以上是生活随笔為你收集整理的iOS开发——高级技术地图功能的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《从零开始学Swift》学习笔记(Day
- 下一篇: Android 4.4沉浸式状态栏的实现