[英]CLLocationManager returns location as 0.00 when app is installed for the first time
为了获得位置,我制作了LocationManager.h
和LocationManager.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。
我不知道为什么这样的位置。
代码有问题吗?
我记得在某处读到,您将立即获得最后一个已知位置,然后更新新位置。
本文中的获取用户的当前位置| 从服务 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);
}
}
我认为这样可以过滤“无效”位置更新和“零更新”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.