2014-12-11 28 views
0

我正在創建一個簡單的UIMapView自定義註釋。剛升級到Yosemite和Xcode 6.1.1。我已經嘗試了一切,以獲取用戶位置在地圖上顯示,但由於某些原因,藍點不顯示在地圖上。我在UIMapView的檢查器中選擇了顯示用戶位置。當我在手機上運行時,最後列出的錯誤顯示在屏幕底部的日誌窗口中。感謝您的任何幫助。用戶位置將不會顯示在mapView

我.h文件中

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


@interface ViewController : UIViewController <MKMapViewDelegate> 

@property (strong, nonatomic) IBOutlet MKMapView *mapView; 

我.m文件

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self.mapView.delegate self]; 
    [self.mapView setShowsUserLocation:YES]; 
} 

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation 
{ 
    CLLocationCoordinate2D loc = [userLocation coordinate]; 
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 500, 500); 
    [self.mapView setRegion:region animated:YES]; 

} 

錯誤接收運行後 501:107687]嘗試啓動MapKit位置更新,而不提示進行位置信息授權。必須首先調用 - [CLLocationManager requestWhenInUseAuthorization]或 - [CLLocationManager requestAlwaysAuthorization]。

+0

所以..該錯誤信息是很清楚的給我。您需要請求授權... – 2014-12-11 04:56:26

+0

快速Google搜索揭示* DOZENS *示例https://www.google.com/search?q=CLLocationManager+requestWhenInUseAuthorization&oq=CLLocationManager+requestWhenInUseAuthorization&aqs=chrome..69i57j0l5.404j0j7&sourceid=chrome&es_sm= 119&ie = UTF-8 – 2014-12-11 04:57:16

+0

特別是,您應該在ViewDidLoad中調用此* * BEFORE *調用setShowsUserLocation ...因此,將您的身份驗證包裝在BOOL方法中,然後只調用映射方法... – 2014-12-11 04:58:58

回答

0

根據我對問題的評論......您處於權限錯誤狀態。 你需要牛逼檢查用戶權限調用Update方法之前...

CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus]; 

if (authorizationStatus == kCLAuthorizationStatusAuthorized || 
    authorizationStatus == kCLAuthorizationStatusAuthorizedAlways || 
    authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) { 

    // update location here...  

} 

然後,如@zaheer指出的那樣,你需要一個鍵值面值添加到-Info.plist

NSLocationWhenInUseUsageDescription 一些信息給用戶爲什麼想自己的位置

NSLocationAlwaysUsageDescription 有些詼諧的評論關於美國國家安全局

取決於只有當應用程序在前臺或後臺是否需要位置...

+1

還必須添加,NSLocationWhenInUseUsageDescription,NSLocationAlwayseUsageDescription在info.plist中。 – zaheer 2014-12-11 05:03:21

相關問題