2014-11-05 32 views
1

我正在构建一个在MapView中显示POI的iOS应用程序,但首先我可以显示地图,而在iOS8中,这开始出现问题。 我在这个网站上看了很多问题,但没有在我的应用上运行,即使代码看起来不错。iOS 8中的MapKit未运行

我进入myapplicationTest-的info.plist下面的代码

<key>NSLocationAlwaysUsageDescription</key> 

和代码在file.m是这样的:

#import "MapViewController.h" 

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) 

@interface MapViewController() 

@end 

@implementation MapViewController 
@synthesize mapView = _mapView ; 


- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 

    self.mapView.delegate = self; 
    self.locationManager.delegate = self; 
    self.locationManager = [[CLLocationManager alloc] init]; 
if (IS_OS_8_OR_LATER) { 
    //[self.locationManager requestWhenInUseAuthorization]; 
    [self.locationManager requestAlwaysAuthorization]; 
} 

    [self.locationManager startUpdatingLocation]; 

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

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

    self.locationManager.distanceFilter = kCLDistanceFilterNone; //Whenever we move 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    [self.locationManager startUpdatingLocation]; 
    NSLog(@"%@", [self deviceLocation]); 

    //View Area 
    MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } }; 
    region.center.latitude = self.locationManager.location.coordinate.latitude; 
    region.center.longitude = self.locationManager.location.coordinate.longitude; 
    region.span.longitudeDelta = 0.005f; 
    region.span.longitudeDelta = 0.005f; 
    [mapView setRegion:region animated:YES]; 

} 

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

} 
- (NSString *)deviceLocation { 
    return [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude]; 
} 
- (NSString *)deviceLat { 
    return [NSString stringWithFormat:@"%f", self.locationManager.location.coordinate.latitude]; 
} 
- (NSString *)deviceLon { 
    return [NSString stringWithFormat:@"%f", self.locationManager.location.coordinate.longitude]; 
} 
- (NSString *)deviceAlt { 
    return [NSString stringWithFormat:@"%f", self.locationManager.location.altitude]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 


@end 

哪里错了吗?

为什么它不运行?

+0

る增加键在plist中 – 2014-11-05 13:26:34

+0

我已经将它们设置 – 2014-11-05 13:56:10

回答

1

您需要为NSLocationAlwaysUsageDescription键提供一个值,它是一个描述为什么你的应用程序要使用位置服务的字符串 -

例如 -

<key>NSLocationAlwaysUsageDescription</key> 
<string>FindMeDonuts will use your location to identify nearby donut shops</string> 

此外,不是检查iOS版本,最好检查CLLocationManager是否响应授权选择器 -

if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { 
     [self.locationManager requestAlwaysAuthorization]; 
} 

最后,这不会阻止您的地图更新,但它没有任何意义 - 在分配并初始化之前,您正在为您的CLLocationManager分配一个代理。

你应该说 -

self.locationManager = [[CLLocationManager alloc] init]; 
self.locationManager.delegate=self; 
+0

对不起,我刚刚为NSLocationAlwaysUsageKey提供了一个值,我也加了你如果选择器和它又错了.. – 2014-11-05 13:36:43

+0

你在控制台上得到任何警告吗?你是在模拟器上还是在真实的设备上运行?你是否真的从iOS获得权限提示?你有没有尝试删除你的应用程序,然后运行它? – Paulw11 2014-11-05 13:38:02

+0

没有任何警告,我用真实设备 – 2014-11-05 13:39:48

0

我不知道我是否理解你的问题。

如果您的问题是关于地图不显示,您需要设置您的地图的框架。

self.mapView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); 

希望我可以帮助你

+0

不,地图是刚刚绘制的,但我不能表现出我的位置,我不不知道为什么会发生这种情况,因为代码看起来不错。 – 2014-11-05 13:20:57

0

最好的办法是使用-locationManager:由CLLocationManagerDelegate提供didUpdateLocations方法。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    [self.locationManager stopUpdatingLocation]; 
    // Move this line here 
    self.mapView.showsUserLocation = YES; 
} 

在viewDidLoad中调用[self.locationManager startUpdatingLocation]后,你不应该设置self.mapView.showsUserLocationYES但以使用位置服务的权限尚未获得批准。在将self.mapView.showsUserLocation设置为YES之前,请使用代理等待/从系统获取位置更新。

0

您还需要启用位置服务

if([CLLocationManager resolveClassMethod:@selector(locationServicesEnabled)]) { 
    [CLLocationManager locationServicesEnabled]; 
}