1

地図ビューでユーザーの位置を追跡および追跡する方法を教える youtube のチュートリアルを進めています。チュートリアルにはコードのコピーが付属しているので、コード ファイルをダウンロードして Xcode で開きました。Xcode でコードを初めて開いたときは、最新の Xcode 5 を使用していました。場所の検索とトレースは問題なく実行されました。約 1 日後に 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