2013-06-03 47 views
0

我在我的应用程序中遇到了非常奇怪的问题,有一个部分我从服务器收集数据,作为响应,我获得了lat和long附近的餐馆,通过使用这些拉特和长我得到每个距离当前用户的位置,我已经应用排序像衣柜远离当前用户位置收集的数据,everthing正常工作在我的结束但是每当客户端在他的最后测试时,那么排序不起作用。我在这里附上了代码。根据纬度和长度获取错误的排序距离

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    myDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

    NSLog(@"Lat %f", [[myDelegate.myLocation objectForKey:@"LATITUDE"]floatValue]); 
    NSLog(@"Long %f",[[myDelegate.myLocation objectForKey:@"LONGITUDE"]floatValue]); 

    self.coordArray= [[NSMutableArray alloc]init]; 
    // Do any additional setup after loading the view from its nib. 


    timer=[NSTimer scheduledTimerWithTimeInterval:(10.0) target:self selector:@selector(upateLocation) userInfo:nil repeats:YES]; 

    float dist; 

    for (int i=0; i<[myDelegate.placeArray count]; i++) { 

     dist=(6371 * acos(cos(RADIANS_TO_DEGREES([[myDelegate.myLocation objectForKey:@"LATITUDE"]floatValue])) * 
            cos(RADIANS_TO_DEGREES([[[myDelegate.placeArray objectAtIndex:i] latitude] floatValue])) * cos(RADIANS_TO_DEGREES([[[myDelegate.placeArray objectAtIndex:i] longitude] floatValue]) - RADIANS_TO_DEGREES([[myDelegate.myLocation objectForKey:@"LONGITUDE"]floatValue])) + sin(RADIANS_TO_DEGREES([[myDelegate.myLocation objectForKey:@"LATITUDE"]floatValue])) * sin(RADIANS_TO_DEGREES([[[myDelegate.placeArray objectAtIndex:i] latitude] floatValue])))); 

     //int distVal=(int)dist; 
     distStr= [NSString stringWithFormat:@"%.2f",dist]; 
     NSLog(@"new---%@",distStr); 
     [self.coordArray addObject:distStr]; 
    } 
    NSMutableDictionary *tmpDict; 

    for (int i=0; i<[myDelegate.placeArray count]; i++) { 

     tmpDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:[[myDelegate.placeArray objectAtIndex:i] ids],@"contentId",[[myDelegate.placeArray objectAtIndex:i] restaurantname],@"contentTitle",//contentAdd 
        [[myDelegate.placeArray objectAtIndex:i] res_type],@"contentType", 
        [[myDelegate.placeArray objectAtIndex:i] user_image],@"contentImg",[[myDelegate.placeArray objectAtIndex:i] address],@"contentAdd",[[myDelegate.placeArray objectAtIndex:i] phone],@"contentPhone", 
        [self.coordArray objectAtIndex:i],@"contentDist",nil]; 

     [self.dataArray addObject:tmpDict]; 
     [tmpDict release]; 
     tmpDict = nil; 


     // NSLog(@"self.dataArray---%f",[[[self.dataArray objectAtIndex:i] valueForKey:@"contentDist"] floatValue]); 
    } 

    NSMutableArray *tempArray = [[NSMutableArray alloc] init]; 
    [tempArray removeAllObjects]; 
    [tempArray addObjectsFromArray: self.dataArray]; 

    NSString *key = @"contentDist"; 
    NSSortDescriptor *brandDescriptor = [[NSSortDescriptor alloc] initWithKey:key ascending:YES]; 
    NSArray *sortDescriptors = [NSArray arrayWithObjects:brandDescriptor,nil]; 
    NSArray *sortedArray = [tempArray sortedArrayUsingDescriptors:sortDescriptors]; 
    [brandDescriptor release]; 
    [tempArray removeAllObjects]; 
    tempArray = (NSMutableArray*)sortedArray; 
    [self.dataArray removeAllObjects]; 
    [self.dataArray addObjectsFromArray:tempArray]; 

    /*haversine=[[Haversine alloc] init]; 
    [haversine initWithLat1:28.618095 lon1:77.390228 lat2:28.635860 lon2:77.506226]; 
    [haversine toKilometers]; 
    NSLog(@"haversine--- %f",[haversine toKilometers]);*/ 
// [self performSelector:@selector(upateLocation) withObject:nil afterDelay:10.0]; 


    } 

} 

下面是对上述代码的解释。 1 - 有一个阵列(地方阵列),它具有所有经度和长度对应于每家餐馆。 2 - 从这个数组中,通过使用Harvest方法,我得到以km为单位的距离,然后将这些距离添加到名为(coordArray)的另一个数组中。 3然后创建上,我已经申请最终阵列即(dataArray中的排序

这工作得很好,在我的结束,但不工作,一旦我对iPhone 5进行测试 任何人都可以建议我什么是错与此。代码我不是歌厅

请帮我从这个并感谢所有给你的宝贵时间

回答

2

不是一个真正的答案,但我相信你可以重新格式化你的代码来消除大量不必要的循环。只需将代码与您的代码类似,您可以将距离存储为NSNumber。您也可以使用快速枚举来删除大量冗长。

self.dataArray = [NSMutableArray array]; 
for (int i=0; i<[myDelegate.placeArray count]; i++) { 

    float dist=(6371 * acos(cos(RADIANS_TO_DEGREES([[myDelegate.myLocation objectForKey:@"LATITUDE"]floatValue])) * 
         cos(RADIANS_TO_DEGREES([[[[myDelegate.placeArray objectAtIndex:i] latitude] floatValue])) * cos(RADIANS_TO_DEGREES([[[myDelegate.placeArray objectAtIndex:i] longitude] floatValue]) - RADIANS_TO_DEGREES([[myDelegate.myLocation objectForKey:@"LONGITUDE"]floatValue])) + sin(RADIANS_TO_DEGREES([[myDelegate.myLocation objectForKey:@"LATITUDE"]floatValue])) * sin(RADIANS_TO_DEGREES([[[myDelegate.placeArray objectAtIndex:i] latitude] floatValue])))); 

    //int distVal=(int)dist; 
    NSString *distStr= [NSString stringWithFormat:@"%.2f",dist]; 

    NSDictionary *tmpDict = @{@"contentId":[[myDelegate.placeArray objectAtIndex:i] ids], 
           @"contentTitle":[[myDelegate.placeArray objectAtIndex:i] restaurantname], 
           @"contentType":[[myDelegate.placeArray objectAtIndex:i] res_type], 
           @"contentImg":[[myDelegate.placeArray objectAtIndex:i] user_image], 
           @"contentAdd":[[myDelegate.placeArray objectAtIndex:i] address], 
           @"contentPhone":[[myDelegate.placeArray objectAtIndex:i] phone], 
           @"contentDist":distStr}; 

    [self.dataArray:tmpDict]; 
} 

NSSortDescriptor *brandDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"contentDist" 
                    ascending:YES]; 

[self.dataArray sortUsingDescriptors:@[brandDescriptor]]; 
+0

感谢阿努普,它的工作,在我们的终端,因为它是工作ealriaer但在客户端这不是working.It一些值正确地排序,但在某些价值观是不work.Can请你建议。 –

+0

@FlexsinFlex由于排序是基于距离。仔细检查它的值。我的建议是删除许多详细的步骤,以提高可读性和易于调试。由于您有用于存储的模型对象,因此可以将创建的字典移动到该类。然后你只需要添加距离来创建tmpDict(由模型形成)。 – Anupdas

2

如果存储的距离为字符串

distStr= [NSString stringWithFormat:@"%.2f",dist]; 
NSLog(@"new---%@",distStr); 
[self.coordArray addObject:distStr]; 

然后他们将被排序为字符串而不是数字,例如11.00 < 2.00

你应该距离存储为NSNumber对象,而不是:

[self.coordArray addObject:[NSNumber numberWithFloat:dist]]; 

(请注意,CLLocation有distanceFromLocation: 方法,你可以改用自己的计算。)

相关问题