【问题标题】:Location is shown 0.0 in iPad and iPod位置在 iPad 和 iPod 中显示为 0.0
【发布时间】:2015-10-26 17:19:40
【问题描述】:

您好,在 iPad、iPod 以及 iPhone 上获取位置的最佳方式是什么。

我已集成以下内容,但仍无法获得正确的当前位置。 在 iPod 上,我只能第一次获得位置:

+ (GPS*)get 
{
    if (!g_) 
    {
        NSLog(@"Creating instance of GPS!");
        g_ = [GPS new];
    }
    return g_;
}

@synthesize manager;

- (id)init
{
    if (!(self = [super init]))
        return nil;

    //Setup the manager
    manager = [[CLLocationManager alloc] init];
    if (!manager) 
    {
        return nil;
    }
    manager.distanceFilter = kCLDistanceFilterNone;
    manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    manager.desiredAccuracy = kCLLocationAccuracyBest;
    manager.delegate = self;

    if ([manager respondsToSelector:@selector(pausesLocationUpdatesAutomatically)]) 
    {
        manager.pausesLocationUpdatesAutomatically = NO;
    }
    if ([manager respondsToSelector:@selector(requestAlwaysAuthorization)])
    {
        [manager requestAlwaysAuthorization];
    }

    [manager startUpdatingLocation];

    return self;
}

- (void)start
{
    NSLog(@"Start Tracking");

    [manager startUpdatingLocation];
 }

-(void)stop
{
    [manager stopUpdatingLocation];
}

#pragma mark CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *currentLocation = [locations lastObject];
    NSLog(@"%f : %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
    //[self writeToFile];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"locationManager - didFailWithError: %@", [error localizedDescription]);
    NSLog(@"domain:%@", [error domain]);
    //NSLog(@"code:%i", [error code]);
    //[self writeToFile];
}

【问题讨论】:

  • 我的设备使用的是 8.4

标签: ios objective-c iphone core-location ipod-touch


【解决方案1】:

iOS8 上存在位置授权问题。根据您最近使用 iOS 8.4 的评论,这可能是您的问题。所以在startUpdatingLocation之前试试下面的代码:

// This part was added due to a location authorization issue on iOS8
// See more at: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
if ([manager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [manager requestWhenInUseAuthorization];
}
self.mapView.showsUserLocation = YES; //?

在此之前,请确保通过添加以下一个或两个键来正确设置 Info.plist:

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

【讨论】:

    猜你喜欢
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多