2015-03-02 29 views
-1

我是位置服務的新手。我已經使用startMonitoringSignificantLocationChanges(void)locationManager:(CLLocationManager *)manager didUpdateToLocation: (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation方法,但它不會在我更改位置值時被調用。每當用戶改變其位置時,我都想獲取位置值。我應該如何實現這一點?請幫我解決。提前致謝。每當它改變位置時都要監視用戶的位置

+0

您是否分配了CLLocationManagerDelegate? – 2015-03-02 11:34:09

+0

@DheerajSingh是的我已經使用myLocationManager.delegate = self;並分配給它的代表。 – 2015-03-02 12:39:19

+0

是你使用ios8嗎?並可以顯示你的代碼 – 2015-03-02 12:55:27

回答

0

您需要實現「didUpdateLocations」委託方法。下面是示例

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    NSLog(@"locations : %@",locations.description); 

    CLLocation *currentLocation = [locations lastObject]; 
    NSLog(@"current location : %f %f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude); 



} 
+0

請找到我更新的問題,是的,我也嘗試過這種方法,但它不工作。 – 2015-03-02 11:33:05

+0

didUpdateToLocation方法已棄用。如果locationManager:didUpdateLocations:實現了 ,則不會調用此方法。一旦檢查它 – user4261201 2015-03-02 11:40:32

+0

我使用你的updateLocation方法,那麼它不會被調用。爲什麼會這樣呢? – 2015-03-02 12:38:20

0

在iOS中8,你需要做兩個額外的東西來獲得位置工作: 1.添加下列鍵的一個或兩個,以你的Info.plist文件:

NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription

  1. 接下來,您需要爲相應的位置方法(WhenInUse或Background)請求授權。使用這些調用之一:

    [self.locationManager requestWhenInUseAuthorization];

    [self.locationManager requestAlwaysAuthorization];

有關詳細信息,請參閱以下鏈接。 http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/