基于lat和long获得错误的排序距离

时间:2013-06-03 12:12:49

标签: iphone ios objective-c

我在我的应用程序中遇到了非常奇怪的问题,有一个部分我从服务器收集数据,作为回应我得到了lat和很长的所有附近的餐馆,通过使用这些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-Then创建一个最终数组,即(我已应用排序的dataArray

这在我的最后工作正常,但一旦我在iPhone 5上测试就无法工作。 任何人都可以告诉我这个代码有什么问题,我不知道。

请帮助我解决这个问题并感谢所有人给予你宝贵的时间。

2 个答案:

答案 0 :(得分: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: 您可以使用的方法,而不是您自己的计算。)

答案 1 :(得分: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]];