iT邦幫忙

0

ios 地圖定位問題 無法取得 didUpdateLocations

  • 分享至 

  • xImage
import UIKit
import MapKit
import CoreLocation
import FirebaseAuth

class riderViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

 @IBOutlet weak var callBtn: UIButton!
 @IBOutlet weak var mapView: MKMapView!
 var iscall = true
 @IBAction func backToHome(_ sender: UIBarButtonItem) {
     do{
         try  Auth.auth().signOut()
         navigationController?.dismiss(animated: true, completion: nil)
         
     }catch{
         print("not success")
     }
    
 }
 @IBAction func callToUber(_ sender: UIButton) {
    
     if iscall {
         calltoUber()
     }else{
         cancelCallUber()
     }
     
 }
 
 func calltoUber(){
     callBtn.backgroundColor = UIColor(red: 255, green: 0, blue: 0, alpha: 1)
     callBtn.setTitle("cancel to call Uber", for: .normal)
     callBtn.setTitleColor(UIColor(red: 255, green: 255, blue: 255, alpha: 1), for: .normal)
     iscall = false
     
 }
 
 func cancelCallUber(){
     callBtn.backgroundColor = UIColor.orange
     callBtn.setTitle("Call To Uber", for: .normal)
     callBtn.setTitleColor(UIColor(red: 255, green: 255, blue: 255, alpha: 1), for: .normal)
     iscall = true
 }
 
 var myLocationManager :CLLocationManager = CLLocationManager()
 
 override func viewDidLoad() {
     super.viewDidLoad()
     
     
     
             // Do any additional setup after loading the view.
 }
 
 override func viewDidAppear(_ animated: Bool) {
     myLocationManager.desiredAccuracy =
       kCLLocationAccuracyBest
     myLocationManager.delegate = self
     
     
     
     if CLLocationManager.authorizationStatus()
         == .notDetermined {
             // 取得定位服務授權
         myLocationManager.requestWhenInUseAuthorization()

             // 開始定位自身位置
             myLocationManager.startUpdatingLocation()
        
         }
         // 使用者已經拒絕定位自身位置權限
         else if CLLocationManager.authorizationStatus()
                     == .denied {
             // 提示可至[設定]中開啟權限
             let alertController = UIAlertController(
               title: "定位權限已關閉",
               message:
               "如要變更權限,請至 設定 > 隱私權 > 定位服務 開啟",
                 preferredStyle: .alert)
             let okAction = UIAlertAction(
                 title: "確認", style: .default, handler:nil)
             alertController.addAction(okAction)
             self.present(
               alertController,
               animated: true, completion: nil)
         }
         // 使用者已經同意定位自身位置權限
         else if CLLocationManager.authorizationStatus()
                     == .authorizedWhenInUse {
             // 開始定位自身位置
            print("a")
             myLocationManager.startUpdatingLocation()
             
             DispatchQueue.main.async {
                 self.myLocationManager.startMonitoringSignificantLocationChanges()
                
             }
         }
   

     let latitude:CLLocationDegrees = 22.611875508972055
     let longtitude:CLLocationDegrees = 120.30028684557932
     
     let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longtitude)
     let xscale:CLLocationDegrees = 0.018
     let yscale:CLLocationDegrees = 0.018
     
     let span = MKCoordinateSpan(latitudeDelta: xscale, longitudeDelta: yscale)
     let region = MKCoordinateRegion(center: location, span: span)
     
//        加入大頭針
     let annotation = MKPointAnnotation()
     annotation.coordinate = location
     annotation.title = "my location"
     mapView.addAnnotation(annotation)
     mapView.setRegion(region, animated: true)
 

 }
 
 
 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
     print(locations)

     print("aa")
     
     if let userLocation:CLLocationCoordinate2D = manager.location?.coordinate{
         let region = MKCoordinateRegion(center: userLocation, span: MKCoordinateSpan(latitudeDelta: 0.018, longitudeDelta: 0.018))
         mapView.setRegion(region, animated: true)
         let annotation = MKPointAnnotation()
         annotation.coordinate = userLocation
         annotation.title = "my location"
         mapView.addAnnotation(annotation)
         mapView.setRegion(region, animated: true)
     }

 }
 

}

也有設定info.plist增加
Privacy - Location When In Use Usage Description

目前問題為在跑viewDidAppear時驗證使用者允許後進不到didUpdateLocations的func而無法取得當前回傳的經緯度,我快瘋掉了
請各位板上的高手救救我!!!

測試拒絕權限後。狀態是restricted 你沒有處理(status == .restricted || status == .denied)
可以試試把app刪掉重裝,接受權限要求
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
YC
iT邦好手 1 級 ‧ 2021-05-24 18:01:14

我不知道你遇到什麼問題,不過

  1. authorizationStatus有5種case,你只處理了3種
  2. 你沒實作func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
0
tony781124
iT邦新手 5 級 ‧ 2021-08-03 11:13:27

測試拒絕權限後。狀態是restricted 你沒有處理(status == .restricted || status == .denied)
可以試試把app刪掉重裝,接受權限要求

我要發表回答

立即登入回答