2014-09-28 64 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