【问题标题】:Swift request geolocation from non-view从非视图快速请求地理定位
【发布时间】:2017-03-26 20:54:32
【问题描述】:

我正在尝试在任意类中获取地理位置数据。我对 Swift 很陌生,所以我不知道为什么这不起作用?

有什么建议吗?

import Foundation
import UIKit
import CoreLocation

class GeolocationPlugin:NSObject, CLLocationManagerDelegate {
  var locationManager: CLLocationManager!
  var lat: Double = 0
  var long: Double = 0

  func getLocation() {
    print("Getting location")

    // For use in foreground
    self.locationManager = CLLocationManager()
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
//    locationManager.startMonitoringSignificantLocationChanges()


    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError) {
      print("Error while updating location " + error.localizedDescription)
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [CLLocation]) {
      let locValue:CLLocationCoordinate2D = manager.location!.coordinate
      print("locations = \(locValue.latitude) \(locValue.longitude)")
    }


    self.locationManager.requestLocation()

    print("gets here")
  }
}

我目前看到Getting location,然后出现错误:

2017-03-26 15:42:32.634 IonicRunner[42304:5668243] *** Assertion failure in -[CLLocationManager requestLocation], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreLocationFramework_Sim/CoreLocation-2100.0.34/Framework/CoreLocation/CLLocationManager.m:865
2017-03-26 15:42:32.638 IonicRunner[42304:5668243] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didUpdateLocations:'

【问题讨论】:

    标签: swift geolocation


    【解决方案1】:

    最终的解决方案是将方法移出getLocation(),以正确激活模拟器中的位置,并移动该类的启动位置,因此不会在getLocation() 立即发布完成。

    import Foundation
    import UIKit
    import CoreLocation
    
    class GeolocationPlugin:NSObject, CLLocationManagerDelegate {
      var locationManager = CLLocationManager()
      var lat: Double = 0
      var long: Double = 0
      var cb: ((Double, Double) -> Void)? = nil
    
      func getLocation(callback: @escaping (Double, Double) -> Void) {
        print("Getting location")
    
        // For use in foreground
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    
        self.locationManager.requestLocation()
        self.cb = callback
      }
    
      func locationManager(_ manager: CLLocationManager, didFailWithError error: NSError) {
        print("Error while updating location " + error.localizedDescription)
      }
    
      func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
        //print("locations = \(locValue.latitude) \(locValue.longitude)")
    
        if( self.cb != nil) {
          self.cb!(locValue.latitude, locValue.longitude)
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-02-06
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多