我正在使用iOS 8中的Map Kit,使用的是Obj-C而不是SWIFT.我无法获取设备位置,它被设置为0.00,0.00,我收到错误信息:

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

我实现了:(我一次只try 了一个,没有运气)

if(IS_OS_8_OR_LATER) {
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation]; 

还有信息.普利斯特

NSLocationWhenInUseUsageDescription  :   App would like to use your location.
NSLocationAlwaysUsageDescription  :  App would like to use your location.

我确实会被提示允许应用程序使用我的位置,但在我同意后,没有任何变化.位置显示为0.00,0.00.

显示用户位置的代码:

//Get Location
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];

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

迈克.

**编辑:查看下面的答案.

推荐答案

我成功了.我在下面发布了我的代码,以帮助其他有问题的人.

以下是我在iOS 8中使用MapKit map 视图的完整代码.

在你的AppName信息里.普利斯特

NSLocationWhenInUseUsageDescription

NSLocationAlwaysUsageDescription

该值是要显示的消息的字符串:

YourAppName would like to use your location.

在头文件中.(我使用App Name-Prefix.pch,但你的视图控制器.H也可以)

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

你的视图控制器.H

#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>

@interface YourViewController : UIViewController <MKMapViewDelegate,  CLLocationManagerDelegate> {

}


@property(nonatomic, retain) IBOutlet MKMapView *mapView;
@property(nonatomic, retain) CLLocationManager *locationManager;

你的视图控制器.M

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    mapView.delegate = self;
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    #ifdef __IPHONE_8_0
    if(IS_OS_8_OR_LATER) {
         // Use one or the other, not both. Depending on what you put in info.plist
        [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager requestAlwaysAuthorization];
    }
    #endif
    [self.locationManager startUpdatingLocation];

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

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

    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    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];
}

享受

--迈克

Objective-c相关问答推荐

如何使用 iOS 7 SpriteKit 粒子向非游戏的 iOS 应用程序添加粒子效果?

可供学习的 iOS 示例项目

如何使用格式化占位符本地化字符串?

表达式不可分配 - 将浮点数分配为 Xcode 中其他两个浮点数的总和时出现问题?

在子类中覆盖init

使用 [NSBundle mainBundle] pathForResource: ofType:inDirectory: 访问文件

带有语法高亮的 UITextView

xcode 未知类型名称

在 Xcode 中打破 EXC_BAD_ACCESS?

将 NSArray 转换为 NSDictionary

NSString:从字符串中删除 UTF-8 重音的简单方法?

滚动后检测 UITableView 中的当前顶部单元格

在 iPhone 上测量人体胀气( Human-Flatulence)传播波包

进入编辑模式时动画自定义绘制的 UITableViewCell

如何在 ios 8 的 alertview 中添加文本输入?

如何在 UIImageView 中显示 base64 图像?

如何推送两个视图控制器但只为第二个设置动画过渡?

选中时选中的 UItableViewCell 保持蓝色

如何在 xcode 项目中启用/禁用 ARC?

请求访问相机胶卷的权限