我有一个用Objective-C编写的简单应用程序,使用CLLocationManager
等获取当前位置。该代码在iPhone 6.1模拟器上工作正常,但它在我的实际iPod touch上不起作用。 iPod Touch 3rd Gen正在运行iOS5.1,它确实要求用户启用位置服务,但它不会更新。如果你问我可以发布代码,但我认为只会有一个明显的兼容性问题,这将是一个相当简单的修复。
任何帮助,将不胜感激!
谢谢!
〜Carpetfizz
ViewController.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
ViewController.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
答案 0 :(得分:2)
只需使用此代理
即可 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocation *location = newLocation;
NSLog(@"%f,%f",location.coordinate.latitude,location.coordinate.longitude);
}