2

我有一个存储有纬度和经度值的航点数组。我创建了一个 for 循环来遍历数组并比较我当前的 CLLocation 位置纬度和经度,以找到我最接近的航点。我还需要获取第二个最近的航路点并存储此 ac sa CLLocation 对象,但无法使其正常工作。

逻辑是这样的

am I closest
     yes
move closest location to second closest
     set as second closest
loop again to get the closest point

我的代码:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    //set latestlocation as the last object from the locations array
    CLLocation *currentLocation = [locations lastObject];

    //declare a closestpoint object
    CLLocation *closestWayPointToCurrentLocation;

    //declare a second closest point object
    CLLocation *secondClosestWayPointToCurrentLocation;

    //set the distance to a high number
    float distance = 10000000;
    float secondClosestWaypointDistance = 10000000;

    //load in plist
    NSString *plistName = [self.mapsInfo objectForKey:@"plistName"];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Chester" ofType:@"plist"];
    NSString *path = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];

    //store in array called waypoints
    NSArray *waypoints= [NSArray arrayWithContentsOfFile:path];

    //declare a variable for locationNum (the waypoints)
    int locationNum = 0;

    for (NSDictionary *point in waypoints) {
        CLLocation *waypointLocation = [[CLLocation alloc]initWithLatitude:[(NSNumber *)[point  objectForKey:@"Lat"]floatValue] longitude:[(NSNumber *)[point objectForKey:@"Long"]floatValue]];

        float waypointDistanceFromCurrentLocation = [currentLocation distanceFromLocation:waypointLocation];

        //secondClosestWayPointToCurrentLocation = waypointLocation;

        if(waypointDistanceFromCurrentLocation < distance) {
            //todo: move the current closestWayPointToCurrentLocation into second postion
            //update the second closest waypoint distance variable also with distance

            distance = waypointDistanceFromCurrentLocation;
            closestWayPointToCurrentLocation = waypointLocation;



            if(closestWayPointToCurrentLocation == waypointLocation) {

            }

        }
        else
        {
            //check against the second position
        //if closer than second position, replace it with new waypoint with code similar to above
        }
4

2 回答 2

0

如果您确定您始终是倒数第二个,那么您可以像这样检索它

int totalNumberOfWaypoints = [waypoints count];

//Get penultimate waypoints
NSDictionary *penultimateWaypoint = [waypoints objectAtIndex:(totalNumberOfWaypoints - 2)];  
于 2013-03-30T21:04:39.003 回答
0

你为什么不做一个排序的点数组,按距离升序排序?

NSMutableArray *waypoints= [NSArray arrayWithContentsOfFile:path];

    for (NSDictionary *point in waypoints) {


[waypoints sortUsingComparator: (NSComparisonResult) ^ (id obj1, id obj2) {
        CLLocation *waypointLocation1 = [[CLLocation alloc]initWithLatitude:[(NSNumber *)[obj1 objectForKey:@"Lat"]floatValue] longitude:[(NSNumber *)[obj1 objectForKey:@"Long"]floatValue]];
        CLLocation *waypointLocation2 = [[CLLocation alloc]initWithLatitude:[(NSNumber *)[obj2 objectForKey:@"Lat"]floatValue] longitude:[(NSNumber *)[obj2 objectForKey:@"Long"]floatValue]];

        float distance1 = [currentLocation distanceFromLocation:waypointLocation1];
        float distance2 = [currentLocation distanceFromLocation:waypointLocation2];

        if (distance1 < distance2) {
            return NSOrderedAscending;
        }
        else if (distance1 > distance2) {
            return NSOrderedDescending
        }
        else
            return NSOrderedSame;

}];

这样,您始终可以拥有最近的点[waypoints objectAtIndex:0]和第二最近的点,[waypoints objectAtIndex:1]依此类推。

更少的工作,更好的结果恕我直言

于 2013-03-30T21:08:28.403 回答