繁体   English   中英

在iOS中没有触发startUpdatingLocation

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM