0

locationManager在我的应用程序中调用了 a,并在各种方法中delegate引用了 getUserLatitude 和 getUserLongitude 函数。viewDidAppearviewControllers

我的问题是我的viewDidAppear方法启动太快,appDelegate无法找到用户,我得到 0.000000latitude和 0.000000 longitude。有谁知道我该如何解决这个问题?谢谢!

我的 appDelegate 由以下位置方法构成:

- (NSString *)getUserCoordinates
{
    NSString *userCoordinates = [NSString stringWithFormat:@"latitude: %f longitude: %f", 
    locationManager.location.coordinate.latitude, 
    locationManager.location.coordinate.longitude];
    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    [locationManager startUpdatingLocation];
    return userCoordinates;
}

- (NSString *)getUserLatitude
{
    NSString *userLatitude = [NSString stringWithFormat:@"%f", 
    locationManager.location.coordinate.latitude];
    return userLatitude;
}

- (NSString *)getUserLongitude
{
    NSString *userLongitude = [NSString stringWithFormat:@"%f", 
    locationManager.location.coordinate.longitude];
    return userLongitude;
}

我正在使用这段代码从 appDelegate 获取更新的位置:

- (void)viewDidAppear:(BOOL)animated
{   
    NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
    getUserLatitude];

    NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
    getUserLongitude];
}
4

3 回答 3

1

在位置管理器更新位置后,会调用一个可以在此处使用的委托。更新位置后,您将在 didUpdateToLocation 方法中获得控件

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"Success ");
}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"Failed %d",[error code]);

}
于 2012-12-24T11:56:49.857 回答
0

我在我的一个应用程序中也面临同样的问题。首先分配CLLocationManager,设置delegate它,然后调用startUpdatingLocation可以解决您问题的方法。

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
    [locationManager startUpdatingLocation];

设置委托后,它调用其委托方法并为我们提供当前位置的lattitude&longitude作为。

//delegate method
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{    
    self.currentLat=newLocation.coordinate.latitude;
    self.currentLong=newLocation.coordinate.longitude;
    NSLog(@"appDelegate newLocation %f  %f",self.currentLat,self.currentLong);
}
于 2012-12-24T11:52:03.270 回答
0

我通过做这样的事情来解决这个问题。

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


     NSLog(@"Ahsan");
     if (!oldLocation ||
        (oldLocation.coordinate.latitude != newLocation.coordinate.latitude && 
         oldLocation.coordinate.longitude != newLocation.coordinate.longitude &&
         newLocation.horizontalAccuracy <= 100.0)) 
     {
        //Your code comes here...
     }
 }
于 2012-12-24T12:33:10.933 回答