位置管理器的问题

时间:2014-02-04 22:24:43

标签: objective-c location

我多年来一直坚持这个问题而且我确定原因是我对Objective-c了解不够。我刚刚开始使用编码!

无论如何,我正在尝试设计一个跟踪当前行进距离的应用程序,并显示使用MKPolyLine拍摄的路线。我现在已经对MKPolyLine进行了排序,但我遇到的问题是获得当前距离。我认为代码还可以,但我认为我正在做一些与位置管理员有趣的事情。

我做了一些调试,发现我的第二个locationManager方法没有被调用。

以下是下面的代码,如果有人可以快速查看并直接指向我,那将非常感激。

非常感谢。

.m:

@interface StartCycleViewController ()

@property (nonatomic, strong) CLLocationManager *locationManager;

@property (strong, nonatomic) IBOutlet MKMapView *mapView;

@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) CLLocationManager *distanceManager;



@end

@implementation StartCycleViewController

static double totalDistanceBetween;
@synthesize cycleLocation = _cycleLocation;
@synthesize currentCycleLocation = _currentCycleLocation;
@synthesize mapView;



- (void)viewDidLoad
{
    [super viewDidLoad];
    [self startCycleLocation];
    [mapView setDelegate:self];
    self.locations = [NSMutableArray array];
}


- (void)dealloc
{
    self.locationManager.delegate = nil;
}


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





#pragma mark - startCycleLocation

- (void)startCycleLocation{

    if (!_cycleLocation){
        _cycleLocation = [[CLLocationManager alloc]init];
        _cycleLocation.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        _cycleLocation.distanceFilter = 5;
        _cycleLocation.delegate = self;
        _startLocation = nil;

    }
    [_cycleLocation startUpdatingLocation];

}



- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"%@",error);

    if ( [error code] != kCLErrorLocationUnknown ){
        [self stopLocationManager];
    }
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *location = [locations lastObject];

    if (location.horizontalAccuracy < 0)
        return;

    [self.locations addObject:location];
    NSUInteger count = [self.locations count];

    if (count > 1) {
        // only both to add new location if distance has changed by more than 5 meters

        [self.locations addObject:location];
        count = [self.locations count];

        CLLocationCoordinate2D coordinates[count];
        for (NSInteger i = 0; i < count; i++) {
            coordinates[i] = [(CLLocation *)self.locations[i] coordinate];
        }

        MKPolyline *oldPolyline = self.polyline;
        self.polyline = [MKPolyline polylineWithCoordinates:coordinates count:count];
        [self.mapView addOverlay:self.polyline];
        if (oldPolyline)
            [self.mapView removeOverlay:oldPolyline];



    }
    NSLog(@"didUpdateToLocation2: %@", location);
}

- (void) stopLocationManager {
    [self.cycleLocation stopUpdatingLocation];
}

#pragma mark - MKMapViewDelegate

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{

    NSLog(@"PolyLine Started");
    if ([overlay isKindOfClass:[MKPolyline class]])
    {

        MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
        [self.mapView setVisibleMapRect:overlay.boundingMapRect animated:YES];
        renderer.strokeColor = [[UIColor redColor] colorWithAlphaComponent:0.7];
        renderer.lineWidth   = 3;

        return renderer;
        NSLog(@"MKPolyline Rendering");
    }

    return nil;
}

- (IBAction)stopActivity:(id)sender {
    [self stopLocationManager];
    NSLog(@"Stopped");
}

-(void)distanceManager:(CLLocationManager *)manager didUpdateLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
      NSLog(@"Current Distance Started");
    if (self.startLocation == nil)
    {
        totalDistanceBetween = 0;
        _startLocation = newLocation;
    }
    CLLocationDistance distanceBetween = [newLocation distanceFromLocation:_startLocation ];
    self.startLocation = newLocation;
    totalDistanceBetween += distanceBetween;
    NSString *cycleDistanceString = [[NSString alloc]
                                     initWithFormat:@"%f",
                                     totalDistanceBetween];
    _CurrentDistance.text = cycleDistanceString;
    NSLog(@"cycleDistance is %@", cycleDistanceString);

}


@end

.h:

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <AVFoundation/AVFoundation.h>

@interface StartCycleViewController : UIViewController <NSXMLParserDelegate, CLLocationManagerDelegate, MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (nonatomic,strong) CLLocation *currentCycleLocation;
@property (nonatomic,strong) CLLocationManager *cycleLocation;
@property (nonatomic,strong) CLLocation *startLocation;
@property (strong, nonatomic) IBOutlet UILabel *CurrentDistance;


- (IBAction)stopActivity:(id)sender;
@property(nonatomic, strong) MKPolyline *polyline;
@property (nonatomic,strong) NSMutableArray *locations;
@property (nonatomic,strong) NSNumber *currentSpeed;
@property (nonatomic,strong) NSMutableArray *locationHistory;

- (void) stopLocationManager;



@end

0 个答案:

没有答案