【问题标题】:viewDidAppear Method Called before locationManager can Locate UserviewDidAppear 方法在 locationManager 可以定位用户之前调用
【发布时间】:2012-12-24 11:46:54
【问题描述】:

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

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

我的 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];
}

【问题讨论】:

  • 在您的 .h 委托文件中设置 CLLocationManagerDelegate 委托,并按照我在回答中提到的操作。

标签: iphone objective-c ios cllocationmanager locationmanager


【解决方案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]);

}

【讨论】:

  • 嗨,我尝试在 viewControllers 中的 viewDidAppear 方法上方添加这些代码,这些代码引用回 appDelegate。他们似乎从来没有被叫过。你知道为什么会这样吗?
  • 你在声明位置管理器的地方写了 locationManager.delegate = self
  • 我不这么认为。我去看看
  • 您的实现可能存在几个问题。根据您的准确度设置和过滤器,可能需要几秒钟才能很好地修复您的位置。您肯定需要将您的视图设置为位置经理代表,以便在您的位置更新时收到通知。 (请参阅此答案中的方法)从那里,您可以更新视图的其余部分。只要确保您没有阻止您的视图/UI 等待位置更新。
【解决方案2】:

我的一个应用程序也面临同样的问题。先分配CLLocationManager,给它设置delegate,然后调用startUpdatingLocation方法可能会解决你的问题。

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

设置委托后,它调用它的委托方法并给我们当前位置的lattitudelongitude

//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);
}

【讨论】:

  • 感谢 Girish!我去看看!
【解决方案3】:

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

 - (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...
     }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 2013-01-30
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多