iOS CoreLocation未获取GPS坐标
所以我是iOS开发新手,我只是想用我当前的GPS坐标更新一个标签。我的编译没有问题,但我的坐标显示为iOS CoreLocation未获取GPS坐标,ios,objective-c,core-location,Ios,Objective C,Core Location,所以我是iOS开发新手,我只是想用我当前的GPS坐标更新一个标签。我的编译没有问题,但我的坐标显示为0.00000,0.00000 以下是my.h文件的代码: #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface ViewController : UIViewController{ IBOu
0.00000,0.00000
以下是my.h文件的代码:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController{
IBOutlet CLLocationManager *locationManager;
IBOutlet UILabel *location;
}
//@property (nonatomic, retain) IBOutlet CLLocationManager *locationManager;
//@property (weak, nonatomic) IBOutlet UILabel *location;
@end
不要使用
locationManager.location.coordinate.latitude
,而是保留类型为CLLocationCoordinate2D
的实例变量。您可以将其称为类似于currentLocation
。然后,当您在委托方法locationManager:didUpdateLocations:
中获得一个值时,设置currentLocation
的值
您必须调用[locationManager startUpdatingLocation]
并设置其委托(以及实现该委托方法)
您目前使用位置管理器的方式是错误的,我认为您最好按照教程了解基本知识。修复了它:
我没有实现任何委托方法,也没有实现[locationManager startUpdatingLocation]。现在我更清楚了
.h文件:
@interface MapViewController : UIViewController <CLLocationManagerDelegate>
@property (nonatomic, retain) IBOutlet CLLocationManager *locationManager;
@property (strong, nonatomic) IBOutlet UILabel *location;
@end
谢谢你指出我有多傻。我明白了。谢谢大家 像这样尝试一次,在ViewDidLoad中:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = (id)self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
[self updateLabel];
使用此委托方法,否则将得到0个值:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//No need to do any code...
// NSLog(@"Got location %f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}
在更新标签方法中:
- (void) updateLabel
{
//Getting Current Latitude and longitude..
CLLocation *location = [locationManager location];
float longitude=location.coordinate.longitude;
float latitude=location.coordinate.latitude;
NSLog(@"latitude,longitudes are >> %f,%f",latitude,longitude);
locationlabel.text = [NSString stringWithFormat:@"%f,%f",longitude,latitude];
}
我发现将location用作singleton并将值保存到defaultuser中非常有趣。我是一名年轻的程序员,正在尝试用oop编写所有代码。我的用法如下(这段代码仍需要重构,并且alertUserWithTitle:是NYMessageToUser的一个类方法,用于提醒用户):
/##头文件:
@接口nLocationManager:NSObject
{
CLLocationManager*locationManager;
漂浮的孤独;
浮动纬度;
漂浮高度;
}
@财产(非原子,保留)CLLocationManager*locationManager;
@属性(非原子,读写)浮点经度;
@属性(非原子,读写)浮点纬度;
@属性(非原子,读写)浮点高度;
+(nLocationManager*)getInstance;
-(无效)启动日期地点;
-(void)停止更新位置;
-(double)GetDistanceFromUserLocation to Coordination Slatitude:(float)纬度经度:(float)lon;
@结束
//###实施文件:
@实现NYLocationManager
@综合定位经理;
@综合纬度;
@综合经度;
@综合海拔高度;
+(id)getInstance
{
静态NYLocationManager*实例=nil;
静态调度一次;
一次发送(一次发送)^{
实例=[[self alloc]init];
});
[实例startUpdatingLocation];
返回实例;
}
-(id)init
{
if(self=[super init])
{
纬度=0.0;
经度=0.0;
海拔=0.0;
如果([[NSUserDefaults standardUserDefaults]objectForKey:@“LocationLength”]!=nil)
{
NSUserDefaults*savedLocation=[NSUserDefaults standardUserDefaults];
纬度=[[savedLocation objectForKey:@“locationLatitude”]floatValue];
经度=[[savedLocation objectForKey:@“Location经度”]floatValue];
海拔高度=[[savedLocation objectForKey:@“locationAltitude”]floatValue];
}
locationManager=[[CLLocationManager alloc]init];
locationManager.distanceFilter=KCLDistanceFilterOne;
locationManager.desiredAccuracy=KCallocationAccuracyHundredMeters;
locationManager.delegate=self;
如果([CLLocationManager LocationServiceEnabled])
{
[locationManager startUpdatingLocation];
}否则
{
[NYMessageToUser alertUserWithTitle:@“位置服务已禁用!!!”withMessage:@“此应用旨在与位置共享图像,请为此应用启用位置并重新启用应用”];
}
}
回归自我;
}
-(无效)解除锁定
{
//永远不应该被称为,但只是在这里澄清真的。
}
-(无效)位置管理器:(CLLocationManager*)管理器更新位置:(NSArray*)位置
{
CLLocation*loc=[locations lastObject];
self.longitude=位置坐标经度;
自纬度=位置坐标纬度;
自身高度=定位高度;
NSUserDefaults*savedLocation=[NSUserDefaults standardUserDefaults];
[savedLocation setObject:[NSString stringWithFormat:@“%f”,self.longitude]forKey:@“locationLongitude”];
[savedLocation setObject:[NSString stringWithFormat:@“%f”,self.latitude]forKey:@“locationLatitude”];
[savedLocation setObject:[NSString stringWithFormat:@“%f”,self.altime]forKey:@“LocationAltiment”];
[保存位置同步];
[locationManager停止更新位置];
}
-(无效)locationManager:(CLLocationManager*)manager错误:(N错误*)错误{
[locationManager停止更新位置];
[NYMessageToUser alertUserWithTitle:@“位置错误!!!”withMessage:@“此应用旨在与有效位置一起使用,请为此应用启用位置并重新选中应用”];
}
-(无效)启动日期位置
{
如果([CLLocationManager LocationServiceEnabled])
{
[locationManager startUpdatingLocation];
}否则
{
[NYMessageToUser alertUserWithTitle:@“位置服务已禁用!!!”withMessage:@“此应用旨在与位置共享图像,请为此应用启用位置并重新启用应用”];
}
}
-(void)停止更新位置
{
[locationManager停止更新位置];
}
-(双精度)GetDistanceFromUserLocationToCoordinationSlatitude:(浮点)纬度经度:(浮点)lon
{
CLLocation*locA=[[CLLocation alloc]initWithLatitude:self.latitude经度:self.latitude];
CLLocation*locB=[[CLLocation alloc]initWithLatitude:lat经度:lon];
CLLocationDistance距离=[locA distanceFromLocation:locB];
返回距离;
}
@结束
//###如何使用
NYLocationManager*loc=[NYLocationManager getInstance];
NSLog(@“经度:%f,纬度:%f,海拔:%f”,位置经度,位置纬度,位置海拔);
在哪个点d
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//No need to do any code...
// NSLog(@"Got location %f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}
- (void) updateLabel
{
//Getting Current Latitude and longitude..
CLLocation *location = [locationManager location];
float longitude=location.coordinate.longitude;
float latitude=location.coordinate.latitude;
NSLog(@"latitude,longitudes are >> %f,%f",latitude,longitude);
locationlabel.text = [NSString stringWithFormat:@"%f,%f",longitude,latitude];
}
//##Header file:
@interface NYLocationManager : NSObject<CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
float lonngitude;
float latitude;
float altitude;
}
@property(nonatomic,retain)CLLocationManager *locationManager;
@property(nonatomic,readwrite)float longitude;
@property(nonatomic,readwrite)float latitude;
@property(nonatomic,readwrite)float altitude;
+(NYLocationManager *) getInstance;
-(void)startUpdatingLocation;
-(void)stopUpdatingLocation;
-(double)getDistanceFromUserLocationToCordinatesLatitude:(float)lat Longitude:(float)lon;
@end
//### implementation file:
@implementation NYLocationManager
@synthesize locationManager;
@synthesize latitude;
@synthesize longitude;
@synthesize altitude;
+ (id)getInstance
{
static NYLocationManager *Instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Instance = [[self alloc] init];
});
[Instance startUpdatingLocation];
return Instance;
}
- (id)init
{
if (self = [super init])
{
latitude =0.0;
longitude =0.0;
altitude =0.0;
if([[NSUserDefaults standardUserDefaults] objectForKey:@"locationLongitude"] != nil)
{
NSUserDefaults *savedLocation=[NSUserDefaults standardUserDefaults];
latitude =[[savedLocation objectForKey:@"locationLatitude"] floatValue];
longitude =[[savedLocation objectForKey:@"locationLongitude"] floatValue];
altitude =[[savedLocation objectForKey:@"locationAltitude"] floatValue];
}
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
locationManager.delegate = self;
if ([CLLocationManager locationServicesEnabled])
{
[locationManager startUpdatingLocation];
} else
{
[NYMessageToUser alertUserWithTitle:@"Location Services is Disabled!!!" withMessage:@"This app is designed to share images with location, Please enable location for this app and relucnh the app"];
}
}
return self;
}
- (void)dealloc
{
// Should never be called, but just here for clarity really.
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *loc =[locations lastObject];
self.longitude =loc.coordinate.longitude;
self.latitude =loc.coordinate.latitude;
self.altitude =loc.altitude;
NSUserDefaults *savedLocation=[NSUserDefaults standardUserDefaults];
[savedLocation setObject: [NSString stringWithFormat:@"%f", self.longitude] forKey:@"locationLongitude"];
[savedLocation setObject: [NSString stringWithFormat:@"%f", self.latitude] forKey:@"locationLatitude"];
[savedLocation setObject: [NSString stringWithFormat:@"%f", self.altitude ] forKey:@"locationAltitude"];
[savedLocation synchronize];
[locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
[locationManager stopUpdatingLocation];
[NYMessageToUser alertUserWithTitle:@"Location Error!!!" withMessage:@"This app is designed to use with valid location, Please enable location for this app and relucnh the app"];
}
-(void)startUpdatingLocation
{
if ([CLLocationManager locationServicesEnabled])
{
[locationManager startUpdatingLocation];
} else
{
[NYMessageToUser alertUserWithTitle:@"Location Services is Disabled!!!" withMessage:@"This app is designed to share images with location, Please enable location for this app and relucnh the app"];
}
}
-(void)stopUpdatingLocation
{
[locationManager stopUpdatingLocation];
}
-(double)getDistanceFromUserLocationToCordinatesLatitude:(float)lat Longitude:(float)lon
{
CLLocation *locA = [[CLLocation alloc] initWithLatitude:self.latitude longitude:self.longitude];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
CLLocationDistance distance = [locA distanceFromLocation:locB];
return distance;
}
@end
//### How to use
NYLocationManager *loc =[NYLocationManager getInstance];
NSLog(@"longitude: %f, latitude: %f, altitude: %f",loc.longitude,loc.latitude,loc.altitude);