0

我正在使用此代码获取用户的位置:

@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);
}
4

1 回答 1

0

以类似的方式声明它们Appdelegate.h-

    @property(nonatomic,retain)NSString *latitude;
    @property(nonatomic,retain)NSString *longitude;

确保你,在类中综合latitudelongitude属性。AppDelegate.m

将它们设置在TabBar.m您获得位置的CLLocationManager位置didUpdateToLocation:

if (currentLocation != nil) {
    [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] setLatitude:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]];

    [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] setLongitude:[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]];

        //UsersCurrentLatitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];

        //NSLog(@"Latitude:%@,Longitude:%@",UsersCurrentLatitude,UsersCurrentLongitude);

    }

访问其他类,如 -

    NSString aLatitude = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] latitude];
    NSString aLongitude = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] longitude];

PS:还有很多其他方法可以做你需要的事情,比如 - 在SingletoniOS 应用程序中上课或保存,File system即在 App Sandbox..

于 2013-05-08T18:00:02.470 回答