title: not getting the exact speed when driving a car
body: <p>i`m building an app wich notify the user when getting to the max speed limit "120 km/h"</p>
  
  <p>but i`m not getting the exact speed value when testing the app while driving</p>
  
  <p>lets say i`m driving at 80 km/h speed , the value i get from the app is a range between 60 - 100 sometimes more changing every second.</p>
  
  <p>this is my whole code i`m using</p>
  
  <p>viewController.h</p>
  
  <pre><code>#import &lt;UIKit/UIKit.h&gt;
  #import &lt;CoreLocation/CoreLocation.h&gt;
  #import "CoreLocationController.h"
  #import &lt;MapKit/MapKit.h&gt;
  
  @interface ViewController : UIViewController &lt;CLLocationManagerDelegate,     CoreLocationControllerDelegate&gt; {
  MKMapView *mapview;
  CLLocationManager *locationManager;
  IBOutlet UILabel *speedLabel;
  CoreLocationController *CLController;
  
  int distance;
  int currentSpeed;
  int maxSpeed;
  IBOutlet UILabel *distanceLbl;
  IBOutlet UILabel *maxSpeedLbl;
  IBOutlet UILabel *currentSpeedLbl;
  }
  
  - (IBAction)Getlocation:(id)sender;
  
  @property (nonatomic, retain) IBOutlet UILabel *distanceLbl;
  @property (nonatomic, retain) IBOutlet UILabel *maxSpeedLbl;
  @property (nonatomic, retain) IBOutlet UILabel *currentSpeedLbl;
  @property (retain, nonatomic) IBOutlet UILabel *speedLabel;
  @property (nonatomic, retain) CoreLocationController *CLController;
  @property (retain, nonatomic) IBOutlet MKMapView *mapview;
  @property (strong, nonatomic) IBOutlet CLLocationManager *locationManager;
  </code></pre>
  
  <p>viewController.m</p>
  
  <pre><code>@synthesize distanceLbl,maxSpeedLbl,currentSpeedLbl;
  @synthesize locationManager;
  @synthesize mapview;
  @synthesize speedLabel;
  @synthesize CLController;
  
  - (void)locationChange:(CLLocation *)newLocation:(CLLocation *)oldLocation {
  
      NSTimeInterval difference = [[newLocation timestamp] timeIntervalSinceDate:[oldLocation timestamp]];
  
      double temp_distance = [newLocation getDistanceFrom:oldLocation];
  
      distance += temp_distance;
      distanceLbl.text = [NSString stringWithFormat:@"%.2d",distance];
      currentSpeed = (temp_distance/difference) * (18.0/5.0);
      if (currentSpeed &gt; maxSpeed) {
          maxSpeed = currentSpeed;
          maxSpeedLbl.text = [NSString stringWithFormat:@"%.2d",maxSpeed];
      }
      currentSpeedLbl.text = [NSString stringWithFormat:@"%.2d",currentSpeed];
  }
  
  - (void)locationUpdate:(CLLocation *)location {
      speedLabel.text = [NSString stringWithFormat:@"SPEED: %f", [location speed]];
      self.mapview.showsUserLocation = YES;
  }
  
  - (void)locationError:(NSError *)error {
      speedLabel.text = [error description];
  }
  
  - (IBAction)Getlocation:(id)sender {
      locationManager = [[CLLocationManager alloc] init];
      locationManager.distanceFilter = kCLDistanceFilterNone;
      locationManager.desiredAccuracy = kCLLocationAccuracyBest;
      locationManager.delegate = self;
      [locationManager startUpdatingLocation];
  
      [mapview setMapType:MKMapTypeStandard];
      [mapview setZoomEnabled:YES];
      [mapview setScrollEnabled:YES];
      self.mapview.showsUserLocation = YES;
      MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
      region.center.latitude = locationManager.location.coordinate.latitude;
      region.center.longitude = locationManager.location.coordinate.longitude;
      region.span.longitudeDelta = 0.007f;
      region.span.latitudeDelta = 0.007f;
      [mapview setRegion:region animated:YES];
      [mapview setDelegate:sender];
  }
  
  - (IBAction)setMap:(id)sender {
  
      switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
          case 0:
              mapview.mapType = MKMapTypeStandard;
              break;
          case 1:
              mapview.mapType = MKMapTypeSatellite;
              break;
          case 2:
              mapview.mapType = MKMapTypeHybrid;
              break;
          default:
              break;
      }
  
  }
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
  
      CLController = [[CoreLocationController alloc] init];
      CLController.delegate = self;
      [CLController.locMgr startUpdatingLocation];
  
      [self Getlocation:self];
  
      maxSpeed = 120;
  }
  </code></pre>
  
  <p>CoreLocationController.h</p>
  
  <pre><code>@protocol CoreLocationControllerDelegate
  @required
  
  - (void)locationUpdate:(CLLocation *)location;
  - (void)locationError:(NSError *)error;
  - (void)locationChange:(CLLocation *)newLocation:(CLLocation *)oldLocation;
  
  @end
  
  
  @interface CoreLocationController : NSObject &lt;CLLocationManagerDelegate&gt; {
      CLLocationManager *locMgr;
      id delegate;
  
      //new speed method
      int distance;
      int currentSpeed;
      int maxSpeed;
      IBOutlet UILabel *distanceLbl;
      IBOutlet UILabel *maxSpeedLbl;
      IBOutlet UILabel *currentSpeedLbl;
  }
  
  @property (nonatomic, retain) IBOutlet UILabel *distanceLbl;
  @property (nonatomic, retain) IBOutlet UILabel *maxSpeedLbl;
  @property (nonatomic, retain) IBOutlet UILabel *currentSpeedLbl;
  
  @property (nonatomic, retain) CLLocationManager *locMgr;
  @property (nonatomic, assign) id delegate;
  </code></pre>
  
  <p>CoreLocationController.m</p>
  
  <pre><code>@synthesize locMgr;
  @synthesize delegate = _delegate;
  @synthesize distanceLbl,maxSpeedLbl,currentSpeedLbl;
  
  
  - (id)init {
      self = [super init];
  
      if(self != nil) {
          self.locMgr = [[CLLocationManager alloc] init];
          self.locMgr.delegate = self;
      }
  
      return self;
  }
  
  - (void)locationChange:(CLLocation *)newLocation:(CLLocation *)oldLocation {
  
      NSTimeInterval difference = [[newLocation timestamp] timeIntervalSinceDate:[oldLocation timestamp]];
  
      double temp_distance = [newLocation getDistanceFrom:oldLocation];
  
      distance += temp_distance;
      distanceLbl.text = [NSString stringWithFormat:@"%.2d",distance];
      currentSpeed = (temp_distance/difference) * (18.0/5.0);
      if (currentSpeed &gt; maxSpeed) {
          maxSpeed = currentSpeed;
          maxSpeedLbl.text = [NSString stringWithFormat:@"%.2d",maxSpeed];
      }
      currentSpeedLbl.text = [NSString stringWithFormat:@"%.2d",currentSpeed];
  }
  
  - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
      if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {
          [self.delegate locationUpdate:newLocation];
          [self.delegate locationChange:newLocation :oldLocation];
      }
  }
  
  - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
      if([self.delegate conformsToProtocol:@protocol(CoreLocationControllerDelegate)]) {
          [self.delegate locationError:error];
      }
  }
  </code></pre>