Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate the current speed and average speed of user travelling from current location to particular locaction in iPhone? [closed]

I am having a map application. When a person travels from current location to particular location I want to calculate the distance covered by the person, the current speed of the person, average speed of the person.

like image 980
Rani Avatar asked May 13 '11 07:05

Rani


People also ask

How do I calculate my average journey speed?

Divide the total distance traveled by the total time spent traveling. This will give you your average speed. . So if Ben traveled 150 miles in 3 hours, 120 miles in 2 hours, and 70 miles in 1 hour, his average speed was about 57 mph.

How do you calculate average speed and distance?

That is speed = distance ÷ time. Or to put it another way distance divided by speed will give you the time. Provided you know two of the inputs you can work out the third. For example if a car travels for 2 hours and covers 120 miles we can work out speed as 120 ÷ 2 = 60 miles per hour.

How do you calculate someones average speed?

You can find the average speed of an object if you know the distance travelled and the time it took. The formula for speed is speed = distance ÷ time. To work out what the units are for speed, you need to know the units for distance and time.


2 Answers

Use following methods to get distance in miles or kilometers. You have to declare timer in your header file synthesize it.

//SpeedViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MobileCoreServices/UTCoreTypes.h>

@interface SpeedViewController : UIViewController <CLLocationManagerDelegate,UINavigationControllerDelegate>
{
    CLLocationManager *locManager;
    CLLocationSpeed speed;
    NSTimer *timer;

    CLLocationSpeed currentSpeed;
    float fltDistanceTravelled; 
}

@property (nonatomic,retain) NSTimer *timer;

-(float)getDistanceInKm:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
-(float)getDistanceInMiles:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;

@end

//SpeedViewController.h
#import "SpeedViewController.h"
#define kRequiredAccuracy 500.0 //meters
#define kMaxAge 10.0 //seconds
#define M_PI   3.14159265358979323846264338327950288   /* pi */


@implementation SpeedViewController

@synthesize timer;
- (void)startReadingLocation {
    [locManager startUpdatingLocation];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    CLLocationManager *locationManager=[[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
    [locationManager startUpdatingLocation];
}
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    NSLog(@"new->%d old->%d",(newLocation==NULL),(oldLocation==NULL));

    if(newLocation && oldLocation)
    {
        fltDistanceTravelled +=[self getDistanceInKm:newLocation fromLocation:oldLocation];
    }
}

//this is a wrapper method to fit the required selector signature
- (void)timeIntervalEnded:(NSTimer*)timer {
    fltDistanceTravelled=0;
    [self startReadingLocation];
}


-(float)getDistanceInKm:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    float lat1,lon1,lat2,lon2;

    lat1 = newLocation.coordinate.latitude  * M_PI / 180;
    lon1 = newLocation.coordinate.longitude * M_PI / 180;

    lat2 = oldLocation.coordinate.latitude  * M_PI / 180;   
    lon2 = oldLocation.coordinate.longitude * M_PI / 180;

    float R = 6371; // km
    float dLat = lat2-lat1;
    float dLon = lon2-lon1; 

    float a = sin(dLat/2) * sin(dLat/2) + cos(lat1) * cos(lat2) * sin(dLon/2) * sin(dLon/2); 
    float c = 2 * atan2(sqrt(a), sqrt(1-a)); 
    float d = R * c;

    NSLog(@"Kms-->%f",d);

    return d;
}

-(float)getDistanceInMiles:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    float lat1,lon1,lat2,lon2;

    lat1 = newLocation.coordinate.latitude  * M_PI / 180;
    lon1 = newLocation.coordinate.longitude * M_PI / 180;

    lat2 = oldLocation.coordinate.latitude  * M_PI / 180;   
    lon2 = oldLocation.coordinate.longitude * M_PI / 180;

    float R = 3963; // km
    float dLat = lat2-lat1;
    float dLon = lon2-lon1; 

    float a = sin(dLat/2) * sin(dLat/2) + cos(lat1) * cos(lat2) * sin(dLon/2) * sin(dLon/2); 
    float c = 2 * atan2(sqrt(a), sqrt(1-a)); 
    float d = R * c;

    NSLog(@"Miles-->%f",d);

    return d;
}

@end

I have not tested this on device but logically it should work.

like image 178
Janak Nirmal Avatar answered Oct 25 '22 16:10

Janak Nirmal


In order to determine any speed, you'll want to figure:

Distance: Take your two latest geographic coordinate readings (and the time each was recorded) to calculate the distance between them

To do this, you can read about theory here, or you can check out code here.

Speed: Using the distance between the latest GPS coordinate reading and the previous, calculate the speed by the formula speed = distance/duration. So, the distance you found between the two geo coordinates divided by the duration in seconds or minutes or whatever. Then your answer will be speed is x miles or kilometers per whatever.

like image 32
Todd Hopkinson Avatar answered Oct 25 '22 16:10

Todd Hopkinson