繁体   English   中英

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

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

我有一个用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

只需使用此代表

 - (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