gpt4 book ai didi

ios - iOS 8 中的 MapKit 无法运行

转载 作者:行者123 更新时间:2023-11-29 12:34:23 25 4
gpt4 key购买 nike

我正在构建一个在 MapView 中显示 POI 的 iOS 应用程序,但首先我可以显示 map ,而对于 iOS8,这开始是一个问题。我在这个网站上阅读了很多问题,但即使代码看起来正确,也没有一个问题能在我的应用程序上运行。

我在 myapplicationTest-info.plist 中输入了以下代码

<key>NSLocationAlwaysUsageDescription</key>

文件.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 键提供一个值,该键是描述您的应用为何要使用定位服务的字符串 -

例如——

<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];
}

最后,这不会阻止您的 map 更新,但它没有意义 - 您在分配和初始化它之前将委托(delegate)分配给您的 CLLocationManager

你应该说——

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

关于ios - iOS 8 中的 MapKit 无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26757127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com