標籤:ios
在iOS應用程式中,我們可以使用Map Kit API開發地圖應用,其核心是MKMapView類。本次主要實現的功能是顯示地圖,添加標註,跟蹤使用者位置變化。
(本日誌的所有內容基於已成功擷取座標位置,擷取方法見上篇日誌)
1、顯示地圖
首先需要引用<MapKit/MapKit.h>,它的委託協議是 MKMapViewDelegate。
初始化MapView
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(5, 120, self.view.frame.size.width - 10, 430)]; mapView.mapType = MKMapTypeStandard; mapView.delegate = self;
地圖類型(mapType)有以下3種:
MKMapTypeStandard 標註地圖類型
MKMapSatellite 衛星地圖類型
MKMapTypeHybrid 混合地圖類型
在擷取到位置的placemark後,我自訂了一個函數,實現地圖的顯示
- (void) showInMapView: (CLPlacemark *) placemark{ CLLocationCoordinate2D coordinate = placemark.location.coordinate; // 添加MapView MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);<span style="white-space:pre"></span>// 跨度(比例) MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span); // 範圍、地區 [mapView setRegion:region]; [self.view addSubview: mapView];} 此處只需設定mapView的顯示地區,使用如下結構體:
typedef struct{
CLLocationCoordinate2D center;// 中心點
MKCoordinateSpan Span;// 跨度
}MKCoodinateRegion;
結構體初始化使用 MKCoodinateRegionMake函數,其第一個參數指定了目的地區域中心點,第二個設定目的地區域的跨度
而顯示跨度的結構體MKCoordinateSpan的定義為:
typedef struct{
CLLocationDegress latitudeDelta;// 地區的南北跨度
CLLocationDegress longtitudeDelta;// 地區的東西跨度
}MKCoordinateSpan;
南北跨度1度大約是111公裡,東西跨度在赤道上1度大約是111公裡,越靠近兩極,這個距離在逐漸減小,在極點是變為0公裡。
最後使用setRegion函數,為mapView設定顯示地區。
2、添加標註
首先要引用它的標頭檔<MapKit/MKAnnotation.h>,
首先設定標註點,需使用MKPointAnnotation類,它主要有以下三個屬性
- (NSString *) titile; 標註點的主標題
- (NSString *)subtitle; 標註點的副標題
- (CLLocationCoordinate2D) coordinate; // 標註點的位置資訊
在設定好以上三個屬性後,直接使用addAnnotation函數將標註點添加進mapView。
這部分代碼還是在上面的showInMapView: 函數中,代碼如下
- (void) showInMapView: (CLPlacemark *) placemark{ CLLocationCoordinate2D coordinate = placemark.location.coordinate; // 添加MapView MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01); MKCoordinateRegion region = MKCoordinateRegionMake(coordinate, span); [mapView setRegion:region animated:YES]; [self.view addSubview: mapView]; // 添加Annotation MKPointAnnotation *annotaion = [[MKPointAnnotation alloc] init]; annotaion.coordinate = coordinate; annotaion.title = placemark.locality; annotaion.subtitle = placemark.name; [mapView addAnnotation: annotaion];}
要實現標註的顯示,我們還應實現地圖視圖委託協議中的 mapView: viewForAnnotation:方法,在該方法中,我們執行個體化一個標註點視圖(MKPointAnnotationView),並完成它的初始化工作,實現代碼如下:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{ MKPinAnnotationView *annotaionView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"PIN_ANNOTATION"]; if (annotaionView == nil) { annotaionView = [[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier:@"PIN_ANNOTATION"]; } annotaionView.pinColor = MKPinAnnotationColorRed;<span style="white-space:pre"></span>// 標註點顏色 annotaionView.animatesDrop = YES;<span style="white-space:pre"></span>// 動畫 annotaionView.canShowCallout = YES;<span style="white-space:pre"></span>// 插圖編號 return annotaionView;} 其中 (MBAnnotationView *) - dequeueReusableAnnotationViewWithIdentifier: 通過標識符返回可複用的標註視圖
3、跟蹤使用者位置變化
開啟地圖的showsUserLocation屬性,並設定方法setUserTrackMode:即可實現跟蹤使用者的位置和方向變化。
mapView.showsUserLocation = YES; [mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
其中使用者跟蹤模式有以下三種:
MKUserTrackingModeNone 沒有使用者跟蹤模式
MKUsetTrackingModeFollow 跟蹤使用者的位置變化
MKUserTrackingModeFollowWithHeading 跟蹤使用者位置和方向變化
同時還需實現地圖視圖的委託方法mapView:didUpdataUserLocation:
- (void) mapView:(MKMapView *) mapView didUpdateUserLocation:(MKUserLocation *)userLocation{ mapView.centerCoordinate = userLocation.location.coordinate;}
iOS學習筆記—— MapKit