2011-11-21 62 views
2

我在我的應用程序中使用帶有「showsUserLocation」選項的MKMapView。我已經使用界面構建器添加了mapview,並在viewDidAppear和viewWillDissappear中設置了showUserLocation選項。MKMapView關閉位置服務

問題是從視圖返回或將應用程序置於後臺時。位置服務圖標不會消失。其實我必須刪除應用程序才能使其消失。即使我手動強制關閉應用程序,它仍然存在,如果我進入設置,它仍然在使用位置服務的應用程序列表中處於活動狀態。

我在下面添加了相關的代碼,我在這裏丟失了什麼?提前致謝!

的* .h:

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 

@interface ***viewController: UIViewController { 
    IBOutlet MKMapView *theMapView; 
} 

@property (nonatomic, retain) MKMapView *theMapView; 
@end 

* .M

- (void)viewDidAppear:(BOOL)animated { 

     [super viewDidAppear:animated]; 

     theMapView.showsUserLocation = YES; 
    } 

    - (void)viewWillDisappear:(BOOL)animated { 
     [super viewWillDisappear:animated]; 

     theMapView.showsUserLocation = NO; 

    } 

- (void)dealloc { 
    [super dealloc]; 

    [theMapView release]; 
} 
+1

添加委託的.h類並添加自己。 theMapView.delegate = self; –

+0

將代表添加到mapView沒有幫助,但感謝回覆。 – Madoc

+1

你是否設法解決這個問題?如果是這樣,我很想知道解決方案。 – chakrit

回答

3

我只是猜測,這可能不是解決它,但嘗試都將自自我聲明

- (void)viewDidAppear:(BOOL)animated { 

     [super viewDidAppear:animated]; 

     self.theMapView.showsUserLocation = YES; 
    } 

    - (void)viewWillDisappear:(BOOL)animated { 
     [super viewWillDisappear:animated]; 

     self.theMapView.showsUserLocation = NO; 

    } 
+0

一個很好的猜測,但不,它沒有幫助:\ – Madoc

+0

這對我有效。將MKMapView置於跟蹤模式將打開showsUserLocation,但關閉跟蹤模式不會關閉showsUserLocation。如上所述手動設置工作。 – Joe

0

當應用進入和出來的背景,你可以註冊通知,並保存和恢復分別將用戶的位置狀態:

class MyVCWithAMap: UIViewController { 
    ... 
    // Track the state of the map user location property when going to background 
    var trackUserLocation = false 
    ... 
    deinit { 
     NSNotificationCenter.defaultCenter().removeObserver(self) 
    } 
    ... 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     ... 
     // Register for notifications 
     NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(appEnteredForeground), name:UIApplicationWillEnterForegroundNotification, object: nil) 
     NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(appEnteredBackground), name:UIApplicationDidEnterBackgroundNotification, object: nil) 
    } 

    func appEnteredBackground() { 
     // Capture map user location state 
     viewModel.trackUserLocation = mapView.showsUserLocation    
     mapView.showsUserLocation = false 
    } 

    func appEnteredForeground() { 
     // Restore pre-snooze state 
     mapView.showsUserLocation = viewModel.trackUserLocation 
    } 

} 
+0

另外 - 請注意,當你走到背景時,我已經看到它需要10秒或更多的時間來讓位置指示器變得空洞(指示最近的訪問但不是當前的) – earnshavian