locationManager返回0.00000,在viewDidLoad中0.00000个坐标



我试图找出如何获得我的位置坐标"currentCentre"在ViewDidLoad,所以我可以自动查询谷歌的数据在我的详细视图(不点击任何),问题是坐标是0.0000,0.0000最初和谷歌不能查询任何东西,但如果我放大在地图上,然后查询我返回谷歌结果。我怎么能解决这个问题,我读过几个地方的locationManager需要更多的时间来找到我的位置,所以如果这是问题,什么是一个或两个线性我可以用来创建一个人为的延迟,所以我可以得到有效的坐标:

代码片段:

- (void)viewDidLoad
{
[super viewDidLoad];
_mapView.delegate = self;
self.mapView.delegate = self;
[self.mapView setShowsUserLocation:YES];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
[self autoQuery];
}
-(void)autoQuery {
NSLog(@"%f", currentCentre.latitude);
NSLog(@"%f", currentCentre.longitude);
[self queryGooglePlaces:@"cafe"]; //]self.detailItem];
NSLog(@"%@",self.detailItem);
}

@rmaddy和其他可能好奇的人,我现在明白我以前的解决方案不断更新,这是不希望的(我只想让autoQuery在视图打开时运行一次),所以我添加了属性@property (nonatomic) int count;,并使用以下代码确保每次打开视图时只调用一次自动查询

if (currentCentre.latitude != 0.000000)
{
    if (_count == 0) {
        [self autoQuery];
        _count = _count + 1;
    }
}

currentCentre在接口中定义,使用CLLocationCoordinate2D currentCentre;

获取GPS坐标

如果有人有更好或更有效的方法,请写评论。由于

您可以使用位置管理器委托方法并在那里更新google查询。

  - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { [self autoQuery]; }

我实际上想出了一个更好的方法来解决这个问题,如果你调用:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    NSLog(@"%f", currentCentre.latitude);
    [self autoQuery];
}

此方法将不允许您执行内部代码,直到位置被更新,所以一旦更新内部代码将执行。

最新更新