2011-03-21 67 views
7

我正在iphone sdk4.0上做一個應用程序。在那沒有更新的位置方法調用。 我已經給出了我的代碼below.Please help.Thanks提前。didUpdateLocation方法從來沒有叫

-(id)init 
{ 
    [super init]; 

    obj=[[UIApplication sharedApplication]delegate]; 

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

    return self; 

} 

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

    NSLog(@"%f",newLocation.coordinate.longitude); 

    obj=[[UIApplication sharedApplication]delegate]; 

    location=[[CLLocation alloc]initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude]; 
    obj.lattitude1=[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude]; 

    obj.longitude1=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude]; 
    //location=[[CLLocation alloc]initWithLatitude:39.9883 longitude:-75.262227]; 
} 
+0

終於工作了......我沒有做任何事情,我只是做了一個新項目,並複製了它的所有類。我不知道如何?但它的工作.. – 2011-03-23 05:11:01

回答

5

嗨,你的代碼似乎沒問題。 現在這些都成爲可能的原因:

您的init

:嘗試檢查是否有locationServicesEnabled與否。

locationManager = [[CLLocationManager alloc] init]; 
if(locationManager.locationServicesEnabled == NO){ 
    //Your location service is not enabled, Settings>Location Services 
} 

其他原因,您可能會禁止獲取位置到您的應用程序。 解決方案:只需從iPhone中刪除您的應用程序並重新構建,現在應該彈出對話框以允許位置。

使用此功能來檢查錯誤

- (void)locationManager:(CLLocationManager *)manager 
     didFailWithError:(NSError *)error 
{  
    NSLog(@"Error while getting core location : %@",[error localizedFailureReason]); 
    if ([error code] == kCLErrorDenied) { 
     //you had denied 
    } 
    [manager stopUpdatingLocation]; 
} 

否則一切似乎確定, 你已經運行iOS 4.0,可在iPhone 3G被安裝以後, 如果是iPhone 2G,那麼這個問題可能發生。

+1

我已經添加到我的應用程序..它的工作gr8 ... – 2011-04-01 09:03:19

1

您代理中的-locationManager:didFailWithError:有沒有被調用?也許你在某個時候拒絕了對位置數據的訪問,現在沒有得到提示,但訪問被拒絕。

0

allocinit你的位置管理器後,請檢查位置服務是允許的。此代碼是從蘋果的「LocateMe」代碼示例

if (locationManager.locationServicesEnabled == NO) { 
    UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [servicesDisabledAlert show]; 
    [servicesDisabledAlert release]; 
} 

如果你使用模擬器測試,嘗試用測試設備來代替。模擬器要求計算機的WiFi已打開,並且可以找到位於已知位置數據庫中的WiFi基站。

3

如果你在它不會工作的模擬器測試此。

這是因爲當該裝置改變位置該方法僅稱爲(並且模擬器從不改變位置)。

+0

不..我不檢查模擬器..我檢查它在iPhone設備上 – 2011-03-21 11:38:12

+0

您是否嘗試過手動調用當前位置並確保您有一個有效的位置?您可以使用locationManager.location執行此操作。 – Zebs 2011-03-22 01:02:50

+7

這是不正確的,你現在可以模擬運動。 '模擬器 - >調試 - >位置' – Peres 2014-02-09 18:58:35

5

確保您添加CLLocationManager作爲屬性。

@屬性(非原子,強)CLLocationManager *的LocationManager;

+1

加布裏埃爾你給我看了很好的方法。我現在得到了解決方案。我試圖調用didupdateLocation方法,但它並沒有調用即使我應用除了你的建議之外的所有內容。最後我應用了CLLocationManager作爲property.Now委託方法被調用,我得到更新位置(緯度,長)值。 – user3182143 2016-08-01 07:17:18

+0

非常感謝你兄弟 – user3182143 2016-08-01 07:17:39

+0

你能告訴我關於屬性CLLocationManager的原因或解釋,爲什麼沒有屬性不工作? – user3182143 2016-08-01 07:28:11

18

此外,在iOS8上必須有兩個額外的東西:

  • 從位置管理您的Info.plist並請求授權添加一個關鍵要求它開始。

    • NSLocationWhenInUseUsageDescription

    • NSLocationAlwaysUsageDescription

  • 您需要請求授權對相應的位置的方法。

    • [self.locationManager requestWhenInUseAuthorization]

    • [self.locationManager requestAlwaysAuthorization]

代碼例如:

self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.delegate = self; 
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7. 
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 
    [self.locationManager requestWhenInUseAuthorization]; 
} 
[self.locationManager startUpdatingLocation]; 

來源:http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

+0

...一般來說,_Always_方法是不鼓勵的,如果您不確定自己在做什麼,請使用其他方法。 – bshirley 2014-10-01 21:49:59