下面的代码只工作一次,现在突然间它没有显示兴趣点(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个--------------------
更新位置
我已经注意到了以下功能不运行:
func locationManager(manager: CLLocationManager, didUpdateLocations
locations: [CLLocation])
---------------------更新2 ------ --------------
该应用程序工作一次后停止。不知道发生了什么事。是的,我加入NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription
locationManager func未执行。我通过将print()语句放在函数中来确认。我不确定它为什么没有运行。 – Drew1208
应用程序是否使用位置服务? – RichAppz
只询问,因为它听起来像你没有在你的.plist文件中设置NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription(你的情况)。 转到您的目标>信息并添加一行与NSLocationWhenInUseUsageDescription和字符串将是您希望使用接受位置服务的应用 – RichAppz