简体   繁体   中英

User Location Will Not display on mapView

I am creating a simple UIMapView with custom annotations. Just upgraded to Yosemite and Xcode 6.1.1. I have tried everything to get user location to show on map but for some reason the blue dot doesn't show on the map. I have selected show user location in the inspector for the UIMapView. The error listed at the end shows in the log window at the bottom of the screen when I run it on my phone. Thank you for any help.

My .h file

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


@interface ViewController : UIViewController <MKMapViewDelegate>

@property (strong, nonatomic) IBOutlet MKMapView *mapView;

My .m file

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.mapView.delegate self];
    [self.mapView setShowsUserLocation:YES];
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 500, 500);
    [self.mapView setRegion:region animated:YES];

}

ERROR RECEIVED AFTER RUNNING 501:107687] Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

As per my comments on the question... you are in a Permission error situation. You need t check for User Permission before you call the update methods...

CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];

if (authorizationStatus == kCLAuthorizationStatusAuthorized ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {

    // update location here...      

}

Then, as pointed out by @zaheer, you need to add a Key-Value par to the -Info.plist

NSLocationWhenInUseUsageDescription Some Message To The User About Why You Want Their Location

or

NSLocationAlwaysUsageDescription Some Witty Comment About The NSA

depending upon whether you need the location only when the app is in the foreground, or in the background...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM