[英]startUpdatingLocation not being fired in iOS
我正在嘗試編寫一個簡單的地理定位應用程序來查找用戶在iOS中的位置。 當用戶按下按鈕時,會找到並顯示用戶的位置,並且在觸發startUpdatingLocation時應該更改。 但是沒有觸發startUpdatingLocation。 這是我的代碼:
在我的ViewController.h中
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface CGViewController : UIViewController<CLLocationManagerDelegate>
@property (strong, nonatomic) IBOutlet UITextField *nameField;
- (IBAction)butStart:(id)sender;
@property (strong, nonatomic) IBOutlet MKMapView *map;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) IBOutlet UILabel *latitude;
@property (strong, nonatomic) IBOutlet UILabel *longitude;
@property (strong, nonatomic) IBOutlet UILabel *altitude;
@end
在我的ViewController.m中:
- (IBAction)butStart:(id)sender {
self.locationManager=[[CLLocationManager alloc] init];
if([CLLocationManager locationServicesEnabled]){
self.locationManager.delegate=self;
self.locationManager.distanceFilter=1;
[self.locationManager startUpdatingLocation];
}
}
(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog( @"new location = %@", [newLocation description] );
MKCoordinateSpan span;
span.latitudeDelta = 0.2;
span.longitudeDelta = 0.2;
MKCoordinateRegion region;
region.span = span;
region.center = newLocation.coordinate;
map.showsUserLocation = YES;
self.latitude.text = [NSString stringWithFormat:@"%.3f", newLocation.coordinate.latitude];
self.longitude.text = [NSString stringWithFormat:@"%.3f", newLocation.coordinate.longitude];
}
我試圖省去不必要的代碼,就像我擁有的地圖一樣。 我見過的每個樣本都有appManager的東西在AppDelegate.h / .m中設置,而我試圖在用戶按下按鈕時調用它。 我不確定為什么不被召喚。
您使用的方法在SDK 6中已棄用。您應該使用以下方法:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations;
如果您使用iOS 6.0及更高版本,則可能無法調用您編寫的方法。
我還在我的項目中實現了你的代碼,它運行正常。
- 如果你第一次在模擬器中運行你的項目,那么位置服務允許和禁止消息來,如果你允許那么這個方法被調用請用這個替換yoyr方法
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
float currentLangitude = newLocation.coordinate.longitude;
float currentLatitude = newLocation.coordinate.latitude;
NSLog( @"new location lat = %f long = %f", currentLangitude,currentLatitude );
}
謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.