萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

iOS CoreLocation 衛星定位實戰:從專案建置到座標顯示完整教學

前言

CoreLocation 框架讓 iOS 應用輕鬆獲取裝置地理位置 (經緯度、海拔、速度、航向)。本文以「Utility Application」範本為基礎,完整演示從專案建立、框架引入、程式碼實作、介面佈置到實機測試的全流程。

專案建立與環境配置

  1. Xcode → File → New Project → iOS → Application → Utility Application
  2. 專案命名:WhereAmI
  3. 專案設定 → Targets → Linked Frameworks and Libraries → + → 搜尋並加入 CoreLocation.framework

加入 CoreLocation.framework

核心程式碼實作

MainView.h — 介面宣告

#import 
#import 

@interface MainView : UIView  {
    IBOutlet UITextField *altitude;
    IBOutlet UITextField *latitude;
    IBOutlet UITextField *longitude;
    CLLocationManager *locManager;
    BOOL wasFound;
}

- (IBAction)update;
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation;
- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error;

@end

MainView.m — 邏輯實作

#import "MainView.h"

@implementation MainView

- (IBAction)update {
    locManager = [[CLLocationManager alloc] init];
    locManager.delegate = self;
    locManager.desiredAccuracy = kCLLocationAccuracyBest; // 最高精度
    [locManager startUpdatingLocation];
}

- (void)awakeFromNib {
    [self update]; // 介面載入即啟動定位
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    if (wasFound) return; // 僅取首次定位成功
    wasFound = YES;

    CLLocationCoordinate2D coord = newLocation.coordinate;
    latitude.text  = [NSString stringWithFormat:@"%f", coord.latitude];
    longitude.text = [NSString stringWithFormat:@"%f", coord.longitude];
    altitude.text  = [NSString stringWithFormat:@"%f", newLocation.altitude];

    // 實際應用中可在此停止更新以省電:[locManager stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error {
    NSLog(@"定位失敗: %@", error.localizedDescription);
    // 處理權限拒絕、網路不可用等錯誤
}

- (void)dealloc {
    [locManager stopUpdatingLocation];
    locManager.delegate = nil;
    [super dealloc];
}

@end

介面佈置 (MainView.xib)

  1. 雙擊 MainView.xib 進入 Interface Builder
  2. 拖入 3 個 Label (經度、緯度、海拔) 與 3 個 Text Field (唯讀顯示數值)
  3. 將 Text Field 的 Referencing Outlets 分別連接至 File's Owner 的 latitude、longitude、altitude
  4. 加入一個 Button,連接 update Action (可選,供手動重新定位)

介面佈置示意

關鍵技術點補充

主題說明
權限聲明iOS 8+ 需在 Info.plist 加入 NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysAndWhenInUseUsageDescription,否則定位失敗無提示
精度與耗電kCLLocationAccuracyBest 耗電最多;導航用 kCLLocationAccuracyBestForNavigation;一般 App 建議 kCLLocationAccuracyHundredMeters
背景定位需開啟 Background Modes → Location updates,並設定 allowsBackgroundLocationUpdates = YES
模擬器測試Xcode Debug → Simulate Location 可模擬城市、騎行、駕駛、GPX 路徑
iOS 14+ 隱私用戶可選擇「精確位置」關閉,App 只能獲取模糊位置 (約 10km 範圍)

進階應用方向

  • 地理柵欄:startMonitoringForRegion: 進入/離開指定區域觸發通知
  • 訪問歷史:visitMonitoring 記錄用戶停留地點
  • 室內定位:結合 iBeacon / WiFi RTT 提升室內精度
  • 地理編碼:CLGeocoder 經緯度 ↔ 地址字串互轉
copyright © 萬盛學電腦網 all rights reserved