Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello, Everybody.

I developed to get speed of car with gps for iphone and tested it many times.
then i didn't have accurate speed and it often was set 0.

My code is below.

- (void) setupLocationManager
{
    self.locationManager = [[[CLLocationManager alloc] init] autorelease];
    if (locationManager.locationServicesEnabled == NO) {
        UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [servicesDisabledAlert show];
        [servicesDisabledAlert release];
    }

    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    self.stateString = NSLocalizedString(@"Updating", @"Updating");
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
	
    // test that the horizontal accuracy does not indicate an invalid measurement
 	if (newLocation.horizontalAccuracy < 0) return;

	// test the age of the location measurement to determine if the measurement is cached
    // in most cases you will not want to rely on cached measurements
	NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;
	
	if ([newLocation speed] < 0.0f) {
[g_Config SetSpeed:0];
[g_Config SetCourseValue];
	}
	else {
		float fSpeed = [newLocation speed];
		[g_Config SetRealSpeed:fSpeed];
		[g_Config SetCourseValue:newLocation.course];
	}
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{
	NSLog(@"didFailWithError");
[g_Cofig SetSpeed:0];
[g_Config SetCourseValue:0];
	
    // The location "unknown" error simply means the manager is currently unable to get the location.
    // We can ignore this error for the scenario of getting a single location fix, because we already have a 
    // timeout that will stop the location manager to save power.
    if ([error code] != kCLErrorLocationUnknown) {
        [self stopUpdatingLocation:NSLocalizedString(@"Error", @"Error")];
    }
}

- (void)stopUpdatingLocation:(NSString *)state {
	self.stateString = state;
	NSLog(@"%@", state);
    [locationManager stopUpdatingLocation];
	locationManager.delegate = nil;
}


Please help me.

If i get correct speed from car, how to set distanceFilter and desiredAccuracy?

thanks .
Posted
Updated 29-Dec-11 8:56am
v2
Comments
[no name] 29-Dec-11 15:02pm    
Format code snippets

1 solution

First off, civilian GPS systems are not accurate enough to be used to reliably calculate an accurate speed. There are also a number of factors that can interfere with the signals, sometimes you are even using cell tower triangulation rather than true GPS.

Secondly, if you are in a car and need accurate speed calculations there is already an app for that. It's called the speedometer and comes preloaded on most vehicles.

Third, not everything requires an iPhone app.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900