1

我正在浏览 youtube 上的教程,该教程教您如何在地图视图上跟踪和跟踪用户的位置。本教程附带代码副本,因此我下载了代码文件并在 Xcode 中打开它们。我第一次在 Xcode 中打开代码时,我有最新的 Xcode 5。它运行得很好,可以找到和跟踪位置。大约一天后 Xcode 6 发布了,所以我将 Xcode 更新为 Xcode 6。在 Xcode 6 中打开代码文件时,应用程序无法正确执行。我收到一个错误提示...

2014-09-28 17:24:34.468 GPSTrack[1644:130866] Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

在头文件 GPSTrackerViewController.h

//
//  GPSTrackViewController.h
//  GPSTrack
//
//  Created by Nick Barrowclough on 4/21/14.
//  Copyright (c) 2014 iSoftware Developers. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h> //import the mapkit framework

@interface GPSTrackViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, MKOverlay> {

    CLLocationManager *lm; //core lcoation manager instance

    NSMutableArray *trackPointArray; //Array to store location points

    //instaces from mapkit to draw trail on map
    MKMapRect routeRect;
    MKPolylineView* routeLineView;
    MKPolyline* routeLine;
}
- (IBAction)startTracking:(id)sender;
- (IBAction)stopTracking:(id)sender;
- (IBAction)clearTrack:(id)sender;

@property (weak, nonatomic) IBOutlet MKMapView *mapview;

@end

GPSTrackViewController.m

//
//  GPSTrackViewController.m
//  GPSTrack
//
//  Created by Nick Barrowclough on 4/21/14.
//  Copyright (c) 2014 iSoftware Developers. All rights reserved.
//

#import "GPSTrackViewController.h"

@interface GPSTrackViewController ()

@end

@implementation GPSTrackViewController

@synthesize mapview;

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

    mapview.mapType = MKMapTypeHybrid;
}

- (void)viewWillAppear:(BOOL)animated {
    trackPointArray = [[NSMutableArray alloc] init];
}

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

- (IBAction)startTracking:(id)sender {
    //start location manager
    lm = [[CLLocationManager alloc] init];
    lm.delegate = self;
    lm.desiredAccuracy = kCLLocationAccuracyBest;
    lm.distanceFilter = kCLDistanceFilterNone;
    [lm startUpdatingLocation];

    mapview.delegate = self;
    mapview.showsUserLocation = YES;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

    //get the latest location
    CLLocation *currentLocation = [locations lastObject];

    //store latest location in stored track array;
    [trackPointArray addObject:currentLocation];

    //get latest location coordinates
    CLLocationDegrees Latitude = currentLocation.coordinate.latitude;
    CLLocationDegrees Longitude = currentLocation.coordinate.longitude;
    CLLocationCoordinate2D locationCoordinates = CLLocationCoordinate2DMake(Latitude, Longitude);

    //zoom map to show users location
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(locationCoordinates, 1000, 1000);
    MKCoordinateRegion adjustedRegion = [mapview regionThatFits:viewRegion]; [mapview setRegion:adjustedRegion animated:YES];

    NSInteger numberOfSteps = trackPointArray.count;

    CLLocationCoordinate2D coordinates[numberOfSteps];
    for (NSInteger index = 0; index < numberOfSteps; index++) {
        CLLocation *location = [trackPointArray objectAtIndex:index];
        CLLocationCoordinate2D coordinate2 = location.coordinate;

        coordinates[index] = coordinate2;
    }

    MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
    [mapview addOverlay:polyLine];

    //NSLog(@"%@", trackPointArray);
}

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
    MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
    polylineView.strokeColor = [UIColor redColor];
    polylineView.lineWidth = 4.0;

    return polylineView;
}

- (IBAction)stopTracking:(id)sender {
    //reset location manager and turn off GPS
    lm = [[CLLocationManager alloc] init];
    [lm stopUpdatingLocation];
    lm = nil;

    //stop shwing user location
    mapview.showsUserLocation = NO;

    //reset array fo tracks
    trackPointArray = nil;
    trackPointArray = [[NSMutableArray alloc] init];
}

- (IBAction)clearTrack:(id)sender {
    //remove overlay on mapview
    [mapview removeOverlays: mapview.overlays];
}


@end

有人可以帮我理解为什么应用程序不再运行,并给我一些建议,告诉我我需要做些什么来让它重新启动并运行。

4

1 回答 1

1

在基本 sdk 8 下(这是 xcode 6 使用的,我删除了 xcode 标签,因为它不是 IDE 特定的)你必须首先请求授权并且必须有一个 plist 密钥(一个字符串说明你为什么需要使用 GPS)

plist 键取决于您的需要是

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

这个 SO 问题看起来不错:

IOS 8 CLLocationManager 问题(授权不起作用)

有关详细说明,我可以推荐(略读):

http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

于 2014-09-28T22:41:50.510 回答