2012-12-24 24 views
4

我工作的地圖應用,StopUpdatingLocation方法不工作的iOS5

我嘗試使用[locationManager stopUpdatingLocation];方法停止位置服務。

它似乎可以在iOS4.3中正常工作,但在iOS5中無法正常工作。

請任何人建議我如何在iOS5中停止位置服務?

+0

釋放你的位置服務對象,locationServiceManager = nil;如果ARC –

+0

確定「'locationManager'」不是零,當你把它叫做「'stopUpdatingLocation'」方法... –

+0

感謝prateek的評論..我沒有在我的應用程序中使用ARC ..但我寫了一個代碼這個.. - (無效)的LocationManager:(CLLocationManager *)經理 \t didUpdateToLocation:(CLLocation *)newLocation \t \t fromLocation:(CLLocation *)oldLocation { 如果(的LocationManager) { mapview.showsUserLocation = NO; locationManager = nil; [locationManager stopUpdatingLocation]; } } ...可以停止位置服務嗎? – Anki

回答

12

有時候碰巧位置管理不停止即使stopUpdationLocations

你需要釋放你的位置管理,對於設置

locationManager = nil; 

這應該寫入後

[locationManager stopUpdatingLocation];

另外,小心你不要調用locationManager對象,將其設置爲零後,否則你的appl它會崩潰

如果它仍然沒有停止位置服務,那麼有解決方法,只要使用BOOL變量,如果你不想在將來使用它。

希望這可以幫助你!

+1

感謝prateek,但我將需要調用開始更新位置方法,因爲每2分鐘我需要採取當前位置的經緯度。由於電池耗盡問題,我在獲取新位置時使用stopUpdatingLocation方法。 – Anki

+0

您可以重新分配它,然後可以調用它 –

5

你可以從以下兩個鏈接得到的幫助:

LINK-1:

http://iphonedevsdk.com/forum/iphone-sdk-development/2299-cllocationmanager-stopupdatinglocation-not-working.html

參考site_reg的回答上面的鏈接:

我是有這個同樣的問題,它似乎是通過在.m文件中聲明靜態BOOL並在輸入didUpdateToLocation時檢查它來解決的HOD。

@implementation MyFile 
@synthesize MySynth; 
static BOOL haveAlreadyReceivedCoordinates = NO; 

然後在您的didUpdateToLocation方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 

    if(haveAlreadyReceivedCoordinates) { 
     return; 
    } 
    haveAlreadyReceivedCoordinates = YES; 
    ... 
} 

這不是技術上使它停止接收更新,但它會忽略第一個之後的任何更新。如果你的應用程序依靠獲取一個位置更新來完成它的工作,這應該有所幫助。

編輯-1:

而且一旦這樣做,以後還可以[locationManager stopUpdatingLocation];行添加locationManager.delegate = nil;

LINK-2:

why self.locationManager stopUpdatingLocation doesn't stop location update

請參考上述鏈路jnic的回答是:

startMonitoringSignificantLocationChanges反面不是 stopUpdatingLocation,它是 stopMonitoringSignificantLocationChanges

你可能想用 startUpdatingLocation爲更多的定期更新的緣故,以取代 startMonitoringSignificantLocationChanges,除非 你只監測顯著 位置變化的具體原因。

查看CLLocation documentation 瞭解更多詳情。

我已經添加了鏈接的答案,只是爲了確保答案是有用的,即使提供的鏈接將來發生故障。