2015-11-10 120 views
3

下面的代码只工作一次,现在突然间它没有显示兴趣点(POI)。我在两台不同的机器上尝试过它,并获得相同的行为。这是我感到困惑的,是代码还是我的模拟器设置。iOS模拟器没有更新位置

我清理了这个项目,确定我有自定义的模拟器。我现在确实有经纬度打印,现在它突然不会在控制台上打印。

进口的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

夫妇与你的代码问题:

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

+0

locationManager func未执行。我通过将print()语句放在函数中来确认。我不确定它为什么没有运行。 – Drew1208

+0

应用程序是否使用位置服务? – RichAppz

+0

只询问,因为它听起来像你没有在你的.plist文件中设置NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription(你的情况)。 转到您的目标>信息并添加一行与NSLocationWhenInUseUsageDescription和字符串将是您希望使用接受位置服务的应用 – RichAppz

2

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

它应该是这样的:

enter image description here

如果你想测试真正的移动位置,你应该测试它的设备上。

+0

请参阅更新... – Drew1208