0

我有一个用 Objective-C 编写的简单应用程序,它使用CLLocationManager等获取我的当前位置。该代码在 iPhone 6.1 Simulator 上运行良好,但在我实际的 iPod touch 上却无法运行。iPod Touch 3rd Gen 运行的是 iOS5.1,它确实要求用户启用定位服务,但它没有得到更新。如果您问,我可以发布代码,但我认为只会有一个明显的兼容性问题,这将是一个相当简单的修复。任何帮助,将不胜感激!谢谢!~地毯嘶嘶声

视图控制器.h

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

@interface ViewController : UIViewController <CLLocationManagerDelegate>
{
    CLLocationManager* locationManager;
}

@property (weak, nonatomic) IBOutlet UILabel *labelLong;
@property (weak, nonatomic) IBOutlet UILabel *labelLat;
-(IBAction)updateLocaitonButtonAction:(id)sender;
@end

视图控制器.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize labelLong,labelLat;

- (void)viewDidLoad
{
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc]init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations lastObject];
    NSLog(@"%f,%f",location.coordinate.latitude,location.coordinate.longitude);
    self.labelLong.text = [NSString stringWithFormat:@"%f",location.coordinate.longitude];
    self.labelLat.text  = [NSString stringWithFormat:@"%f",location.coordinate.latitude];
    [locationManager stopUpdatingLocation];
}

-(IBAction)updateLocaitonButtonAction:(id)sender
{
    [locationManager startUpdatingLocation];
}


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

@end
4

1 回答 1

2

只需使用此委托

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
      CLLocation *location = newLocation;
      NSLog(@"%f,%f",location.coordinate.latitude,location.coordinate.longitude);
}
于 2013-06-30T09:18:54.347 回答