简体   繁体   English

iOS5.1中的位置服务无法正常工作

[英]Location services in iOS5.1 not working as it should

I have a simply app written in Objective-C that gets my current position using CLLocationManager , etc. The code works fine on the iPhone 6.1 Simulator, but it doesn't work on my actual iPod touch. 我有一个用Objective-C编写的简单应用程序,它使用CLLocationManager等获取我当前的位置。该代码在iPhone 6.1模拟器上工作正常,但它在我的实际iPod touch上不起作用。 The iPod Touch 3rd Gen is running iOS5.1, and it does ask for the user to enable location services, however it does not get updated. iPod Touch 3rd Gen正在运行iOS5.1,它确实要求用户启用位置服务,但它不会更新。 I can post code if you ask, but I thought that there would just be a glaring compatibility issue that would be a rather simple fix. 如果你问我可以发布代码,但我认为只会有一个明显的兼容性问题,这将是一个相当简单的修复。 Any help would be appreciated! 任何帮助,将不胜感激! Thanks! 谢谢! ~Carpetfizz 〜Carpetfizz

ViewController.h 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 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

Simply use this delegate 只需使用此代表

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
      CLLocation *location = newLocation;
      NSLog(@"%f,%f",location.coordinate.latitude,location.coordinate.longitude);
}

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

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