iOS開發--地圖與定位

一、定位

實現定位需要使用Core Location框架(Core Location框架屬于Core Services層):

  • 添加CoreLocation.framework框架
  • #import <CoreLocation/CoreLocation.h>

步驟:

  • 創建定位管理對象
  • 設置定位管理對象(設置過濾、設置定位精度)
  • 設置定位管理對象的代理
  • 實現<CLLocationManagerDelegate>協議里的代理方法
  • 開始定位

示例代碼:

創建定位管理對象

//創建一個定位管理對象
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
//要求locationManager對象返回全部信息,參數用于過濾信息,一般添kCLDistanceFilterNone意思是不過濾
[locationManager setDistanceFilter:kCLDistanceFilterNone];
//設置定位精度,精度越高耗電量越高
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
//設置代理
locationManager.delegate = self;
//開始定位,這是異步方法
[locationManager startUpdatingLocation];

實現協議方法

實現<CLLocationManagerDelegate>協議里的代理方法,方法在定位開始后會不斷被調用。

iOS6.0之前:
- (void) locationManager:(CLLocationManagerr *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//從newLocation對象中獲取當前的經緯度坐標,CLLocationCoordinate2D是結構體
CLLocationCoordinate2D coord2D = newLocation.coordinate;
NSLog(@"%f, %f", coord2D.longitude, coord2D.latitude);
[manager stopUpdatingLocation];
}

iOS6.0之后:
- (void) locationManager: (CLLocationManager *)manager didUpdateLocations: (NSArray *)locations
{
for(CLLocation *location in locations)
{
CLLocationCoordinate2D coord2D = location.coordinate;
NSLog(@"%f, %f", coord2D.longitude, coord2D.latitude);
}
}

Tip:

CLLocation解析:newlocation是CLLocation對象。

  • 獲取經緯度坐標
    CLLocationCoordinate2D coord2D = newLocation.coordinate;
  • 獲取精度,水平精度和垂直精度
    CLLocationAccuracy horizontal = newLocation.horizontalAccuracy;
    CLLocationAccuracy vertical = newLocation.verticalAccuracy;
  • 獲取高度
    CLLocationDistance altitude = newLocation.altitude;
  • 系統獲取位置信息所用的時間
    NSDate timestamp = [newLocation timestamp];
  • 計算兩個位置間的距離
    CLLocationDistance distance = [newLocation distanceFromLoation: oldLocation];

二、位置的反編碼

位置的反編碼就是將經緯度轉換成具體的城市位置信息。iOS5.0之后使用CLGeocoder類用于反編碼處理。

步驟

  • 創建反編碼處理對象
  • 反編碼處理對象調用

示例代碼

//創建一個反編碼處理對象
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//geocoder對象調用位置反編碼方法,傳入CLLocation類型的參數(要反編碼的位置信息)
[geocoder reverseGeocodeLocation: newLocation 
                completionHandler:^(NSArray *placemarks, NSError *error)
                {
                    for(CLPlacemark *place in placemarks)
                    {
                        NSLog(@"name, %@", place.name);                         //位置名
                        NSLog(@"thoroughfare, %@", place.thoroughfare);         //街道
                        NSLog(@"subThoroughfare, %@", place.subThoroughfare);   //子街道
                        NSLog(@"locality, %@", place.locality);                 //市
                        NSLog(@"subLocality, %@", place.subLocality);           //區
                        NSLog(@"country, %@", place.country);                   //國家
                    }
                }]

三、地圖

  • 使用MapKit框架可以在地圖上顯示位置信息。
  • 使用地圖服務時,需要添加MapKit.framework框架。
    #import <MapKit/MapKit.h>
  • MapKit的大部分功能由MKMapView實現,它主要負責地圖視圖的顯示。

步驟:

  • 創建MKMapView類型的視圖對象
  • 給MKMapView視圖對象設置代理
  • 設置是否顯示用戶當前位置
  • 設置地圖顯示類型
  • 提供經緯度坐標
  • 設置顯示區域的精度
  • 根據經緯度坐標和顯示范圍精度來創建一個顯示區域
  • 設置地圖的顯示區域
  • 實現代理方法

示例代碼

//創建地圖視圖對象
MKMapView *mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
//設置地圖視圖的代理
mapView.delegate = self;
//設置是否顯示用戶當前位置
mapView.showsUserLocation = YES;
//設置地圖顯示類型:標準地圖、衛星地圖、混合地圖
mapView.mapType = MKMapTypeStandard;
//提供經緯度信息
CLLocationCoordinate2D coord2D = {39.910650, 116.47030};
//顯示區域精度
MKCoordinateSpan span = {0.1, 0.1};
//設置顯示區域,MKCoordinateRegion是結構體類型
MKCoordinateRegion region = {coord2D, span};
//給地圖設置顯示區域
[mapView setRegion: region animated: YES];
[self.view addSubview: mapView];

常用的代理方法

  • 返回標注的視圖(大頭針視圖)調用
    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation: (id <MKAnnotation>)annotation;
  • 更新當前的位置時調用
    - (void) mapView: (MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation;
  • 選中標注視圖時調用
    - (void) mapView: (MKMapView *) didSelectAnnotationView: (MKAnnotationView *)view;
  • 地圖的顯示區域重新設置時調用
    - (void) mapView: (MKMapView *)mapView regionDidChangeAnimated: (BOOL)animated;
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容