我正在使用位置管理器來獲取用戶位置時,他/她按下按鈕。CLLocationManager奇怪的行爲
我只需要那個exat時間的位置,所以我得到位置後關閉更新。
事情,如果我在第一次獲得id 0 lat 0 lng的位置。
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
lat = newLocation.coordinate.latitude;
lng = newLocation.coordinate.longitude;
[locationManager stopUpdatingLocation];
NSLog(@"New Latlng: %f x %f", lat, lng);
}
當用戶按下按鈕時,我調用:[locationManager startUpdatingLocation];
然後,我將位置發送到我的服務器[self sendData]。但是位置是零。 在我-sendData方法我增加了一個NSLog的追查發生了什麼事情,我意識到,我是在數據發送到-locationManager之前的服務器:didUpdateToLocation被調用......
2012-04-02 16:06:24.225 MyApp[2892:707] ---- Data sent to server ----
2012-04-02 16:06:25.167 MyApp[2892:707] Nova Latlng: -23.003582 x -43.316311
我想這但它沒有按預期工作:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[locationManager startUpdatingLocation];
dispatch_async(dispatch_get_main_queue(), ^{
[self sendData];
});
});
如何使[self sendData]等待,直到我有lat和lng變量正確填充?
該委託仍然被調用至少2次。 – cleverbit 2012-10-11 20:42:08