位置管理器无法在模拟器中工作但是在Iphone中正常运行

时间:2016-10-20 09:16:56

标签: swift ios-simulator core-location cllocationmanager

应用程序在iPhone中运行,但是当我尝试在模拟器中运行时,它会显示图像中给出的错误

请帮助我&先谢谢你了

//这是我的代码:

    locationManager.delegate = self
    var locManager = CLLocationManager()
    locManager.requestWhenInUseAuthorization()


    if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
        CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized)
    {
        let latitude1 = locManager.location!.coordinate.latitude.description
        latitude = latitude1

        let longitude1 = locManager.location!.coordinate.longitude.description
        longnitude = longitude1
        print(latitude)
        print(longnitude)
    } else {
        latitude = ""
        longnitude = ""

    }

enter image description here

2 个答案:

答案 0 :(得分:1)

试试这段代码:在Xcode模拟器(Swift 3)中测试

更新您的plist:

隐私 - 使用时的位置使用说明==某些值

 import UIKit
 import MapKit
 import CoreLocation

  class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

  @IBOutlet weak var mapView: MKMapView!

  let locationManager = CLLocationManager()

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    self.mapView.showsUserLocation = true

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    let location = locations.last

    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2))

    self.mapView.setRegion(region, animated: true)

    self.locationManager.stopUpdatingLocation()

    print(location)
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error)
{
    print("Errors: " + error.localizedDescription)
}
}

代码输出:

enter image description here

答案 1 :(得分:0)

您必须将框架导入为 import CoreLocation