简体   繁体   English

在iOS中没有触发startUpdatingLocation

[英]startUpdatingLocation not being fired in iOS

I'm trying to write a simple geolocation app that find the users position in iOS. 我正在尝试编写一个简单的地理定位应用程序来查找用户在iOS中的位置。 When the user presses a button, the user's position is found and displayed, and should be changing when startUpdatingLocation is fired. 当用户按下按钮时,会找到并显示用户的位置,并且在触发startUpdatingLocation时应该更改。 But startUpdatingLocation isn't being fired. 但是没有触发startUpdatingLocation。 Here's my code: 这是我的代码:

In my ViewController.h 在我的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

And in my ViewController.m: 在我的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];
     }    

I've tried to leave out unnecessary code, like a map that I have. 我试图省去不必要的代码,就像我拥有的​​地图一样。 Every sample I've seen has the locationManager stuff being set in the AppDelegate.h/.m, whereas I'm trying to call it when the user presses a button. 我见过的每个样本都有appManager的东西在AppDelegate.h / .m中设置,而我试图在用户按下按钮时调用它。 I'm not sure why its not being called. 我不确定为什么不被召唤。

The method which you have used is deprecated in SDK 6. You should use below method instead: 您使用的方法在SDK 6中已弃用。您应该使用以下方法:

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations;

There are chances that if you are using iOS 6.0 and above the method which you have written may not get called. 如果您使用iOS 6.0及更高版本,则可能无法调用您编写的方法。

I have also implement your code in my project and it is working fine . 我还在我的项目中实现了你的代码,它运行正常。

-If you run your project in simulator first time then location service allow and disallow message comes and if you allow then this method get called please replace yoyr method with this - 如果你第一次在模拟器中运行你的项目,那么位置服务允许和禁止消息来,如果你允许那么这个方法被调用请用这个替换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 );

} }

thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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