下面的代碼只工作一次,現在突然間它沒有顯示興趣點(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