您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

CoreLocation 取得 iOS 位置資訊

編輯:iOS 相關資訊

在 iOS 中,CoreLocation 框架可以取得使用者目前位置,也能追蹤裝置移動資訊。這是地圖、導航、附近商家與位置相關功能的基礎。

基本步驟

  1. 匯入 CoreLocation。
  2. 讓 ViewController 遵循 CLLocationManagerDelegate。
  3. 建立 CLLocationManager 物件。
  4. 設定 delegate、desiredAccuracy 與 distanceFilter。
  5. 呼叫 startUpdatingLocation 開始定位。

在代理方法中,可以取得最新的 CLLocation,並讀取 latitude 與 longitude。例如 didUpdateLocations 會回傳位置陣列,通常取最後一筆即可。

模擬器定位

若在 iPhone Simulator 中只看到開始定位訊息,卻沒有收到位置更新,通常是模擬器定位未開啟。可以在 Xcode 的 Debug 選單中選擇 Simulate Location,或自行輸入經緯度。

此外,iOS 8 之後需要向使用者請求定位權限,因此要在 Info.plist 中加入 NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription,並提供清楚說明。

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