2013-06-03 48 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: 方法,你可以改用自己的計算。)

相關問題