我存储了一系列具有纬度和经度值的航点。我创建了一个for循环来循环遍历数组并比较我当前的CLLocation位置纬度和经度来找到我最接近的哪个航点。我还需要获得第二个最近的航点,并将此交易存储为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
}
答案 0 :(得分:0)
如果您确定自己始终是倒数第二个,那么您可以像这样重复它
int totalNumberOfWaypoints = [waypoints count];
//Get penultimate waypoints
NSDictionary *penultimateWaypoint = [waypoints objectAtIndex:(totalNumberOfWaypoints - 2)];
答案 1 :(得分: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]
处获得第二个最近点,依此类推。
工作少,效果更好恕我直言