0

我在我的应用程序中遇到了非常奇怪的问题,有一个部分我正在从服务器收集数据,作为回应,我得到了所有附近餐馆的经纬度,通过使用这些经度和经度,我得到了每个人与当前用户位置的距离,并且我已经在收集的数据上应用了像壁橱一样的排序,使其远离当前用户位置,一切正常,但每当客户在他的最后进行测试时,排序就不起作用.我在这里附上了代码。

- (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 方法,我得到以公里为单位的距离,然后将它们添加到另一个名为(coordArray)的数组中。3-然后创建一个最终数组,即(我已对其应用排序的dataArray

这在我的最后工作正常,但在我在 iPhone 5 上测试后就不起作用了。任何人都可以建议我这个代码有什么问题,我没有得到。

请帮我解决这个问题,感谢大家付出宝贵的时间。

4

2 回答 2

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: 方法可以用来代替您自己的计算。)

于 2013-06-03T12:23:01.750 回答
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]];
于 2013-06-03T12:37:39.823 回答