【问题标题】:Why my didUpdate userLocation method doesn't get called?为什么我的 didUpdate userLocation 方法没有被调用?
【发布时间】:2021-02-13 19:41:33
【问题描述】:

我的视图中有一个 MapKit,我想访问 userLocation 数据。但是,实际功能

func firstMapView(_ firstMapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation){
    print("it never gets called")
}

永远不会被调用。

整个代码如下所示:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{

    @IBOutlet weak var firstMapView: MKMapView!
    let locationManager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        
        firstMapView.delegate = self
    }
    
    
    func firstMapView(_ firstMapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation){
        print("it never gets called")
    }
    
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        if status == .authorizedWhenInUse {
            print("Authorized when in use detected")
            locationManager.startUpdatingLocation()
            firstMapView.showsUserLocation = true
        }
    }
}

当我运行代码时,我收到调试消息:print("Authorized when in use detected")

我看到this的问题,并试图复制东西,但没有帮助。

【问题讨论】:

  • 您没有使用实际的委托方法:mapView(_:didUpdate:)
  • 你能帮我正确的语法是什么吗?我看到了 Apple 文档,但不确定应该更改哪些部分,哪些不应该更改

标签: swift swiftui mapkit mkmapview core-location


【解决方案1】:

使用这个委托函数

func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
        print(userLocation)

    }

删除这个

func firstMapView(_ firstMapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation){
        print("it never gets called")
    }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-12-07
  • 2019-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-23
  • 1970-01-01
相关资源
最近更新 更多