地圖的上一篇提到了如何顯示靜態地點:[iOS][Xcode][Swift]iPhone手機編程--在APP中加入地圖Map(MapKit)初級應用--靜態地點顯示
作為一款地圖APP,追蹤用戶實時位置的功能自然必不可少,
要存取用戶信息,就需要涉及到權限的問題,
這個詢問權限的畫面大家都應該不會陌生
只有被允許了,APP才能獲得用戶實時位置
獲取用戶位置權限
Step 1. 導入CoreLocation
Step 2. 在Info.plist的Key中加入兩個權限
Privacy - Location When In Use Usage Description
Privacy - Location Always and When In Use Usage Description
(直接複製貼上就可以了,記得要刪除多餘空行)
Step 3. 加入Delegate
一般加入Delegate都需要在Storyboard中將Objects拖曳到Code,
(不懂的詳見另一篇:[iOS][Xcode][Swift]iPhone手機編程--Text Field在APP中的簡單設定(彈出收起鍵盤收縮頁面, UITextFieldDelegate, Gesture, Tap))
而有些"Objects"是無法在StoryBoard中找到的,
這時可以直接在Code中輸入CLLocationManager():
輸入後便能成功連接Delegate啦~
Step 4. 調用用戶最後位置的function
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations.last?.coordinate)
//print的結果不會顯示在"電話"上,只在後台調試中出現。然而這步的重點不是在於print,而是要call出最後坐標
}
Step 5. 在APP預加載時便向用戶申請權限
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
//調用Step 2預備好的權限
locationManager.delegate=self
locationManager.desiredAccuracy=kCLLocationAccuracyBest
//實時位置的準確度,其中AccuracyBest為精確度最高
順便貼上APPLE的官方解釋,有需要的可以看看,簡單而言,1-6,1精度最高、有精度最低:
Code | Description | |
1 | kCLLocationAccuracyBestForNavigation | The highest possible accuracy that uses additional sensor data to facilitate navigation apps. |
2 | kCLLocationAccuracyBest | The best level of accuracy available. |
3 | kCLLocationAccuracyNearestTenMeters | Accurate to within ten meters of the desired target. |
4 | kCLLocationAccuracyHundredMeters | Accurate to within one hundred meters. |
5 | kCLLocationAccuracyKilometer | Accurate to the nearest kilometer. |
6 | kCLLocationAccuracyReduced | The level of accuracy used when an app isn' t authorized for full accuracy location data. |
Step 6. 開啓追蹤模式
Map.userTrackingMode = .follow
完成~~
如果有跟著做的親會發現做到最後一步,
虛擬器中的地圖還是沒有絲毫變化,
那時當然,
"用戶"又沒動,
地圖上的點當然不會動啊,
那難道要拿著機子邊走邊測試嗎?
呵呵,
少年你太年輕了,
請看上面的MARK,
就是在虛擬器中開啓開車、跑步等模式:
最後,上面的STEP都是按照邏輯及操作順序排的,
不代表CODE中的先後,
故此貼上完整的CODE供參考。
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
let locationManager=CLLocationManager()
//顯示實時位置
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations.last!.coordinate) //show the last coordinate
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//MARK: request for location authority
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
locationManager.delegate=self //link the deldgete to the view controller
locationManager.desiredAccuracy=kCLLocationAccuracyThreeKilometers
//MARK: set the center of the map
let initCenter = CLLocationCoordinate2DMake(22.3600578, 114.1131754)
map.setCenter(initCenter, animated: true)
let region=MKCoordinateRegion(center: initCenter, latitudinalMeters: 1200, longitudinalMeters: 1200)
map.setRegion(region, animated: true) //set region 比例尺
//MARK: set the point of the map center
let annotation=MKPointAnnotation()
annotation.title="I am here"
annotation.coordinate=initCenter
map.addAnnotation(annotation)
//MARK: 追蹤模式 (Simulator:Features>location)
map.userTrackingMode = .follow
}
}
地圖三步曲
地圖初級應用
[iOS][Xcode][Swift]iPhone手機編程--在APP中加入地圖Map(MapKit)初級應用--靜態地點顯示
地圖升級應用
地圖升升級應用 (中高級應用某君可不懂,只能一路升級下去好了)
留言列表