CLLocationManager 直接繼承 NSObject,在程式中要用 import CoreLocation 取得其相關工具 (元件) CLLocationManager 的使用方式如下: 生成 CLLocationManager 這物件,並指定其代理 delegate:locationManager = CLLocationManager() locationManager.delegate = self
與 Core Location 這類別相關的常數 Constant: 所以,我們在此只設定取到位置需要的精準度:locationManager.desireAccuracy = kCLLocatinAccruacyBest
取得使用 Core Location 授權: 在此選用 requestAlwaysAuthorization() 方法:locationManager.requestAlwaysAuthorization()
如何取得目前的座標位置? 新增專案: 程式碼:// // ViewController.swift // FindLocationDemo // // Created by Elvis Meng on 2015/9/23. // Copyright © 2015年 Elvis Meng. All rights reserved. // import UIKit import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate{ var locationManager: CLLocationManager! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. locationManager = CLLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() //locationManager.startUpdatingHeading() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let curLocation:CLLocation = locations[0] print("test") print("latitude = \(curLocation.coordinate.latitude)") print("longitude = \(curLocation.coordinate.longitude)") } override func viewDidDisappear(animated: Bool) { locationManager.stopUpdatingLocation() } }
測試環境設定: 新增 2 個環境變數: 開始測試: 允許定位: 此時新增 Privacy 檢查 Location Service 是否開啟 ON: 設定 Apple 已預設城市座標,選擇 Tokyo, Japan: 測試結果: 參考: 1. CLLocationManager, https://developer.apple.com/library/prerelease/watchos/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/cl/CLLocationManager 2. Core Location and Map Kit: Bringing Your Own Maps [Voices That Matter: iPhone 2010], http://www.slideshare.net/invalidname/adamson-bringingyourownmaps 3. Reduce Location Accuracy and Duration, https://developer.apple.com/library/prerelease/ios/documentation/Performance/Conceptual/EnergyGuide-iOS/LocationBestPractices.html 4. CLHeading, https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLHeading_Class/ 5. About Location Services and Maps, https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html 6. Getting the User’s Location, https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW1 7. Core Location Constants Reference, https://developer.apple.com/library/prerelease/tvos/documentation/CoreLocation/Reference/CoreLocationConstantsRef/ 8. CLLocationManagerDelegate, https://developer.apple.com/library/prerelease/watchos/documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/index.html#//apple_ref/occ/intf/CLLocationManagerDelegate
prettyprint
2015年9月23日 星期三
取得目前定位的位置
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言