从AppDelegate获取当前位置

时间:2012-04-17 12:32:23

标签: iphone ios cllocationmanager

我的appDelegate中有一个方法获取纬度和经度,并返回一个我可以在任何viewController中使用的字符串。

AppDelegate

-(void)StartUpdating
{    
    locManager = [[CLLocationManager alloc] init];
    locManager.delegate = self;
    locManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    [locManager startUpdatingLocation];
}

#pragma mark 
#pragma mark locationManager delegate methods 


- (void)locationManager: (CLLocationManager *)manager
    didUpdateToLocation: (CLLocation *)newLocation
           fromLocation: (CLLocation *)oldLocation
{

    float latitude = newLocation.coordinate.latitude;
    strLatitude = [NSString stringWithFormat:@"%f",latitude];
    float longitude = newLocation.coordinate.longitude;
    strLongitude = [NSString stringWithFormat:@"%f", longitude];
    //[self returnLatLongString:strLatitude:strLongitude];

}

-(NSString*)returnLatLongString
{    
    NSString *str = @"lat=";
    str = [str stringByAppendingString:strLatitude];
    str = [str stringByAppendingString:@"&long="];
    str = [str stringByAppendingString:strLongitude];

    return str;
}

我在应用程序启动时调用 StartUpdating 。现在在我的viewController中我调用这个方法:

AppDelegate *appDelegate=[AppDelegate sharedAppDelegate];
NSString *str = [appDelegate returnLatLongString];  

但我在

中遇到了崩溃
str = [str stringByAppendingString:strLatitude];  
returnLatLongString中的

我知道我正在崩溃,因为当时 strLatitude 没有任何价值。但我该如何解决这个问题呢?我怎样才能更新纬度和经度值?

另外,我不想在viewControllers中使用 locationManager 。这样我就可以在所有viewControllers中获取当前位置,我在appDelegate中完成了它。

5 个答案:

答案 0 :(得分:5)

崩溃可能是因为str是NSString,因此不可变。在这种情况下将其分配回自身是有问题的。使用stringWithFormat

会更简单
NSString *str = [NSString stringWithFormat: @"lat=%@&long=%@", strLatitude, strLongitude];

答案 1 :(得分:0)

创建AppDelegate Class ..

的对象时出错了

要创建对象,只需编写此代码:

AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *str = [appDelegate returnLatLongString];

它完成了..

答案 2 :(得分:0)

导入AppDelegate.h文件。然后
使用以下代码:

MyAppDelegate *myAppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *result = [myAppDelegate returnLatLongString];

答案 3 :(得分:0)

我认为你需要初始化那些变量(strLatitude,strLongitude) - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法。如果它第一次到达这一点,它们就不会被初始化,所以你会崩溃。

答案 4 :(得分:0)

您的代码中存在两个潜在问题。

首先请确保在头文件中声明strLatitudestrLongitudestrong属性:

@property (nonatomic, strong) NSString * strLatitude;    
@property (nonatomic, strong) NSString * strLongitude;

使用@synthesize为它们自动生成适当的getter和setter,并为它们分配正确的值:

float latitude = newLocation.coordinate.latitude;
self.strLatitude = [NSString stringWithFormat:@"%f",latitude];
float longitude = newLocation.coordinate.longitude;
self.strLongitude = [NSString stringWithFormat:@"%f", longitude];

确保在分配值后不会释放它们,因为[NSString stringWithFormat:]会返回autoreleased个对象。

其次,在[locManager startUpdatingLocation];之后,在系统提供第一次位置更新之前需要一段时间。因此,在尝试使用它们构造另一个字符串之前,您需要检查strLatitudestrLongitude是否已经分配了值。类似的东西应该做的工作:

-(NSString*)returnLatLongString
{
    if (self.strLatitude == nil || self.strLongitude == nil)
        return nil;

    NSString *str = @"lat=";
    str = [str stringByAppendingString:strLatitude];
    str = [str stringByAppendingString:@"&long="];
    str = [str stringByAppendingString:strLongitude];

    return str;
}

然后,当然要使用[AppDelegate returnLatLongString]的视图控制器需要检查返回的值是否为nil

希望这会有所帮助......