2017-03-26 4 views
1

임의의 클래스에서 Geolocation 데이터를 가져 오려고합니다. 나는 Swift에 매우 익숙하다. 그래서 나는 이것이 왜 작동하지 않는지도 모른다?보기가 아닌 곳에서 신속한 Geolocation 요청

모든 포인터? 적절 시뮬레이터 위치를 활성화 한 곳을 이동하는 상기 액 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

: I은 다음 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) 
    } 
    } 
} 
관련 문제