我正在使用此代码获取用户的位置:
@implementation TabBar
CLLocationManager *locationManager;
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
UsersCurrentLongitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
UsersCurrentLatitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
NSLog(@"Latitude:%@,Longitude:%@",UsersCurrentLatitude,UsersCurrentLongitude);
}
并在 nslog 屏幕中成功显示位置。但是当我想在另一个导入我的位置类(在本例中为 TabBar)的类中使用该位置时,位置变为空。我能做些什么来解决这个问题?编辑:
在我为我的问题尝试了两种不同的方法后,我仍然在 NsLog 中得到空值:
第一种方法:
在 AppDelegate.h:
@property(nonatomic,retain)NSString *latitude;
@property(nonatomic,retain)NSString
At AppDelegate.mlongitude;
@synthesize latitude;
@synthesize longitude;
在 TabBar.m
#import "AppDelegate.h"
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
[(AppDelegate *)[[UIApplication sharedApplication] delegate] setLatitude:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]];
[(AppDelegate *)[[UIApplication sharedApplication] delegate] setLongitude:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]];
}
}
At my SecondView.m
#import "AppDelegate.h"
(void)viewDidLoad
{
[super viewDidLoad];
NSString *UsersCurrentLatitude = [(AppDelegate *)[[UIApplication sharedApplication] delegate] latitude];
NSString *UsersCurrentLongitude = [(AppDelegate *)[[UIApplication sharedApplication] delegate] longitude];
NSLog(@"latitude:%@,longitude%@",UsersCurrentLatitude,UsersCurrentLongitude);
}
第二种方法(我在 youtube 上找到了一个教程,并为我的问题安排了该方法,因此它可能是无意义的代码):
我在 SingletonClass.h 中创建了一个名为 SingletonClass 的 NSObject 类
#import <CoreLocation/CoreLocation.h>
@interface SingletonClass : NSObject <CLLocationManagerDelegate>
@property (nonatomic, strong) NSNumber *UsersCurrentLatitude;
@property (nonatomic, strong) NSNumber *UsersCurrentLongitude;
+(SingletonClass *) Lokasyon;
@end
在 SingletonClass.m
#import "SingletonClass.h"
@implementation SingletonClass {
CLLocationManager *locationManager;
}
@synthesize UsersCurrentLongitude;
@synthesize UsersCurrentLatitude;
+(SingletonClass *)Lokasyon {
static SingletonClass *Lokasyon = nil;
if (!Lokasyon) {
Lokasyon = [[super allocWithZone:nil] init];
}
return Lokasyon;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
UsersCurrentLongitude = [NSNumber numberWithFloat:currentLocation.coordinate.longitude];
UsersCurrentLatitude = [NSNumber numberWithFloat:currentLocation.coordinate.latitude];
}
}
@end
在我的SeconViewController
#import "SingletonClass.h"
- (void)viewDidLoad
{
[super viewDidLoad];
NSNumber *UsersCurrentLongitude1 = [[SingletonClass Lokasyon]UsersCurrentLongitude];
NSLog(@"%@",UsersCurrentLongitude1);
}