iOS模拟器不更新位置

Dre*_*208 3 ios swift2

下面的代码只工作了一次,现在突然没有显示兴趣点(POI)。我在两台不同的机器上尝试过,但得到了相同的行为。这就是让我困惑的地方,是代码还是我的模拟器设置。

我清理了项目,确保我有自定义的模拟器。我确实现在突然打印了经度和纬度,它也没有在控制台中打印。

导入 UIKit 导入 MapKit 导入 CoreLocation

类 MapViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet var map: MKMapView!

var manager:CLLocationManager!
var latitude:Double = 0.0
var longitude:Double = 0.0
var location:Double = 0.0

override func viewDidLoad() {
    super.viewDidLoad()
    print("init")
    manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy - kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    print("test test test")

    let userLocation:CLLocation = locations[0]
    self.latitude = userLocation.coordinate.latitude
    self.longitude = userLocation.coordinate.longitude

    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = "Army Recruiting"

    request.region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 1600, 1600)

    MKLocalSearch(request: request).startWithCompletionHandler { (response, error) in
        guard error == nil else { return }
        guard let response = response else { return }
        guard response.mapItems.count > 0 else { return }

        let randomIndex = Int(arc4random_uniform(UInt32(response.mapItems.count)))
        let mapItem = response.mapItems[randomIndex]

        mapItem.openInMapsWithLaunchOptions(nil)
    }

    print("\(self.longitude) \(self.latitude)")

}
Run Code Online (Sandbox Code Playgroud)

--------------更新 1------------

通过模拟器更新位置

在此处输入图片说明

我注意到以下功能没有运行:

func locationManager(manager: CLLocationManager, didUpdateLocations 
locations: [CLLocation]) 
Run Code Online (Sandbox Code Playgroud)

---------------------更新2-----

该应用程序运行一次然后停止。不知道发生了什么。是的,我添加了 NSLocationWhenInUseUsageDescription 和 NSLocationAlwaysUsageDescription 在此处输入图片说明

Ric*_*ppz 5

您的代码有几个问题:

override func viewDidLoad() {
    super.viewDidLoad()
    print("init")
    manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy - kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
}
Run Code Online (Sandbox Code Playgroud)

用。。。来代替:

override func viewDidLoad() {
    super.viewDidLoad()
    print("init")
    manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()
}
Run Code Online (Sandbox Code Playgroud)

另外(信息)

您可以为不同的位置创建 GPX 文件。

您还可以在 GPX 文件中模拟路线 - 深入研究 Apple 文档,该文档向您展示了如何将 GPX 文件设置为您的目标版本。https://developer.apple.com/library/ios/documentation/IDEs/Conceptual/iOS_Simulator_Guide/CustomizingYourExperienceThroughXcodeSchemes/CustomizingYourExperienceThroughXcodeSchemes.html

这里还有一个很好的教程,其中包含我过去使用过的用于创建 GPX 文件和路由的链接,非常有用。https://blackpixel.com/writing/2013/05/simulating-locations-with-xcode.html