0

使用 iPhone 并利用核心位置,我想计算赛车在赛道上的距离。场景是一次只有一辆车在赛道上,而且赛道也不是直的。当他们在赛道上一一测试汽车时,它类似于 TopGear 程序,你知道赛道不是笔直的。

我正在获取纬度、经度、速度并将其发送到 Web 服务器上以实时监控事物。

我面临的问题是:

1>当汽车在轨道上运行时,它可以以之字形方式运行。轨道很宽,什么时候可以在上面行驶,汽车可以在左、中或右车道上行驶。在汽车换档车道期间,还记录其纬度和经度。意思是如果你在纸上从上到下画一条蛇形,然后从上到下画一条直线,在我的场景中也是如此,我想要实际距离?

我尝试了几件事和教程,但是在设备上运行代码并在房间里来回走动后距离不断增加,但是当那些小动作无法避免时,我如何获得正确的距离?

2> 例如,如果轨道是“U”形并且使用distanceFromLocation:它给出“U”点到点的距离而不是整个“U”轨道距离,例如汽车从“U”左点开始到“U”右点。如何计算整个“U”距离?

我正在关注本教程。

4

3 回答 3

1

如果您正在记录所有点,那么您可以遍历它们并将每对点之间的距离相加。要获得所有点之间的距离(伪代码):

double distance = 0;
CLLocationCoordinate2D p1 = ...;
CLLocationCoordinate2D p2 = ...;

for (NSInteger i = 0; i < count; i++) {
    distance += [self metresBetweenPlace1:p1 place2:p2];

    p1 = p2;
    p2 = ...;
}

- (double)metresBetweenPlace1:(CLLocationCoordinate2D)place1 andPlace2:(CLLocationCoordinate2D)place2
{
    MKMapPoint start = MKMapPointForCoordinate(place1);
    MKMapPoint finish = MKMapPointForCoordinate(place2);

    return MKMetersBetweenMapPoints(start, finish);
}
于 2013-04-26T19:21:44.533 回答
1

不幸的是,您不能仅使用 iOS 设备的内部 GPS 非常准确地做到这一点。因为内部 GPS 仅以 1Hz 的速率更新。对于快速赛车来说,一切都可以在 1 秒内发生。除非您使用高速率的外部 GPS 接收器,否则您无法实现目标。市场上有一些iOS的GPS接收器,你可以google一下。我个人使用的是 20Hz 速率的 Racelogic VBOX Sport。

然后,我会使用速度*时间来计算距离,而不是使用 distanceFromLocation:。因为 GPS 可以为您提供高精度的速度和 UTC 时间,但不能保证高精度的位置(确切位置)。

如果你不担心准确性,你可以这样做。当您获得每个样本 CLLocation 时,获取速度和时间(当前时间戳减去前一个时间戳)并将其相乘并将此结果添加到下一个样本结果中。例如

//Ignore if speed is less than 5 km/h
if (newLocation.speed < 1.39)
return;

//(average speed in m/s) * (time in seconds)
float result = (newLocation.speed + oldLocation.speed)/2 * [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];

并总结所有结果。

于 2013-04-26T13:04:26.980 回答
0

尝试在您的视图控制器中输入此代码,它对我有用。

- (void)viewDidLoad
{
    [super viewDidLoad];
    appDelegate = [[UIApplication sharedApplication] delegate];
    locationManager =[[CLLocationManager alloc] init];
    // Do any additional setup after loading the view, typically from a nib.
    [userMap setCenterCoordinate: userMap.userLocation.coordinate animated: YES];
    [self performSelector:@selector(checkForLogin) withObject:nil afterDelay:1];

    [self startLocationServices];
        // create LM
        self.locationManager = [CLLocationManager new];

        // set its delegate
        [self.locationManager setDelegate:self];

        // set configuration
        [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        [self.locationManager setDistanceFilter:kCLDistanceFilterNone];

        // start location services
        [self.locationManager startUpdatingLocation];
    // create an oldLocation variable if one doesn't exist

    }


- (void)startLocationServices
{
    // create the Location Manager
    if (self.locationManager == nil) {
        self.locationManager = [CLLocationManager new];
    }

    // stop services
    [self.locationManager stopUpdatingLocation];
    [self.locationManager setDelegate:nil];
    self.uilabel.text = @"0.0f";
}
// locationManager didUpdateToLocation FromLocation
    - (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
    NSLog(@"%@", newLocation);
      self.uilabel.text = [NSString stringWithFormat:@"%d %.f ", x, [newLocation speed] * 2.23694];


    }

 + (NSString *) speedToMPH: (float) value
{
 NSString *x = @"0.0 ";
 if (value>0) {
 float mile = 1609.344f;
 float mph = value / mile * 3600;
 x = [NSString stringWithFormat: @"%.f ", mph];
 }
 return x;
 }

并将其添加到您的.h 文件中

@property (strong, nonatomic) IBOutlet UILabel *uilabel;
@property(nonatomic) int x;
@property (nonatomic,retain) CLLocationManager *locationManager;
+ (NSString *) speedToMPH: (float) value;
于 2013-08-21T04:24:26.347 回答