【问题标题】:Optimize coordinate calculations in two nested arrays优化两个嵌套数组中的坐标计算
【发布时间】:2018-01-01 11:05:22
【问题描述】:

我正在开发一个应用程序,其中Google 地图上有一组坐标,用于绘制路线。还有第二个加油站坐标数组。在两个 10,000 个点的数组中。我需要显示第二个数组中偏离 5 英里的路线上的点。我在嵌套数组中提出了这个要求,但是这个计算非常冗长,大约需要五分钟。如何优化并加快计算速度?我将非常感谢任何建议:

这是我的代码

- (void)addRandomPointsOnMap {
CGFloat upperBoundLatitude = 46.80;
CGFloat lowerBoundLatitude = 29.76;
CGFloat upperBoundLongitude = -118.64;
CGFloat lowerBoundLongitude = -75.6;

for (int i = 0; i < 10000; i++) {
    TSPoint *randomPoint = [[TSPoint alloc] init];
    randomPoint.latitude = [self randomFloatBetween:upperBoundLatitude
                                          and:lowerBoundLatitude];
    randomPoint.longitude = [self randomFloatBetween:upperBoundLongitude
                                           and:lowerBoundLongitude];
    CLLocation *randomPointLocation = [[CLLocation alloc] initWithLatitude:randomPoint.latitude longitude:randomPoint.longitude];

    for (int i = 0; i < _routePoints.count; i++) {
        TSPoint *routePoint = _routePoints[i];
        CLLocation *routePointLocation = [[CLLocation alloc] initWithLatitude:routePoint.latitude longitude:routePoint.longitude];
        NSInteger distanceInMeters = [routePointLocation distanceFromLocation:randomPointLocation];

        NSInteger distanceInMiles = distanceInMeters / 1609.344;
        if (distanceInMiles < 5) {

            GMSMarker *markerT1 = [[GMSMarker alloc] init];
            markerT1.position = CLLocationCoordinate2DMake(randomPoint.latitude, randomPoint.longitude);
            markerT1.icon = [UIImage imageNamed:@"blue_pin"];
            markerT1.groundAnchor = CGPointMake(0.5, 0.5);
            markerT1.map = _mapView;
        }
    }
} }

以及计算有界范围内随机坐标的方法:

- (float)randomFloatBetween:(float)lowerBound and:(float)upperBound {
float diff = upperBound - lowerBound;
return (((float) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + lowerBound; }

【问题讨论】:

    标签: ios objective-c arrays xcode google-maps


    【解决方案1】:

    您有一个确定路线的点数组。我的假设是,考虑到 5 英里的偏差,有很多点彼此之间的距离很小。

    这是一个例子: 您路线中的第一个点位于给定位置。路线中的第二个点距离前一个位置 3 英尺。考虑到您找到了一个位于第一个位置 5 英里范围内的加油站,可以安全地假设它也位于第二个位置 5 英里范围内。因此,所有已经验证了第一个位置的站点不再需要检查第二个位置。

    我想你明白我想用这个去哪里了。尝试以适合您的距离减少路线中的点。

    您可以从偏差的 1/20 开始。这意味着路线上距离另一个已验证站点的位置 0.25 英里范围内的每个位置将不再验证站点,因为它将假定与已验证站点相同的站点。

    以下是减少的方式:

    - (NSArray *)reducePointsInArray:(NSArray*)locations withinDistance:(CGFloat)distance {
         NSMutableArray *mutableLocations = [locations mutableCopy];
    
         //This array will hold the indexes of insignificant locations
         NSMutableArray *reduceIndexes = [NSMutableArray array];
    
         //Start with a reference location
         CLLocation referenceLocation = [locations firstObject];
         [locations enumerateObjectsUsingBlock:^(CLLocation *location, NSUInteger idx, BOOL *stop) {
             //Don't compare first location to first location
             if (idx == 0) {
                 return;
             }
             //Compare location with reference location
             if([locations distanceFromLocation:referenceLocation] < distance) {
                 [reduceIndexes addObject:[NSIndexSet indexSetWithIndex:idx]];
             } else {
                 referenceLocation = location;
             }
        }];
        [mutableLocations removeObjectsAtIndexes:reduceIndexes];
        return mutableLocations;
    }
    

    如果你认为 0.25 英里对于减少路线位置来说太多了,你可以少走一点,我认为它仍然会减少一些位置。尝试使用不同的值并记录减少前后的位置数量。根据您的路线点之间的距离,我猜您甚至可以减少多达 80% 的路线点,而不会显着影响您的结果。

    【讨论】:

    • 感谢您的回复!是的,我知道路线点之间的距离非常小,我现在明白你的意思是为了减少迭代。也就是说,如果在半径5英里的范围内找到加油站,那么接下来的坐标检查应该是用偏差半径的值,即-5英里?
    • 不,我的意思是,您选择第一个位置并获得车站。然后考虑所有找到的站点也适用于距第一个位置 0.25 英里范围内的下一个位置。 0.25 英里的位置是算法中的距离。
    • 感谢您提出这个想法。我明白如何决定
    • 我很欣赏这个答案,但我的评分很低,我的评价不可见
    猜你喜欢
    • 2020-12-26
    • 1970-01-01
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多