14

Xcode 6.3.1 で Core Location Framework を使用してユーザーの現在の場所を取得しようとしています。次のことを行いました。

  1. Target -> General -> Linked Frameworks & Librariesの下にCore Location Frameworkを追加
  2. 私のViewController.hファイルは次のとおりです。

    #import <UIKit/UIKit.h>
    #import <CoreLocation/CoreLocation.h>
    
    @interface ViewController : UIViewController<CLLocationManagerDelegate>
    @property (weak, nonatomic) IBOutlet UILabel *lblLatitude;
    @property (weak, nonatomic) IBOutlet UILabel *lblLongitude;
    @property (weak, nonatomic) IBOutlet UILabel *lblAddress;
    @property (strong, nonatomic) CLLocationManager *locationManager;
    
    @end
    
  3. 私の ViewController.m ファイルは次のとおりです。

    - (void)viewDidLoad
    {
    
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    
    self.locationManager.delegate = self;
    if(IS_OS_8_OR_LATER){
        NSUInteger code = [CLLocationManager authorizationStatus];
        if (code == kCLAuthorizationStatusNotDetermined && ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {
        if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
            [self.locationManager requestAlwaysAuthorization];
        } else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
            [self.locationManager  requestWhenInUseAuthorization];
        } else {
            NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
        }
    }
    }
    [self.locationManager startUpdatingLocation];
    }
    
    #pragma mark - CLLocationManagerDelegate
    
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
        NSLog(@"didFailWithError: %@", error);
        UIAlertView *errorAlert = [[UIAlertView alloc]
                           initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [errorAlert show];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
        NSLog(@"didUpdateToLocation: %@", newLocation);
        CLLocation *currentLocation = newLocation;
    
        if (currentLocation != nil) {
            lblLatitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
            lblLongitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
        }
    }
    @end
    

info.plist ファイルに次のキーも追加しました

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

plist画像

herehereherehere、およびその他のリストのすべてを確認しました

それで、この問題の解決策を持っている人はいますか、親切に助けてください。一日中これを探して頭がおかしくなりました。

4

1 に答える 1

6

うん!解決策を手に入れました。これが私のコード全体と、それを機能させるために追加されたものです。@MBarton の大きな助けに感謝します。また、私の問題を解決するために貴重な時間を割いてくれた @ Vinh Nguyen にも感謝します。

Target-> General-> Linked Frameworks & Librariesの下にCore Location Frameworkを追加しました

.plist ファイルに追加

NSLocationAlwaysUsageDescription

スクリーンショットを参照してください:

plistのスクリーンショット

私の中で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) {
    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) { 
        // 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日間から多くのコードと戦った後、解決策を得ました...

于 2015-05-22T06:30:03.883 に答える