【问题标题】:iOS - CLLocation Manager didUpdateLocations being called in one class but not another?iOS - CLLocation Manager didUpdateLocations 在一个类中被调用,而不是在另一个类中?
【发布时间】:2018-02-13 18:26:12
【问题描述】:

您好,我正在制作一个程序来获取用户位置并在地图上放置相应的注释。我首先在 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() 确实被调用。

下面是我从视图控制器调用的新 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()`

在我的视图控制器中

【问题讨论】:

  • 您的SendLocation 实例可能正在被释放,并且它的位置管理器正在处理它。无论创建什么类,您的对象都需要在属性中保存对它的引用,就像您在属性中保存位置管理器一样。
  • 就是这样,谢谢!我自己永远不会得到那个。
  • 如果问题很容易解决,最好删除问题。
  • 自己写答案,最好不要关闭...

标签: ios swift mapkit core-location foundation


【解决方案1】:

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

将 SendLocation 设为 UIViewController 的属性。

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

不会工作:

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

会工作

let location = SendLocation()

func sendLocation() {
    location.sendLocationPost()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    • 2015-12-19
    • 1970-01-01
    • 2011-09-13
    • 1970-01-01
    相关资源
    最近更新 更多