MapKit giving wrong lat & lang - iOS 8

柔情痞子 提交于 2019-12-20 05:57:36

问题


When I try to use map in iOS 8, I get below error.

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, but still no luck.

Below is what I did.

Step 1. Have entry in Info.plist

NSLocationWhenInUseUsageDescription - This is test text

Step 2. Updated -(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;
}

Step 3. Prefix.pch

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

Step 4. Delete the app again with below in viewDidLoad.

CLLocationCoordinate2D theCoordinate;
theCoordinate = [self getLocation];

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

Still I get output as

ffffff===0.000===0.000

Note:

I get warning, but that is just for half seconds and disappears and then I get message as

CLLocationCoordinate2D theCoordinate;
theCoordinate = [self getLocation];

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

When I go in setting to check the status for Location, below is what I have.

Any idea why this is happening?

I am testing on iPad 2 iOS 8.0 & simulator iOS 8.0


回答1:


You need to implement the CLLocationManager Delegate ( -(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations ) to get location updates.

Here's a code snippet for your reference:

-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
     CLLocation *currentLocation = [locations lastObject];

     NSLog(@"ffffff===%f===%f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
}

Alternatively, you can implement the Mapkit's following delegate method:

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



回答2:


Not sure what was real problem, but putting below code in viewDidLoad solve problem.

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




回答3:


The requestWhenInUseAuthorization runs asynchronously, so you should not be calling startUpdatingLocation until you get a change in authorization status in your delegate.




回答4:


Yeah! Got the solution, Here is my whole code & things added to make it working. Special thanks to @MBarton for his great help. Also Thanks to @ Vinh Nguyen for investing his precious time in solving my issue.

Added Core Location Framework under Target-> General-> Linked Frameworks & Libraries

Added in .plist file

NSLocationAlwaysUsageDescription

See Screenshot:

In my 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

Then in 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];
}

Ufff...! Got the solution after fighting with many codes since last 5 days...

Referred from here



来源:https://stackoverflow.com/questions/26356558/mapkit-giving-wrong-lat-lang-ios-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!