iOS模拟器不更新位置

时间:2015-11-10 15:32:29

标签: 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)")

}

---------------------- UPDATE 1 --------------------

通过模拟器更新位置

enter image description here

我注意到以下功能未运行:

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

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

应用程序工作一次然后停止。不确定发生了什么。是的我添加了NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription enter image description here

2 个答案:

答案 0 :(得分:2)

模拟器可以有一组硬编码位置,您可以在xcode控制台中更改或禁用它们。

它应该是这样的:

enter image description here

如果您想测试真实的移动位置,您应该在设备上进行测试。

答案 1 :(得分:2)

您的代码存在一些问题:

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

替换为:

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

另外(有关信息)

您可以为不同的位置创建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