【问题标题】:How to locate my current location in iOS?如何在 iOS 中找到我当前的位置?
【发布时间】:2016-03-28 19:30:08
【问题描述】:

我得到这个错误是什么意思。我不明白它是什么错误。

请大家帮忙

谢谢

错误

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

2。 ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController @synthesize mycurrentlocation;

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

    self.mycurrentlocation.delegate = self; }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated. }

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
    MKCoordinateRegion re = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800,
800);
    [self.mycurrentlocation setRegion:[self.mycurrentlocation regionThatFits:re] animated:YES];

    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = userLocation.coordinate;
    point.title = @"Where am I?";
    point.subtitle = @"I'm here!!!";

    [self.mycurrentlocation addAnnotation:point]; }

@end

【问题讨论】:

标签: ios mapkit


【解决方案1】:

然后把这行代码放到你类的viewDidLoad方法中:

[self.locationManager requestAlwaysAuthorization];

【讨论】:

    【解决方案2】:

    您需要添加核心位置框架和 Map Kit 框架。

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

    NSLocationWhenInUseUsageDescription
    

    或者

    NSLocationAlwaysUsageDescription
    

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

    YourAppName 想使用您的位置。

    在您的头文件中。 (我使用 App Name-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];
    }
    

    【讨论】:

    • 尝试在不提示位置授权的情况下启动 MapKit 位置更新。必须先调用 -[CLLocationManager requestWhenInUseAuthorization] 或 -[CLLocationManager requestAlwaysAuthorization]。
    • @ParagBharambe:您的错误是什么,您可以发送它的屏幕截图。
    • 嘿,我已经编辑了我的问题并添加了错误的屏幕截图,只需打开该链接你就会找到屏幕截图
    • 您是否尝试过我在上面的答案中编写的代码?如果是,那么我的下一个问题是您的 x 代码版本是什么,如果您在 iphone 中运行代码,那么您在手机中的 ios 版本是什么?
    • 显然在 iOS 8 SDK 中,在开始位置更新之前,需要对 CLLocationManager 调用 requestAlwaysAuthorization(用于后台位置)或 requestWhenInUseAuthorization(仅在前台时定位)。 Info.plist 中还需要有 NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseUsageDescription 键,并在提示中显示一条消息。添加这些可能会解决您的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多