2014-09-28 69 views
1

我正在瀏覽YouTube上的一個教程,教您如何跟蹤和跟蹤用戶在地圖視圖上的位置。該教程附帶了一份代碼,因此我下載了代碼文件並在Xcode中打開它們。我第一次在Xcode中打開代碼時,我使用了最新的Xcode 5.它能夠很好地查找並追蹤位置。大約一天之後,Xcode 6出現了,所以我將Xcode更新爲Xcode 6.當在Xcode 6中打開代碼文件時,應用程序不會正確預製。我得到一個錯誤,指出...在Xcode 5中工作後,Xcode中的GPS跟蹤在Xcode 5中停止工作

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 

是否有人可以幫助我理解爲什麼應用程序不再運行,並給我一些建議,說明我需要做些什麼來重新啓動它。

回答

1

在基準sdk 8下(這是xcode 6使用的,我刪除了xcode標籤,因爲它不是IDE特定的),你必須首先要求授權,並且必須有一個plist鍵(一個字符串說明你爲什麼需要使用GPS)

plist中關鍵要看你的需求或者是

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

這太問題看起來不錯:

IOS 8 CLLocationManager Issue (Authorization Not Working)

詳細的指示,我可以推薦(脫脂吧):

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

+0

@妲己,Djan對不起這是一個錯誤。我不小心下了你的問題,現在堆棧不允許我進行投票。 – SARATH 2015-04-08 12:16:40

+0

不用擔心。謝謝你讓我知道 – 2015-04-08 19:07:31