我需要獲取當前的用戶位置,但我不知道它是否已更改。我可以從CLLocationManager請求強制位置更新嗎?或者還有其他方法可以做到嗎?如何強制更新用戶位置?
3
A
回答
13
停止並重新啓動LocationManager應強制設備重新獲取初始位置。
[locationManager stopUpdatingLocation];
[locationmanager startUpdatingLocation];
3
我在我的一個應用程序中遇到了同樣的問題。我實際上不得不改變應用程序結構。 這就是我所做的: 這個班有一個公開的方法。 - (無效)locateMe;一個抽象類需要實例化這個類並運行locateMe,然後當userIsLocated通知將被廣播時。另一種方法可以從(CLLocation *)currentLocation獲得結果座標;
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface ManageUserLocation : NSOperation <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
CLLocation *currentLocation;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@property (nonatomic, retain) CLLocation *currentLocation;
-(void) locateMe;
@end
在.M
#import "ManageUserLocation.h"
@implementation ManageUserLocation
@synthesize locationManager;
@synthesize currentLocation; // Other classes use this to get the coordination even better you can make another method that even dont get the direct access to currentLocation. It is up to you.
- (id)init
{
self = [super init];
if (self) {
//[self locateMe]; // Just a hook if you need to run it
}
return self;
}
-(void) locateMe {
self.locationManager = nil;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// User location has been found/updated, load map data now.
[self.locationManager stopUpdatingLocation];
currentLocation = [newLocation copy];
// WooHoo Tell everyone that you found the userLocation
[[NSNotificationCenter defaultCenter] postNotificationName:@"userLocationIsFound" object:nil];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
// Failed to find the user's location. This error occurs when the user declines the location request or has location servives turned off.
NSString * errorString = @"Unable to determine your current location.";
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error Locating" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
[errorAlert release];
[locationManager stopUpdatingLocation];
}
- (void)dealloc { #warning dont forget this :) }
@end
希望這有助於
。
0
我認爲是違反蘋果指南強制更新。蘋果公司表示,地理更新將自動發生,但在未知的時間。
相關問題
- 1. 如何連續更新用戶位置
- 2. 強制用戶更新應用程序
- 3. 如何強制UIView更新
- 4. 如何強制Openssl更新?
- 5. 如何強制INFORMATION_SCHEMA.COLUMNS更新
- 6. 如何強制用戶刷新緩存
- 7. CLLocationManager - 在設備中強制更改用戶的當前位置
- 8. 如何強制用戶更新應用程序?
- 9. 如何在單擊刷新菜單按鈕後強制更新位置?
- 10. 我如何強制Joomla更新用戶會話
- 11. 如何強制ControlSource更新(Excel用戶表單)
- 12. Rails4 - 如何強制用戶更新他/她的配置文件中的字段?
- 13. 在用戶配置文件更新期間設計強制更新密碼
- 14. 強制提供新的Google Fused位置
- 15. 如何強制網絡位置?
- 16. 更新iOS SDK:它是否強制所有用戶更新?
- 17. 強制更新到多個客戶端
- 18. 更新節點'D3強制佈局中的子元素位置
- 19. D3強制導向圖:更新節點位置
- 20. CLLocationManager顯着位置更改強制刷新
- 21. 如何強制地理位置重新計算?
- 22. 如何判斷新位置是否強制文件下載?
- 23. 如何強制Android重新讀取GPS位置?
- 24. Android:getLastKnownLocation過時 - 如何強制位置刷新?
- 25. XML - 強制更新
- 26. 強制UIScrollView更新
- 27. 強制UI更新
- 28. 如何強制更新MVVM中的UI?
- 29. 如何強制傳單更新地圖?
- 30. 如何強制更新的順序DB