地圖:MapKit和CoreLocation
簡介:
現在很多的社交軟體都引入了地圖和定位功能,要想實作這2大功能,那就不得不學習其中的2個架構:MaKit和CoreLocation
CoreLocation架構可以使用硬體裝置來進行定位服務
MapKit架構能夠使應用程式做一些地圖展示與互動的相關功能
幾乎所有的iOS裝置都支援位置服務,不過在使用位置服務之前,最好檢查一下可用性
手機定位的三種方式:手機基站、WIFI、GPS
添加架構:
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

添加顯示地圖的MKMapView控件 :
地圖的類型 :
可以通過設定MKMapView的mapViewType設定地圖類型
MKMapTypeStandard 普通地圖(左圖)
MKMapTypeSatellite 衛星雲圖 (中圖)
MKMapTypeHybrid 普通地圖覆寫于衛星雲圖之上(右圖)
MapView的代理:
MapView會将一些事件傳遞給它的代理(遵守MKMapViewDelegate協定),代理方法如下:
mapViewWillStartLoadingMap: 當地圖界面将要加載時調用
mapView:viewForAnnotation: 當地圖上有一些動畫效果展示\加載時調用
mapViewWillStartLocatingUser:當準備進行一個位置定位時調用
mapView:regionDidChangeAnimated: 當顯示的區域發生變化時調用
mapView:didUpdateUserLocation:當使用者位置發生變化時調用
如何添加大頭針(地标):
通過MapView的addAnnotation方法可以添加一個大頭針到地圖上
通過MapView的addAnnotations方法可以添加多個大頭針到地圖上
–(void)addAnnotation:(id <MKAnnotation>)annotation;
說明:需要傳入一個遵守了MKAnnotation協定的對象
基本步驟為:
<1>建立一個遵守MKAnnotation協定的類:
@interface MyAnnotation : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
<2>添加Annotation:
MyAnnotation *anno = [[MyAnnotation alloc] init];
anno.title = @“中國";
anno.subtitle = @“北京”;
//經度和緯度
anno.coordinate = CLLocationCoordinate2DMake(40, 110);
//添加大頭針到地圖中
[_mapView addAnnotation:anno];
// 讓地圖挪動到對應的位置(經緯度交叉處)
[_mapView setCenterCoordinate:anno.coordinate animated:YES];
自定義大頭針:
實作MapView的代理方法
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
static NSString *ID = @"anno";
MKPinAnnotationView *annoView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
if (annoView == nil) {
annoView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:ID];
// 顯示氣泡
annoView.canShowCallout = YES;
// 設定綠色
annoView.pinColor = MKPinAnnotationColorGreen;
}
return annoView;
}
注意:可以通過設定MapAnnotationView的image屬性來自定義大頭針顯示的圖檔
請求使用者準許定位:
在iOS8,要想獲得使用者的位置,必須經過使用者準許授權
開發者可以在Info.plist中添加兩個配置項
–NSLocationAlwaysUsageDescription
–NSLocationWhenInUseUsageDescription
通過兩個執行個體方法擷取授權
–requestWhenInUseAuthorization
–requestAlwaysAuthorization
獲得使用者的位置:
// 定位管理器
_mgr = [[CLLocationManager alloc] init];
// 擷取授權
[_mgr requestAlwaysAuthorization];
// 設定代理
_mgr.delegate = self;
// 設定精度
_mgr.desiredAccuracy = kCLLocationAccuracyBest;
// 開始擷取使用者的位置
[_mgr startUpdatingLocation];
定位管理器的代理方法:
當使用者的位置發生改變時,就會不斷調用代理方法,比如
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations•
當定位失敗時,會調用
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
通過位址獲得經緯度 :
CLGeocoder:地理資訊編解碼
位址 -> 經緯度 -> 大頭針顯示
_geocoder = [[CLGeocoder alloc] init];
[_geocoder geocodeAddressString:@“東三旗" completionHandler:^(NSArray *placemarks, NSError *error) {
//沒有找到符合要求的位址
if (placemarks.count == 0) return;
// 取出位置
CLPlacemark *firstPlacemark = placemarks[0];
// 添加大頭針
MyAnnotation *anno = [[MyAnnotation alloc] init];
anno.title = firstPlacemark.name; // 名稱
anno.subtitle = firstPlacemark.country; // 國家
anno.coordinate = firstPlacemark.location.coordinate; // 坐标
[_mapView addAnnotation:anno];
}];
程式猿神奇的手,每時每刻,這雙手都在改變着世界的互動方式!