第一次安装应用程序时,CLLocationManager将位置返回为0.00

时间:2014-09-16 12:53:26

标签: ios cllocationmanager

为了获取位置,我制作了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 我不知道为什么地点就是这样。
代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

我记得我在某个地方读过你会得到最后一个已知地点,然后更新新地点。

在本文Getting the User’s Current Location | Receiving Location Data from a Service中,Apple建议检查收到的数据的年龄:

// 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);

   }

}

我认为这样你可以过滤&#34;无效&#34;位置更新和&#34;零更新&#34;。