【问题标题】:CLLocationManager returns location as 0.00 when app is installed for the first time首次安装应用时,CLLocationManager 将位置返回为 0.00
【发布时间】:2014-09-16 12:53:25
【问题描述】:

为了获取位置,我创建了 LocationManager.hLocationManager.m
LocationManager.h

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

@interface LocationManager : NSObject <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *clLocationMgr;
@property (strong, nonatomic) CLLocation *clLocation;
@property float latitude;
@property float longitude;

+ (LocationManager*)getSharedInstance;
- (void)startLocation;
- (float)currentLatitude;
- (float)currentLogitude;
- (NSString*)abbreviatedDistance:(int)_distance;
@end

LocationManager.m

#import "LocationManager.h"
#import <CoreLocation/CoreLocation.h>

@implementation LocationManager

static LocationManager *sharedInstance = nil;

+ (LocationManager *) getSharedInstance {
    if (!sharedInstance) {
        sharedInstance = [[super allocWithZone:NULL] init];
    }
    return sharedInstance;
}

- (CLLocationManager *)getLocationManager {
    if (_clLocationMgr == nil) {
        _clLocationMgr = [[CLLocationManager alloc] init];
    }
    [_clLocationMgr setDelegate: self];
    return _clLocationMgr;
}

- (void) startLocation {
    if (_clLocationMgr == nil) {
        _clLocationMgr = [self getLocationManager];
    }
    [_clLocationMgr setDistanceFilter: kCLDistanceFilterNone];
    [_clLocationMgr setDesiredAccuracy: kCLLocationAccuracyBest];

    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"location service not available");
    }

    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusRestricted || 
        status == kCLAuthorizationStatusDenied) {
        NSLog(@"location service is restriced or is denied");
    }

    [_clLocationMgr startUpdatingLocation];
    _clLocation = [_clLocationMgr location];
    _latitude = _clLocation.coordinate.latitude;
    _longitude = _clLocation.coordinate.longitude;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    _clLocation = [locations lastObject];
    _latitude = _clLocation.coordinate.latitude;
    _longitude = _clLocation.coordinate.longitude;
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"Fail to handle location: %@", error);

    if (![CLLocationManager locationServicesEnabled]) {
        NSLog(@"location service not available");
    }

    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    if (status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied) {
        NSLog(@"location service is restriced or is denied");
    }
}

- (float)currentLatitude {
    return _latitude;
}

- (float)currentLogitude {
    return _longitude;
}

- (NSString *)abbreviatedDistance:(int)_distance {
    if(_distance < 1000) {
        return [NSString stringWithFormat:@"%@m", [[NSNumber numberWithInt:_distance] stringValue]];
    } else {
        double distanceDouble = _distance / 1000;
        return [NSString stringWithFormat:@"%@km", [[NSNumber numberWithDouble:distanceDouble] stringValue]];
    }
}

@end

然后MainViewController.m 呼叫位置经理。

- (void)viewDidLoad {
    [super viewDidLoad];
    [[LocationManager getSharedInstance] startLocation];
}

当我第一次安装我的应用程序时,位置是 0.00000。
我不知道为什么位置会这样。
代码有问题吗?

【问题讨论】:

    标签: ios cllocationmanager


    【解决方案1】:

    我记得我在某处读到过,您将立即获得最后一个已知位置,然后更新新位置。

    本文Getting the User’s Current Location | Receiving Location Data from a Service苹果建议检查接收数据的年龄:

    // Delegate method from the CLLocationManagerDelegate protocol.
    
    - (void)locationManager:(CLLocationManager *)manager
    
          didUpdateLocations:(NSArray *)locations {
    
        // If it's a relatively recent event, turn off updates to save power.
    
       CLLocation* location = [locations lastObject];
    
       NSDate* eventDate = location.timestamp;
    
       NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    
       if (abs(howRecent) < 15.0) {
    
          // If the event is recent, do something with it.
    
          NSLog(@"latitude %+.6f, longitude %+.6f\n",
    
                  location.coordinate.latitude,
    
                  location.coordinate.longitude);
    
       }
    
    }
    

    我认为这样可以过滤“无效”位置更新和“零更新”。

    【讨论】:

    • 在我触摸警报视图would you like to user...之前,它也返回0.000。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多