Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 12-30-2008, 07:06 PM   #27 (permalink)
zword
Registered Member
 
Join Date: Nov 2008
Posts: 34
zword is on a distinguished road
Default calculating current speed

I'm still trying to figure out how to calculate the current speed of my application. From my point of view the calculation should be correct but now I only get values of 0.0 or slightly above.
I tried it with my car when I drove about 50km/h. I would be very grateful if somebody could help me.

Here is my code:

Code:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
		
		NSLog(@"Updating location");
		
		NSDate *newLocationEventDate = newLocation.timestamp;
		//no pointer because it is a C-Class
		NSTimeInterval howRecentNewLocation = [newLocationEventDate timeIntervalSinceNow];
	
	//initial Update, no oldLocation available therefore it won't be checked how good the data is 
	if(firstUpdate) {
		
		if (signbit(newLocation.horizontalAccuracy)) {
			// Negative accuracy means an invalid or unavailable measurement
			NSLog(@"LatLongUnavailable");
			UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Error Alert" message:@"LatLongUnavailable" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"exit", nil];
			[myAlert show];
			[myAlert release];
		}
		else{
			myLocation = [newLocation retain];
			[self updatedGPS:newLocation];
			firstUpdate = NO;
			[mySpinner stopAnimating];
			isUpdating = NO; 
			return;
		}
	}
		
	if(isUpdating){
		if (signbit(newLocation.horizontalAccuracy)) {
			// Negative accuracy means an invalid or unavailable measurement
			NSLog(@"LatLongUnavailable");
			UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Error Alert" message:@"LatLongUnavailable" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"exit", nil];
			[myAlert show];
			[myAlert release];
		}
		else{
			timesFired++;
			//filter cached and too old locations
			//1.Version:
			//if((myLocation.horizontalAccuracy > newLocation.horizontalAccuracy) && (howRecentNewLocation < -0.0 && howRecentNewLocation > -10.0)) {
			//2.Version
			if(howRecentNewLocation < -0.0 && howRecentNewLocation > -10.0){
				//release the cached location, if available
				if(myLocation)
					[myLocation release];
				myLocation =[newLocation retain];
			}
			
				[self updatedGPS:myLocation];
				timesFired = 0;
				[mySpinner stopAnimating];
				return;
			}
		}
	}
}
  

- (void)updatedGPS:(CLLocation *)goodLocation{
	
	//methode variables for calculating the current and avg speed
	CLLocationDistance distanceMoved;
	NSTimeInterval timeElapsed;
	
	
	NSLog(@"uptadedGPS entered:");
	NSLog(@"Lat: %f", goodLocation.coordinate.latitude);
	NSLog(@"Long: %f", goodLocation.coordinate.longitude);
	
	//calculate distance moved and time elapsed, but only if we have an "old" location
	if(oldLocation != nil) {
		distanceMoved = [goodLocation getDistanceFrom:oldLocation];
		timeElapsed = [goodLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
	}
	else{
		distanceMoved = 0.0;
		timeElapsed = 1.0;
	}
	
	//formatting to km/h
	currentSpeed = (distanceMoved*0.001)/(timeElapsed*360);
	currentSpeedLabel.text = [NSString stringWithFormat:@"%3.1f", currentSpeed];
	NSLog(@"currentSpeed: %f", currentSpeed);
	}
	//save the latest goodLocation for calculating the speed
	oldLocation = [goodLocation retain];
	
	
}
zword is offline   Reply With Quote
 

» Advertisements
» Online Users: 422
16 members and 406 guests
Alexanskoo, aziz, diane21, elhanche, freewind, guillermotricia, hrushikeshaddon, iram91418, nicsmotherman, pipposanta, PixelEnvision, poke, QuantumDoja, roelien, ryantcb, subhan1
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,609
Threads: 94,084
Posts: 402,783
Top Poster: BrianSlick (7,990)
Welcome to our newest member, diane21
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:49 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.