Objective c iOS 8地图工具包Obj-C无法获取用户位置
我使用的是iOS 8中的地图工具包,使用的是Obj-C而不是SWIFT。我无法获取设备位置,它被设置为0.00,0.00,我收到错误信息:Objective c iOS 8地图工具包Obj-C无法获取用户位置,objective-c,mapkit,cllocationmanager,ios8,Objective C,Mapkit,Cllocationmanager,Ios8,我使用的是iOS 8中的地图工具包,使用的是Obj-C而不是SWIFT。我无法获取设备位置,它被设置为0.00,0.00,我收到错误信息: Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlw
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
我实现了:(我一次只尝试了一个,没有运气)
在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];
迈克
**编辑:查看下面的答案。您的代码看起来不错,尽管您不需要调用RequestWhenUseAuthorization和其他requestAlwaysAuthorization,请选择一个您需要的 显示位置的代码还没有分配locationManager,不要期望立即获得位置数据 您需要等待,直到调用委托方法:
-(无效)位置管理器:(CLLocationManager*)管理器更新位置:(NSArray*)位置
,则self.locationManager.location也将被设置。我让它工作起来了。我在下面发布了我的代码,以帮助其他有问题的人 这是我的完整代码,可以让MapKit地图视图在iOS 8中工作 在您的AppName-Info.plist中 添加键名为的新行:
NSLocationWhenInUseUsageDescription
或
值是要显示的消息的字符串:
YourAppName would like to use your location.
在头文件中。(我使用App Name-Prefix.pch,但YourViewController.h也可以)
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;
享受吧
--迈克试试这个:
(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusAuthorizedWhenInUse) {
self.mapView.showsUserLocation = YES;
}
在Mikes的回答之后,我发现使用
[self.locationManager requestwhenUseAuthorization]
和[self.locationManager requestAlwaysAuthorization]代码>如他的代码中所示不起作用。您应该只使用一个
我假设API的最新/稳定版本做了一些进一步的更改。它没有写在任何地方,但是如果你的应用程序以MapKit启动,即使在实现MBarton的回答后,你仍然会收到错误消息“尝试启动MapKit位置更新而不提示位置授权”。为了避免这种情况,您必须在MapKit之前创建一个新的视图控制器,并在那里实现位置管理器委托。我把它叫做授权控制器
因此,在AuthorizationController.h中:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MCIAuthorizationController : UIViewController <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
我也有同样的问题,但在plist文件中添加这两行解决了我的问题
NSLocationWhenInUseUsageDescription
及
注意:必须提供这两个值的字符串描述。您可以在控制器文件中使用它们中的任何一个,如下所示
self.locationManager= [[CLLocationManager alloc] init];
self.locationManager.delegate=self;
[self.locationManager requestAlwaysAuthorization];
您必须在控制器中实现CLLOcationManagerDelegate
才能访问此功能以扩展可接受的答案,如果您仅使用上述功能创建一个示例项目,那么除了CoreLocation
和Mapkit
框架之外,您可能需要添加UIKit
,Foundation
和CoreGraphics
框架以及在Xcode 6
实际上,我正在学习关于位置和地图视图的CS193P第16课,而我无法应用视频中的内容,使位置管理器在iOS 8中工作。
看看你的答案,我可以让它工作
Info.plist按照答案中的描述进行了修改(我使用NSLocationWhenUsageDescription)
在AddPhotoViewController.hn中添加了定义:
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
self.locationErrorCode = error.code;
}
在AddPhotoViewController.m中,在ViewDidLoad中添加了以下代码(在self.image之后):
在您第一次启动应用程序时,只会请求一次授权
以下内容也添加到AddPhotoViewController.h中,因为在第16课中没有提到:
@property (nonatomic) NSInteger locationErrorCode;
shouldPerformSegueWithIdentifier已修改为包含else,如果(!self.location):
添加了DIDROR:
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
self.locationErrorCode = error.code;
}
我也不能让cl正常工作。有了这个beta版(感觉像是alpha版),如果它第n次在ios7上运行,我不认为你应该受到责备:你不应该期望locationManager.location在调用startUpdatingLocation后总是有有效的值。您必须实现didUpdateLocations委托方法并在那里处理位置。别忘了将locationManager.delegate设置为self,否则将不会调用delegate方法。@MBarton Previor ios 8对我来说已经足够了uimapkit.showsUserLocation=YES;和mapkit代表。现在在ios 8中,我应该声明CLLocationManager以请求足够的自动化吗?或者有没有办法强迫mapkit请求足够的权限?你是怎么做到的?@nyekimov据我所知,你需要打电话给CLLocationManager请求授权。@nyekimov你有没有找到一种方法在mapViews locationManager上调用requestAlwaysAuthorization,而不必自己创建locationManager实例?我确信有一种方法可以正确地为MKMapViews执行此操作。不需要使用is\u IOS\u 8\u或\u LATER
宏。只需检查requestwhenuseauthorization
(或requestAlwaysAuthorization
)方法是否存在。他在这些调用的正上方放置了一条注释,以选择其中一个,而不是同时调用两个。自我发表文章以来,该方法已被编辑。。也许要加上这句话。一开始肯定没有。我在网站上找到了一个初学者级别的描述,你可以在网站上得到更多的描述,这是一个非常好的主意。或者在设置mapView.showsUserLocation=YES
之前检查[CLLocationManager授权状态]
。这也有助于消除警告。您不需要额外的控制器,在请求验证并添加plist密钥后,消息将消失。在用户响应授权请求后,消息将消失。因此,在第二次使用该应用程序时,该消息不再出现。但是在用户第一次没有回复时,消息会不断出现。在设置mapView.showsUserLocation=YES之前检查[CLLocationManager authorizationStatus]也是不够的。我相信这个信息是由我触发的
self.locationManager= [[CLLocationManager alloc] init];
self.locationManager.delegate=self;
[self.locationManager requestAlwaysAuthorization];
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER)
{
[self.locationManager requestWhenInUseAuthorization];
}
#endif
@property (nonatomic) NSInteger locationErrorCode;
else if (![self.titleTextField.text length])
{
[self alert:@"Title required"];
return NO;
}
else if (!self.location)
{
switch (self.locationErrorCode)
{
case kCLErrorLocationUnknown:
[self alert:@"Couldn't figure out where this photo was taken (yet)."]; break;
case kCLErrorDenied:
[self alert:@"Location Services disabled under Privacy in Settings application."]; break;
case kCLErrorNetwork:
[self alert:@"Can't figure out where this photo is being taken. Verify your connection to the network."]; break;
default:
[self alert:@"Cant figure out where this photo is being taken, sorry."]; break;
}
return NO;
}
else
{ // should check imageURL too to be sure we could write the file
return YES;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
self.locationErrorCode = error.code;
}