Ios 我需要从for循环中得到倒数第二个结果
我存储了一个具有纬度和经度值的航路点数组。我已经创建了一个for循环来循环数组,并比较我当前的位置纬度和经度,以找到我最接近的路径点。我还需要获得第二个最近的航路点,并将此ac存储为CLLocation对象,但无法使其工作 逻辑应该是这样的Ios 我需要从for循环中得到倒数第二个结果,ios,objective-c,cllocationmanager,Ios,Objective C,Cllocationmanager,我存储了一个具有纬度和经度值的航路点数组。我已经创建了一个for循环来循环数组,并比较我当前的位置纬度和经度,以找到我最接近的路径点。我还需要获得第二个最近的航路点,并将此ac存储为CLLocation对象,但无法使其工作 逻辑应该是这样的 am I closest yes move closest location to second closest set as second closest loop again to get the closest point int
am I closest
yes
move closest location to second closest
set as second closest
loop again to get the closest point
int totalNumberOfWaypoints = [waypoints count];
//Get penultimate waypoints
NSDictionary *penultimateWaypoint = [waypoints objectAtIndex:(totalNumberOfWaypoints - 2)];
我的代码:
-(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
}
-(无效)位置管理器:(CLLocationManager*)管理器更新位置:(NSArray*)位置{
//将latestlocation设置为位置数组中的最后一个对象
CLLocation*currentLocation=[locations lastObject];
//声明closestpoint对象
CLLocation*最接近当前位置的路径点;
//声明第二个最近点对象
CLLocation*SecondClosestWayPoint到当前位置;
//将距离设置为一个较大的数字
浮动距离=10000000;
float second closestwaypointdistance=10000000;
//层压荷载
NSString*plistName=[self.mapsInfo objectForKey:@“plistName”];
NSString*path=[[NSBundle mainBundle]pathForResource:@“Chester”类型:@“plist”];
NSString*path=[[NSBundle mainBundle]pathForResource:plistName of type:@“plist”];
//存储在称为航路点的数组中
NSArray*航路点=[NSArray arrayWithContentsOfFile:path];
//为locationNum(航路点)声明一个变量
int locationNum=0;
用于(NSDictionary*航路点中的点){
CLLocation*waypointLocation=[[CLLocation alloc]initWithLatitude:[(NSNumber*)[point objectForKey:@“Lat”]floatValue]经度:[(NSNumber*)[point objectForKey:@“Long”]floatValue]];
浮动航路点距离fromCurrentLocation=[currentLocation distanceFromLocation:航路点位置];
//secondClosestWayPointToCurrentLocation=航路点位置;
if(航路点距离距当前位置<距离){
//todo:将当前ClosestWayPoint移动到当前位置的第二个位置
//用距离更新第二个最近的航路点距离变量
距离=航路点距离当前位置;
closestWayPointToCurrentLocation=航路点位置;
if(closestWayPointToCurrentLocation==waypointLocation){
}
}
其他的
{
//对照第二个位置进行检查
//如果距离第二个位置较近,则将其更换为代码与上述类似的新航路点
}
如果你确定你的点总是倒数第二位,那么你可以像这样重新计算它
am I closest
yes
move closest location to second closest
set as second closest
loop again to get the closest point
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;
}];
NSMutableArray*航路点=[NSArray arrayWithContentsOfFile:path];
用于(NSDictionary*航路点中的点){
[航路点排序比较器:(NSComparisonResult)^(id obj1,id obj2){
CLLocation*航路点位置1=[[CLLocation alloc]INITWITH纬度:[(NSNumber*)[obj1 objectForKey:@“Lat”]floatValue]经度:[(NSNumber*)[obj1 objectForKey:@“Long”]floatValue]];
CLLocation*航路点位置2=[[CLLocation alloc]INITWITH纬度:[(NSNumber*)[obj2 objectForKey:@“Lat”]floatValue]经度:[(NSNumber*)[obj2 objectForKey:@“Long”]floatValue]];
浮动距离1=[currentLocation distanceFromLocation:航路点位置1];
浮动距离2=[currentLocation distanceFromLocation:航路点位置2];
如果(距离1<距离2){
回报率下降;
}
否则如果(距离1>距离2){
退避
}
其他的
返回指定名称;
}];
这样,您就可以始终在[waypoints objectAtIndex:0]
处找到最近点,在[waypoints objectAtIndex:1]
处找到第二个最近点,依此类推
更少的工作,更好的结果IMHO请正确格式化您的代码,不要滥用
xcode
标记。我以前从未使用过这个网站。您能澄清一下代码应该如何格式化吗?另外,我不知道我的标记被滥用-这个项目是在xcode中。关于代码格式化的一些一般建议:如我所见,您应该使用不要使用更多的空格,以便于阅读和分段。此外,不欢迎使用所有大写字母。Xcode标记不合适,因为代码、内部错误及其解决方案不依赖于IDE。关于你的问题,使用Xcode、Ecplipse或根本不使用任何IDE都没有区别。我对Obj-C、b不太熟悉但我认为NSArray应该有某种方法,可以在特定索引处返回数组的内容。它还应该有一种方法来调整数组的长度。如果您知道数组有多大,并且可以从任何索引中读取,那么您肯定必须能够从第n-1索引中读取,或者找到该索引的值索引,并对照第n-1个索引检查给定迭代期间的索引。数组中的航路点是地图上的分散点。代码在数组中循环并检查当前位置与每个航路点之间的距离。距离变量已设置为一个高数值,以便在E第一个循环,然后更新距离变量并循环,直到到达当前位置的最近点为止。这个点可能在数组的中间,因为我知道,所以我不认为NTH-1会起作用。如果你不使用ARC记住释放WooPosiToLoad,我可能不得不重述我原来的问题。在返回结果之前,ion1和waypointLocation2对象Hanks pro_metedor看起来很不错。我将尝试实现它,并让您知道我是如何做到的。为了确保我理解正确,您编写的代码将根据当前位置对应用程序每次加载时的WayPoint数组进行排序?将块指针强制转换为'NSCompar'isonResult*'(也称为'enu