我有一个存储有纬度和经度值的航点数组。我创建了一个 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
}