[英]MapKit in iOS 8 not run
I am building an iOS application that display a POI in a MapView, but firstly I can display the map, and with iOS8 this is begin a problem. 我正在构建一个在MapView中显示POI的iOS应用程序,但是首先我可以显示地图,而对于iOS8,这是一个问题。 I read a lot question in this site, but none run on my app even if the code seems right.
我在这个站点上读了很多问题,但是即使代码看起来正确,也没有一个在我的应用程序上运行。
I entered into myapplicationTest-info.plist the following code 我将以下代码输入到myapplicationTest-info.plist中
<key>NSLocationAlwaysUsageDescription</key>
And the code in the file.m is this: 而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
Where is the mistake? 错误在哪里?
Why it doesn't run? 为什么不运行?
You need to provide a value for NSLocationAlwaysUsageDescription
key which is the string that describes why your app wants to use location services - 您需要为
NSLocationAlwaysUsageDescription
键提供一个值, NSLocationAlwaysUsageDescription
键是描述您的应用为何要使用位置服务的字符串-
For example - 例如 -
<key>NSLocationAlwaysUsageDescription</key>
<string>FindMeDonuts will use your location to identify nearby donut shops</string>
Also, rather than checking the iOS version, it is better to check if CLLocationManager responds to the authorisation selector - 另外,与其检查iOS版本,不如检查CLLocationManager是否响应授权选择器-
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
finally, this won't stop your map from updating, but it doesn't make sense - you are assigning a delegate to your CLLocationManager
before you allocate and initialise it. 最终,这不会阻止地图的更新,但这没有意义-在分配和初始化它之前,您正在将委托分配给
CLLocationManager
。
You should say - 你应该说 -
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate=self;
I don't know if i understand your problem. 我不知道我是否理解你的问题。
if your problem is about the map is not show, you need to set frame of your map. 如果您的问题是关于地图未显示,则需要设置地图框架。
self.mapView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
hope i could help you 希望我能帮助你
The best way is to use the -locationManager:didUpdateLocations method provided by CLLocationManagerDelegate. 最好的方法是使用CLLocationManagerDelegate提供的-locationManager:didUpdateLocations方法。
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
[self.locationManager stopUpdatingLocation];
// Move this line here
self.mapView.showsUserLocation = YES;
}
After calling [self.locationManager startUpdatingLocation]
in ViewDidLoad, you shouldn't set self.mapView.showsUserLocation
to YES
yet as permission to use location services is not yet approved. 在
[self.locationManager startUpdatingLocation]
调用[self.locationManager startUpdatingLocation]
后,您不应将self.mapView.showsUserLocation
设置为YES
,因为尚未批准使用位置服务的权限。 Use the delegate to wait/get a location update from the system before setting self.mapView.showsUserLocation
to YES
. 在将
self.mapView.showsUserLocation
设置为YES
之前,使用委托从系统等待/获取位置更新。
You also need to enable location services 您还需要启用位置服务
if([CLLocationManager resolveClassMethod:@selector(locationServicesEnabled)]) {
[CLLocationManager locationServicesEnabled];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.