iOS - CLLocation Manager didUpdateLocations 在一个类中被调用但在另一个类中没有被调用?

标签 ios swift mapkit core-location foundation

您好,我正在制作一个获取用户位置并在 map 上添加相应注释的程序。我开始在 View Controller 中编写所有代码,它完美地获取了位置。下面是 View Controller 中的工作代码。

class MapViewController: UIViewController, CLLocationManagerDelegate {

    var annotation = MKPointAnnotation()
    var userLocation: CLLocation?

    @IBOutlet weak var mapView: MKMapView!

    var locationManager:CLLocationManager!


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        determineCurrentLocation()

    }

    func determineCurrentLocation() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.startUpdatingLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
       userLocation = locations[0] as CLLocation

        print("user latitude = \(userLocation?.coordinate.latitude)")
        print("user longitude = \(userLocation?.coordinate.longitude)")
        annotation.coordinate = CLLocationCoordinate2D(latitude: (userLocation?.coordinate.latitude)!, longitude: (userLocation?.coordinate.longitude)!)
        annotation.title = "You"
        mapView.addAnnotation(annotation)


    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Error \(error)")
    }

但是现在,当我尝试在另一个 swift 文件中重新创建几乎完全相同的代码时。 didUpdateLocations 永远不会被调用。 locationManager.startUpdatingLocation() 确实被调用了。

下面是我从 View Controller 调用的新 swift 文件。我在这里缺少任何简单的概念吗,因为我真的不明白为什么这不起作用。

import Foundation
import CoreLocation


class SendLocation:  NSObject, CLLocationManagerDelegate {

    var userLocation: CLLocation?
    var locationManager:CLLocationManager!

    func sendLocationPost() {

        determineCurrentLocation()
    }

    func determineCurrentLocation() {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        print("WHY")
        if CLLocationManager.locationServicesEnabled(){
            locationManager.startUpdatingLocation()
        }
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        userLocation = locations[0] as CLLocation

        print("user latitude = \(userLocation?.coordinate.latitude)")
        print("user longitude = \(userLocation?.coordinate.longitude)")
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Error \(error)")
    }
}

我用 :

     let location = SendLocation()

    location.sendLocationPost()`

在我的 View Controller 中

最佳答案

发生这种情况是因为您没有保留对 SendLocation 对象的引用。

使 SendLocation 成为 UIViewController 的属性。

例如,从静态范围调用它不会保留引用。

不会工作:

static func sendLocation() {
    let location = SendLocation()
    location.sendLocationPost()
}

会工作

let location = SendLocation()

func sendLocation() {
    location.sendLocationPost()
}

关于iOS - CLLocation Manager didUpdateLocations 在一个类中被调用但在另一个类中没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48773287/

相关文章:

ios - 通过 iOS 中的监听器检查 Firebase 当前登录用户

ios - Swift:通过 NSNotificationCenter 的键盘观察器不起作用

ios - 从在线 URL 播放视频有效,但从本地路径播放视频无效

objective-c - Objective-C - 如何将度分秒转换为 double

iphone - 在 iPhone 上渲染 KML 和图像叠加

ios - 无法使用 setPitchEnabled 将其值更改为 YES

iphone - 在 iOS 设备上截屏时如何修复丢失的 "blocks"数据?

ios - 快速设置另一个 View 的背景

objective-c - 生成 PDF 表格

ios - 如何使用 Multipeer 连接(Wifi 网络)计算两个 iOS 设备之间的距离?