您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 地圖實戰:使用 MapKit 並即時描繪運動軌跡

編輯:iOS 開發綜合

本文透過完整程式碼詳細講解 iOS 地圖的使用,示範如何顯示地圖、取得定位,並在移動時即時描繪出軌跡。

第一步:加入框架(雖然現在 Xcode 幾乎會自動處理,但還是保險起見手動加入)

第二步:配置 plist 檔案

第三步:直接上程式碼,請務必仔細閱讀註解。

#import "ViewController.h"
// 導入定位和地圖的兩個框架
#import 
#import 
// 簽訂定位和地圖的代理協議
@interface ViewController ()
// 位置管理者
@property (nonatomic, strong) CLLocationManager *localManager;
// 地圖
@property (nonatomic, strong) MKMapView *mapView;
// 存放用戶位置的數組
@property (nonatomic, strong) NSMutableArray *locationMutableArray;
@end

@implementation ViewController
#pragma mark - 位置管理者懶加載
- (CLLocationManager *)localManager {
    if (_localManager == nil) {
        _localManager = [[CLLocationManager alloc]init];
        // 設置定位的精度
        [_localManager setDesiredAccuracy:kCLLocationAccuracyBest];
        // 位置信息更新最小距離
        _localManager.distanceFilter = 10;
        // 設置代理
        _localManager.delegate = self;
        // 如果沒有授權則請求用戶授權(requestAlwaysAuthorization 為 iOS8 後提出,需加條件判斷防止崩潰)
        if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined && [_localManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [_localManager requestAlwaysAuthorization];
        }
        // 創建存放位置的數組
        _locationMutableArray = [[NSMutableArray alloc] init];
    }
    return _localManager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 全屏顯示地圖並設置地圖的代理
    _mapView = [[MKMapView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _mapView.delegate = self;
    // 是否啟用定位服務
    if ([CLLocationManager locationServicesEnabled]){
        NSLog(@"開始定位");
        // 調用 startUpdatingLocation 後會進入 didUpdateLocations 方法
        [self.localManager startUpdatingLocation];
    } else{
        NSLog(@"定位服務為關閉狀態,無法使用定位服務");
    }
    // 用戶位置追蹤
    _mapView.userTrackingMode = MKUserTrackingModeFollow;
    /** 地圖樣式:
     MKMapTypeStandard        標準地圖
     MKMapTypeSatellite       衛星地圖
     MKMapTypeHybrid          混合地圖
     MKMapTypeSatelliteFlyover 衛星立體地圖
     MKMapTypeHybridFlyover   混合立體地圖 */
    _mapView.mapType = MKMapTypeStandard;
    [self.view addSubview:_mapView];
}

#pragma mark - MKMapViewDelegate
/** 更新用戶位置,只要用戶改變則調用此方法(包括第一次定位到用戶位置)
 第一種畫軌跡的方法:我們用地圖上的變化來描繪軌跡,這種方式不用考慮從 CLLocationManager 取出的經緯度在 mapView 上顯示有偏差的問題 */
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    NSString *latitude = [NSString stringWithFormat:@"%3.5f",userLocation.coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%3.5f",userLocation.coordinate.longitude];
    NSLog(@"更新的用戶位置:緯度:%@, 經度:%@",latitude,longitude);
    // 設置地圖顯示範圍(若不設置會自動顯示區域並以當前用戶位置為中心)
    MKCoordinateSpan span = MKCoordinateSpanMake(0.05, 0.05);
    MKCoordinateRegion region = MKCoordinateRegionMake(userLocation.location.coordinate, span);
    [_mapView setRegion:region animated:true];
    if (_locationMutableArray.count != 0) {
        // 從位置數組中取出最新的位置數據
        NSString *locationStr = _locationMutableArray.lastObject;
        NSArray *temp = [locationStr componentsSeparatedByString:@","];
        NSString *latitudeStr = temp[0];
        NSString *longitudeStr = temp[1];
        CLLocationCoordinate2D startCoordinate = CLLocationCoordinate2DMake([latitudeStr doubleValue], [longitudeStr doubleValue]);
        // 當前確定到的位置數據
        CLLocationCoordinate2D endCoordinate;
        endCoordinate.latitude = userLocation.coordinate.latitude;
        endCoordinate.longitude = userLocation.coordinate.longitude;
        // 移動距離的計算
        double meters = [self calculateDistanceWithStart:startCoordinate end:endCoordinate];
        NSLog(@"移動的距離為%f米",meters);
        // 為了美化移動的軌跡,移動的位置超過 10 米方可添加進位置數組
        if (meters >= 0){
            NSLog(@"添加進位置數組");
            NSString *locationString = [NSString stringWithFormat:@"%f,%f",userLocation.coordinate.latitude, userLocation.coordinate.longitude];
            [_locationMutableArray addObject:locationString];
            // 開始繪制軌跡
            CLLocationCoordinate2D pointsToUse[2];
            pointsToUse[0] = startCoordinate;
            pointsToUse[1] = endCoordinate;
            // 調用 addOverlay 後會進入 rendererForOverlay 方法,完成軌跡繪制
            MKPolyline *lineOne = [MKPolyline polylineWithCoordinates:pointsToUse count:2];
            [_mapView addOverlay:lineOne];
        }else{
            NSLog(@"不添加進位置數組");
        }
    }else{
        // 數組對象個數為 0 說明是第一次進入,將當前位置加入位置數組
        NSString *locationString = [NSString stringWithFormat:@"%f,%f",userLocation.coordinate.latitude, userLocation.coordinate.longitude];
        [_locationMutableArray addObject:locationString];
    }
}

-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id)overlay{
    if ([overlay isKindOfClass:[MKPolyline class]]){
        #pragma clang diagnostic push
        #pragma clang diagnostic ignored "-Wdeprecated-declarations"
        MKPolylineView *polyLineView = [[MKPolylineView alloc] initWithPolyline:overlay];
        polyLineView.lineWidth = 10;   // 折線寬度
        polyLineView.strokeColor = [UIColor blueColor]; // 折線顏色
        return (MKOverlayRenderer *)polyLineView;
        #pragma clang diagnostic pop
    }
    return nil;
}

#pragma mark - CLLocationManagerDelegate
/** 當前定位授權狀態發生改變時調用 */
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:{
            NSLog(@"用戶還未進行授權");
            break;
        }
        case kCLAuthorizationStatusDenied:{
            if([CLLocationManager locationServicesEnabled]){
                NSLog(@"用戶不允許程序訪問位置信息或手動關閉了訪問,可幫助跳轉到設置界面");
                NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                if ([[UIApplication sharedApplication] canOpenURL:url]) {
                    [[UIApplication sharedApplication] openURL: url];
                }
            }else{
                NSLog(@"定位服務關閉,彈出系統提示框,點擊設置可跳轉開啟");
            }
            break;
        }
        case kCLAuthorizationStatusRestricted:{
            NSLog(@"受限制的");
            break;
        }
        case kCLAuthorizationStatusAuthorizedAlways:{
            NSLog(@"授權允許在前台和後台均可使用定位服務");
            break;
        }
        case kCLAuthorizationStatusAuthorizedWhenInUse:{
            NSLog(@"授權允許在前台可使用定位服務");
            break;
        }
        default: break;
    }
}

/** 我們並沒有把從 CLLocationManager 取出來的經緯度放到 mapView 上顯示
 原因:此方法取到的經緯度依據的是地球坐標,但國內地圖顯示按火星坐標標準,
 而 MKMapView 已經做過處理,所以此方法中獲取的坐標直接顯示會有偏差,
 解法很多,可上網查詢,此處不再贅述 */
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *currLocation = [locations lastObject];
    NSString *latitude = [NSString stringWithFormat:@"緯度:%3.5f",currLocation.coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"經度:%3.5f",currLocation.coordinate.longitude];
    NSString *altitude = [NSString stringWithFormat:@"高度值:%3.5f",currLocation.altitude];
    NSLog(@"位置發生改變:緯度:%@,經度:%@,高度:%@",latitude,longitude,altitude);
    [manager stopUpdatingLocation];
}

// 定位失敗的回調方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"無法獲取當前位置 error : %@",error.localizedDescription);
}

#pragma mark - 距離測算
- (double)calculateDistanceWithStart:(CLLocationCoordinate2D)start end:(CLLocationCoordinate2D)end {
    double meter = 0;
    double startLongitude = start.longitude;
    double startLatitude = start.latitude;
    double endLongitude = end.longitude;
    double endLatitude = end.latitude;
    double radLatitude1 = startLatitude * M_PI / 180.0;
    double radLatitude2 = endLatitude * M_PI / 180.0;
    double a = fabs(radLatitude1 - radLatitude2);
    double b = fabs(startLongitude * M_PI / 180.0 - endLongitude * M_PI / 180.0);
    double s = 2 * asin(sqrt(pow(sin(a/2),2) + cos(radLatitude1) * cos(radLatitude2) * pow(sin(b/2),2)));
    s = s * 6378137;
    meter = round(s * 10000) / 10000;
    return meter;
}
@end

以上程式碼詳細說明了地圖的使用,以及真機環境下可能出現的各種授權狀況。定位與位移是常用功能,透過這份程式碼入門,能幫助你更好地開發地圖相關功能。程式碼也實作了即時描繪軌跡的功能——當然它並非完美,加上百度還推出了鷹眼地圖可供查看軌跡。開發時,選擇最適合自己需求的方法來完成功能才是關鍵。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved