我存储了一个具有纬度和经度值的路点数组。我创建了一个for循环来循环数组,并比较我当前的CLLocation位置纬度和经度,以找到我最接近的路点。我还需要获得第二个最近的航路点,并将此ac存储为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
}
如果你确定你总是倒数第二个点,那么你可以像这样检索它
int totalNumberOfWaypoints = [waypoints count];
//Get penultimate waypoints
NSDictionary *penultimateWaypoint = [waypoints objectAtIndex:(totalNumberOfWaypoints - 2)];
为什么不做一个点的排序数组,按距离升序排序?
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]
上有第二个最近的点,以此类推。
事半功倍