2015-07-13 70 views
1

我已經設置當前位置不在圖形頁面顯示出來(IOS)

self.mapView.showsUserLocation = YES;

但是,我仍然無法在Map中看到當前的引腳。

我錯過了什麼?

這裏的viewDidLoad中

- (void)viewDidLoad { 
self.mapView.delegate = self; 
self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.delegate = self; 
self.locationManager.distanceFilter = kCLDistanceFilterNone; 
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
#ifdef __IPHONE_8_0 
if(IS_OS_8_OR_LATER) { 
    [self.locationManager requestAlwaysAuthorization]; 
} 
#endif 

[self.locationManager startUpdatingLocation]; 

self.mapView.showsUserLocation = YES; 
[self.mapView setMapType:MKMapTypeStandard]; 
[self.mapView setZoomEnabled:YES]; 
[self.mapView setScrollEnabled:YES]; 

MKCoordinateRegion region; 
region.center.latitude = 25.03; 
region.center.longitude = 121.5; 
region.span.latitudeDelta = 0.2; 
region.span.longitudeDelta = 0.2; 

[self.mapView setRegion:region animated:YES]; 
} 

而且MapView的

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation*)userLocation 
{ 
double X= userLocation.coordinate.latitude; 
double Y= userLocation.coordinate.longitude; 

NSLog(@"%f,%f",X,Y); 

CLLocationCoordinate2D currentPosition = [userLocation coordinate]; 
MKCoordinateRegion region =MKCoordinateRegionMakeWithDistance(currentPosition, 800, 800); 
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES]; 
} 

謝謝!

+1

您是否要求用戶使用該位置的權限? –

+0

你的意思是一個詢問是否允許訪問的報警窗口? –

+0

請看這個問題的答案(http://stackoverflow.com/questions/24062509/location-services-not-working-in-ios-8),也許你會找到答案。 –

回答

2

iOS8上的位置授權存在問題。爲了解決這個問題,請確保您添加下列鍵的一個或兩個,以你的Info.plist文件:

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

而在你的代碼替換這一部分:

#ifdef __IPHONE_8_0 
if(IS_OS_8_OR_LATER) { 
    [self.locationManager requestAlwaysAuthorization]; 
} 
#endif 

有了這個:

// Add this to solve a location authorization issue on iOS8 
// See more at: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/ 
if ([self._locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { 
    [self._locationManager requestWhenInUseAuthorization]; 
} 

希望這會有所幫助。

+0

這是否適合你? – SanitLee

相關問題