如何计算iPhone中用户从当前位置到特定位置的当前速度和平均速度?

发布于 2024-11-06 13:21:52 字数 64 浏览 6 评论 0原文

我有一个地图应用程序。当一个人从当前位置移动到特定位置时,我想计算该人走过的距离、该人的当前速度、该人的平均速度。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆 2024-11-13 13:21:52

使用以下方法获取以英里或公里为单位的距离。您必须在头文件中声明计时器来综合它。

//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

我还没有在设备上测试过这个,但从逻辑上讲它应该可以工作。

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.

欢烬 2024-11-13 13:21:52

为了确定任何速度,您需要计算:

距离:获取两个最新的地理坐标读数(以及每个记录的时间)来计算它们之间的距离

为此,您可以此处阅读理论,或者您可以查看代码此处

速度:使用最新的 GPS 坐标读数与上一个 GPS 坐标读数之间的距离,通过公式速度 = 距离/持续时间计算速度。因此,您找到的两个地理坐标之间的距离除以以秒或分钟为单位的持续时间或其他。那么你的答案将是速度是 x 英里或公里。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文