2013-01-24 74 views
0

我基本上是从这个CoreLocation iOS教程开始学习如何将CoreLocation实现到我的应用程序中。 (http://www.techotopia.com/index.php/An_Example_iOS_5_iPhone_Location_Applicationcorelocation保持循环

但是我试图将本教程加入到我的应用程序时遇到的问题是,它现在只是一直在循环,这只是让我感到困惑。任何人都可以帮忙吗?

GPSViewController.h

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 

@interface GPSViewController : UIViewController 
@property (strong, nonatomic) CLLocationManager *locationManager; 
@property (strong, nonatomic) CLLocation *startLocation; 
@end 

GPSViewController.m

#import "GPSViewController.h" 

#import "DataClass.h" 

@interface GPSViewController() 

@end 

@implementation GPSViewController 
@synthesize locationManager, startLocation; 

DataClass *obj; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    //initialization og global varable. 
    DataClass *obj=[DataClass getInstance]; 

    //GPS Initialise 
    self.locationManager = [[CLLocationManager alloc] init]; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.delegate = self; 
    [locationManager startUpdatingLocation]; 
    startLocation = nil; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - 
#pragma mark CLLocationManagerDelegate 

-(void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{ 
    NSString *currentLatitude = [[NSString alloc] 
           initWithFormat:@"%g", 
           newLocation.coordinate.latitude]; 
    //latitude.text = currentLatitude; 
    obj.Latatude = currentLatitude; 

    NSString *currentLongitude = [[NSString alloc] 
            initWithFormat:@"%g", 
            newLocation.coordinate.longitude]; 
    //longitude.text = currentLongitude; 
    obj.Longitude = currentLongitude; 

    NSLog(@"latitude %+.6f, longitude %+.6f\n", 
      newLocation.coordinate.latitude, 
      newLocation.coordinate.longitude); 

    if(obj.Latatude != NULL && obj.Longitude != NULL){ 
     [self performSegueWithIdentifier:@"GPSSuccess" sender:self]; 
    } 
} 

-(void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error 
{ 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
    self.startLocation = nil; 
    self.locationManager = nil; 
} 


@end 
+0

通过循环你的意思是它一次又一次地调用didUpdateToLocation? –

回答

3

你应该叫[locationManager stopUpdatingLocation];为了从遍地获取用户的位置停止。

+0

啊是的,我做了这个,但它停止程序获取GPS位置之前停止,但我只是添加[locationManager stopUpdatingLocation];上面一行[self performSegueWithIdentifier:@“GPSSuccess”sender:self];它仍然允许我在没有循环问题的情况下获得所需的细节。非常感谢你。 – Beanonymous