如何在iOS中找到我的当前位置?

如何在iOS中找到我的当前位置?,ios,mapkit,Ios,Mapkit,我得到了这个错误,这个错误意味着什么。我不明白它是什么错误 所以请帮忙 谢谢 错误 1.ViewController.h #import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> @interface ViewController : UIViewController<MKMapViewDelegate> @property

我得到了这个错误,这个错误意味着什么。我不明白它是什么错误

所以请帮忙

谢谢

错误

1.ViewController.h

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface ViewController : UIViewController<MKMapViewDelegate>

@property (weak, nonatomic) IBOutlet MKMapView *mycurrentlocation;

@end

您需要添加核心位置框架和地图工具包框架

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

NSLocationWhenInUseUsageDescription

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

YourAppName希望使用您的位置

在头文件中。(我使用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;

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];
}
#导入
#进口
@界面YourViewController:UIViewController{
}
@属性(非原子,保留)IBMMapView*mapView;
@财产(非原子,保留)CLLocationManager*locationManager;
YourViewController.m
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后执行任何其他设置。
mapView.delegate=self;
self.locationManager=[[CLLocationManager alloc]init];
self.locationManager.delegate=self;
#ifdef\uuuIphone\u8\u0
如果(是8或更高版本){
//使用一个或另一个,而不是两个。取决于您在info.plist中输入的内容
[self.locationManager在未使用授权时请求];
[self.locationManager请求始终授权];
}
#恩迪夫
[self.locationManager startUpdatingLocation];
mapView.showsUserLocation=是;
[mapView setMapType:MKMapTypeStandard];
[mapView SetZoomeEnabled:是];
[地图视图设置可克隆:是];
}
-(无效)视图显示:(BOOL)动画{
[超级视图显示:是];
self.locationManager.distanceFilter=kCLDistanceFilterNone;
self.locationManager.desiredAccuracy=KCallocationAccuracyBest;
[self.locationManager startUpdatingLocation];
NSLog(@“%@,[自设备定位]);
//视野范围
MKCoordinateRegion={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;
[地图视图设置区域:区域动画:是];
}
-(void)mapView:(MKMapView*)mapView didUpdateUserLocation:(MKUserLocation*)userLocation
{
mkcoordinaereregion=mkcoordinaereregionmakewithdistance(userLocation.coordination,800800);
[self.mapView setRegion:[self.mapView regionatfits:region]已设置动画:是];
}
-(NSString*)设备位置{
返回[NSString stringWithFormat:@“纬度:%f经度:%f”,self.locationManager.location.coordinate.latitude,self.locationManager.location.coordinate.longitude];
}
-(NSString*)deviceLat{
返回[NSString stringWithFormat:@“%f”,self.locationManager.location.coordinate.latitude];
}
-(NSString*)DeviceOn{
返回[NSString stringWithFormat:@“%f”,self.locationManager.location.coordinate.longitude];
}
-(NSString*)设备语言{
返回[NSString stringWithFormat:@“%f”,self.locationManager.location.altitude];
}

然后将这行代码放入类的
viewDidLoad
方法中:

[self.locationManager requestAlwaysAuthorization];

您需要在plist中设置授权密钥。我如何在plist中设置授权密钥?尝试在不提示位置授权的情况下启动MapKit位置更新。必须调用-[CLLocationManager RequestWhenUseAuthorization]或-[CLLocationManager requestAlwaysAuthorization]首先。@ParagBharambe:你的错误是什么?你能发送一个屏幕截图吗?嘿,我已经编辑了我的问题并添加了错误的屏幕截图,打开链接你就会找到屏幕截图。你试过我在上面的答案中写的代码了吗?如果是,那么我的下一个问题是你的x代码版本是什么?如果你在iphone中运行你的代码,那么手机中的ios版本是什么?显然在ios 8 SDK中,requestAlwaysAuthorization(用于后台位置)或RequestWhenUseAuthorization(仅用于前台位置)在开始位置更新之前,需要调用CLLocationManager。在Info.plist中还需要NSLocationAlwaysUsageDescription或NSLocationWhenUsageDescription关键字,并在提示中显示消息。添加这些可以解决您的问题
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
#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];
}
[self.locationManager requestAlwaysAuthorization];