目前,開發一個需要從CLLocationManager獲取最後位置的應用程序(沒有任何定期跟蹤)。不管多大,準確是多少。我不需要也不想開始跟蹤 - 我只需要從緩存中獲取最後一個位置,就是這樣。恕我直言,CLLocationManager是iOS中的共享組件,如果某些應用程序使用位置跟蹤,則另一個應用程序應該能夠使用CLLocationManager.location中最新的位置。只需分配/初始化CLLocationManager並獲取其位置即可。但事實並非如此。我在iPhone4上測試過 - 啓動谷歌地圖,看到我當前的位置,然後去我的應用程序,但[[CLLocationManager alloc] init]位置屬性爲零後。應用程序是否應該啓動位置跟蹤,以便從CLLocationManager獲取任何最後一個已知位置?
更新:嘗試[locationManager startUpdatingLocation];和[locationManager stopUpdatingLocation];但結果是一樣的。我想,唯一的解決辦法是開始定期跟蹤?
UPDATE2:奇怪,但在CLLocationManager的alloc/init之後沒有提示「應用程序想使用位置服務」。這裏是我的代碼片段:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
[locationManager stopUpdatingLocation];
NSLog(@"%@", locationManager.location); //prints nil
我建議你看看[如何找到您的當前位置與CoreLocation(http://stackoverflow.com/questions/6516967/how-to-find- your-current-location-with-corelocation),並將找到的位置存儲在「NSArray」中。 –