2013-10-23 39 views
1

我有一個關於位置的問題。獲取有關didUpdateLocations和didUpdateToLocation的位置永遠不會調用ios 5.1和ios6

我需要在iOS 5.1和iOS 6上獲取經度和緯度的位置。

但我在didUpdateLocations和didUpdateToLocation函數中編寫NSLog。

我沒有看到控制檯中的日誌顯示。

我使用模擬設置位置。我測試的緯度值是25.317973,經度值是121.538161(D1)。

那麼我的測試值是(25.917973,121.538161)(D2)。

D1和D2的距離大於10米;

但我從來沒有在控制檯中看到日誌顯示。

我在下方附上我的代碼:

我.h文件中

#mport <GoogleMaps/GoogleMaps.h> 
#import <CoreLocation/CoreLocation.h> 
#import <Corelocation/CLLocationManagerDelegate.h> 

@interface LocationMapViewController : AbstractViewController<  CLLocationManagerDelegate, GMSMapViewDelegate > 
@property (strong, nonatomic) CLLocationManager *locationManager; 
@end 

我.m文件

- (void)viewDidLoad 
     { 
      [super viewDidLoad]; 

if(_locationManager == nil) 
    { 
     _locationManager = [CLLocationManager new]; 
     _locationManager.delegate = self; 

     _locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
     _locationManager.distanceFilter = 10.0f; 
     [_locationManager startUpdatingHeading]; 

    } 

-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray  *)locations 
{ 
    NSLog(@"==latitude %f, longitude %f", [[locations objectAtIndex:([locations  count]-1)] coordinate].latitude , [[locations objectAtIndex:([locations count]-1)]  coordinate].longitude); 
} 

-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation  *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"====latitude %f, longitude %f", newLocation.coordinate.latitude,  newLocation.coordinate.longitude); 
} 

有任何人知道什麼是錯我的代碼?

非常感謝你

回答

1

您不必導入CLLocationManagerDelegate你h和AFAIK我覺得CoreLocation沒有使用谷歌地圖一樣,所以,除非您使用的是谷歌地圖API或東西應該有無需導入GoogleMaps。

嘗試在而不是你的方法,使用這樣的:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    if(_locationManager == nil) 
    { 
     _locationManager = [[CLLocationManager alloc] init]; 
     _locationManager.delegate = self; 
     _locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

     [_locationManager startUpdatingLocation]; 
    } 
} 

編輯:

我意識到你正試圖獲得在距離差。因此,也許只是改變你的

_locationManager = [CLLocationManager new]; 

_locationManager = [[CLLocationManager alloc] init]; 

,並應解決的問題。

繼您的評論後,我再次檢查了一下,發現您正在使用startUpdatingHeading方法調用,因此您應該使用didUpdateHeading委託方法。這是從蘋果CLLocationManager參考:

Heading events are delivered to the locationManager:didUpdateHeading: method of your delegate. If there is an error, the location manager calls the locationManager:didFailWithError: method of your delegate instead.

希望這有助於! :)

+0

我曾試過你的方法。但我仍然沒有在我的控制檯中獲得任何日誌。 :( 我曾嘗試在距離設置差異。 我不知道什麼問題導致。 我曾經使用谷歌地圖api,所以我導入這個頭文件。 謝謝 – dickfala

+0

我明白我的錯在哪裏。 因爲我沒有將startUpdatingHeading更改爲startUpdatingLocation。 非常感謝faterpig。 – dickfala

+0

是的,我正要回答這個問題。祝您好運! :) – faterpig

相關問題