2017-03-26 4 views
1

私は任意のクラスでジオロケーションデータを取得しようとしています。私はSwiftにはとても新しいので、なぜこれがうまくいかないのか分かりません。ノンビューからの迅速なジオロケーション

任意のポインタ?適切シミュレータ内の場所を活性化するために、どこでこれを移動させるために、溶液はgetLocation()からメソッドを移動させること巻き上げ

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:' 

答えて

0

:私は現在、次にGetting locationとエラーが表示さ

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") 
    } 
} 

クラスが開始されたので、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) 
    } 
    } 
}