iOS 8 Map Kit Obj-C无法获取用户位置

巴顿

我正在使用Obj-C NOT SWIFT在iOS 8中使用Map Kit。我无法将设备位置设置为0.00、0.00,但出现错误:

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

我实现了:(我一次只尝试了一次,没有运气)

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

并在info.plist中

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 View。

在您的AppName -Info.plist中,添加一个新行,其键名为:

NSLocationWhenInUseUsageDescription

要么

NSLocationAlwaysUsageDescription

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

YourAppName would like to use your location.

在您的头文件中。(我使用应用程序名称-Prefix.pch,但YourViewController.h也可以使用)

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

YourViewController.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;

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

请享用!

- 麦克风

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

iOS7 Map Kit删除默认位置图标

来自分类Dev

Google Maps iOS SDK,获取用户的当前位置

来自分类Dev

iOS Facebook SDK-获取用户位置

来自分类Dev

iOS:使用Sprite Kit实施用户控制的相机“缩放”

来自分类Dev

google map ios sdk:无法获取当前用户位置?

来自分类Dev

无法获取我的位置以显示在iOS 8的Google地图中

来自分类Dev

无法使用CLLocation Manager获取iOS 8中用户的位置?

来自分类Dev

物理体边缘IOS8,Sprite Kit

来自分类Dev

如何在iOS中使用Twitter Kit / Fabric从登录的用户获取用户配置文件图像

来自分类Dev

检查用户是否允许本地通知。iOS8。Obj-C

来自分类Dev

使用IOS中的CLLocation获取用户当前位置

来自分类Dev

底部共享菜单iOS [Obj-c]

来自分类Dev

未经iOS许可,是否可以获取用户大概位置?

来自分类Dev

HMS Map Kit-群集

来自分类Dev

iOS在Notification Service Extension中获取用户位置

来自分类Dev

在Android Studio中无法获取com.huawei.hms.location.LocationServices for Huawei Map Kit

来自分类Dev

Google Maps iOS SDK,获取用户的当前位置

来自分类Dev

iOS Google Map sdk用户位置图像

来自分类Dev

如何在iOS中使用Map Kit进行自定义注释?

来自分类Dev

iOS Sprite Kit didBeginContact无法正常工作

来自分类Dev

在iOS8中使用Home kit框架设置主页

来自分类Dev

iOS 8 MKMapView用户位置请求失败

来自分类Dev

框架尺寸iOS8 Sprite Kit

来自分类Dev

Xcode Obj-C-从警报框IOS8检索用户输入的文本

来自分类Dev

无法从iOS 8中的Facebook获取用户的电子邮件个人信息

来自分类Dev

iOS:Watch Kit获取当前位置并在地图上显示

来自分类Dev

iOS Map用户位置未显示

来自分类Dev

iOS Sprite Kit BodyWithBodies无法正常工作

来自分类Dev

如何从循环(js)中的 Map(obj) 获取键和值?

Related 相关文章

  1. 1

    iOS7 Map Kit删除默认位置图标

  2. 2

    Google Maps iOS SDK,获取用户的当前位置

  3. 3

    iOS Facebook SDK-获取用户位置

  4. 4

    iOS:使用Sprite Kit实施用户控制的相机“缩放”

  5. 5

    google map ios sdk:无法获取当前用户位置?

  6. 6

    无法获取我的位置以显示在iOS 8的Google地图中

  7. 7

    无法使用CLLocation Manager获取iOS 8中用户的位置?

  8. 8

    物理体边缘IOS8,Sprite Kit

  9. 9

    如何在iOS中使用Twitter Kit / Fabric从登录的用户获取用户配置文件图像

  10. 10

    检查用户是否允许本地通知。iOS8。Obj-C

  11. 11

    使用IOS中的CLLocation获取用户当前位置

  12. 12

    底部共享菜单iOS [Obj-c]

  13. 13

    未经iOS许可,是否可以获取用户大概位置?

  14. 14

    HMS Map Kit-群集

  15. 15

    iOS在Notification Service Extension中获取用户位置

  16. 16

    在Android Studio中无法获取com.huawei.hms.location.LocationServices for Huawei Map Kit

  17. 17

    Google Maps iOS SDK,获取用户的当前位置

  18. 18

    iOS Google Map sdk用户位置图像

  19. 19

    如何在iOS中使用Map Kit进行自定义注释?

  20. 20

    iOS Sprite Kit didBeginContact无法正常工作

  21. 21

    在iOS8中使用Home kit框架设置主页

  22. 22

    iOS 8 MKMapView用户位置请求失败

  23. 23

    框架尺寸iOS8 Sprite Kit

  24. 24

    Xcode Obj-C-从警报框IOS8检索用户输入的文本

  25. 25

    无法从iOS 8中的Facebook获取用户的电子邮件个人信息

  26. 26

    iOS:Watch Kit获取当前位置并在地图上显示

  27. 27

    iOS Map用户位置未显示

  28. 28

    iOS Sprite Kit BodyWithBodies无法正常工作

  29. 29

    如何从循环(js)中的 Map(obj) 获取键和值?

热门标签

归档