【问题标题】:MapKit giving wrong lat & lang - iOS 8MapKit 给出错误的纬度和纬度 - iOS 8
【发布时间】:2014-12-08 23:54:14
【问题描述】:

当我尝试在 iOS 8 中使用地图时,出现以下错误。

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

For this I tried solution from here,但还是没有运气。

以下是我所做的。

步骤 1. 在 Info.plist 中有条目

NSLocationWhenInUseUsageDescription - This is test text

步骤 2. 更新 -(CLLocationCoordinate2D) getLocation

-(CLLocationCoordinate2D) getLocation{
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    // this is change
    if(IS_OS_8_OR_LATER) {
        [locationManager requestWhenInUseAuthorization];
    }
    [locationManager startUpdatingLocation];


    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];

    return coordinate;
}

步骤 3. Prefix.pch

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

步骤 4. 在 vi​​ewDidLoad 中再次删除应用程序。

CLLocationCoordinate2D theCoordinate;
theCoordinate = [self getLocation];

NSLog(@"ffffff===%f===%f", theCoordinate.latitude, theCoordinate.longitude);

我仍然得到输出为

ffffff===0.000===0.000

注意:

我收到警告,但这只是半秒钟然后消失,然后我收到消息

CLLocationCoordinate2D theCoordinate;
theCoordinate = [self getLocation];

NSLog(@"ffffff===%f===%f", theCoordinate.latitude, theCoordinate.longitude);

当我进入设置检查位置状态时,以下是我所拥有的。

知道为什么会这样吗?

我正在 iPad 2 iOS 8.0 和模拟器 iOS 8.0 上进行测试

【问题讨论】:

    标签: objective-c ios8 mapkit cllocationmanager cllocation


    【解决方案1】:

    是的!得到了解决方案,这是我的整个代码和添加的东西以使其正常工作。特别感谢@MBarton 的大力帮助。也感谢@Vinh Nguyen 投入宝贵的时间来解决我的问题。

    Target->General->Linked Frameworks & Libraries

    下添加了Core Location Framework

    添加到 .plist 文件中

    NSLocationAlwaysUsageDescription
    

    查看截图:

    在我的ViewController.h

    #import <UIKit/UIKit.h>
    #import <MapKit/MapKit.h>
    #import <MapKit/MKAnnotation.h>
    
    #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    
    @interface ViewController : UIViewController  <MKMapViewDelegate,  CLLocationManagerDelegate>
    {
        __weak IBOutlet UINavigationItem *navigationItem;
    }
    
     @property (weak, nonatomic) IBOutlet MKMapView *mapView;
     @property(nonatomic, retain) CLLocationManager *locationManager;
    
    @end
    

    然后在ViewController.m

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    @synthesize mapView;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    
        [self setUpMap];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    -(void)setUpMap
    {
        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 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];
    }
    

    噗……!自过去 5 天以来与许多代码战斗后得到了解决方案...

    Referred from here

    【讨论】:

      【解决方案2】:

      requestWhenInUseAuthorization 异步运行,因此在您的委托中的授权状态发生变化之前,您不应调用 startUpdatingLocation

      【讨论】:

        【解决方案3】:

        不确定什么是真正的问题,但将下面的代码放入viewDidLoad 解决问题。

        mapView.delegate = self;
        locationManager.delegate = self;
        self.locationManager = [[CLLocationManager alloc] init];
        #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];
        }
        #endif
        [self.locationManager startUpdatingLocation];
        
        mapView.showsUserLocation = YES;
        [mapView setMapType:MKMapTypeStandard];
        [mapView setZoomEnabled:YES];
        [mapView setScrollEnabled:YES];
        

        Reference

        【讨论】:

          【解决方案4】:

          您需要实现 CLLocationManager 委托 ( -(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations ) 来获取位置更新。

          这里有一个代码 sn-p 供你参考:

          -(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
          {
               CLLocation *currentLocation = [locations lastObject];
          
               NSLog(@"ffffff===%f===%f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
          }
          

          或者,您可以实现 Mapkit 的以下委托方法:

          - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
          {
              MKUserLocation *myLocation = userLocation;
              NSLog(@"ffffff===%f===%f", myLocation.coordinate.latitude, myLocation.coordinate.longitude);
          }
          

          【讨论】:

          • 也这样做了..还是一样....我找到了解决方案,现在将作为答案发布...
          猜你喜欢
          • 1970-01-01
          • 2016-11-14
          • 1970-01-01
          • 1970-01-01
          • 2018-09-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多