[英]MapKit in iOS 8 not run
我正在構建一個在MapView中顯示POI的iOS應用程序,但是首先我可以顯示地圖,而對於iOS8,這是一個問題。 我在這個站點上讀了很多問題,但是即使代碼看起來正確,也沒有一個在我的應用程序上運行。
我將以下代碼輸入到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
錯誤在哪里?
為什么不運行?
您需要為NSLocationAlwaysUsageDescription
鍵提供一個值, 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;
我不知道我是否理解你的問題。
如果您的問題是關於地圖未顯示,則需要設置地圖框架。
self.mapView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
希望我能幫助你
最好的方法是使用CLLocationManagerDelegate提供的-locationManager:didUpdateLocations方法。
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
[self.locationManager stopUpdatingLocation];
// Move this line here
self.mapView.showsUserLocation = YES;
}
在[self.locationManager startUpdatingLocation]
調用[self.locationManager startUpdatingLocation]
后,您不應將self.mapView.showsUserLocation
設置為YES
,因為尚未批准使用位置服務的權限。 在將self.mapView.showsUserLocation
設置為YES
之前,使用委托從系統等待/獲取位置更新。
您還需要啟用位置服務
if([CLLocationManager resolveClassMethod:@selector(locationServicesEnabled)]) {
[CLLocationManager locationServicesEnabled];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.