找到靠近用户位置ios的位置(来自mutablearray)

时间:2013-11-10 18:17:08

标签: ios objective-c

我有一个可变数组的restaurantObjects序列化形式的json数据,如果当前对象名为restaurantDetail,我可以访问这样的corrdinates

float x = [[restaurantDetail.coordinates objectAtIndex:0] floatValue];
float y = [[restaurantDetail.coordinates objectAtIndex:1] floatValue];

所以我想提取靠近用户位置的餐馆并将它们存储到数组

如果用户更改位置数组也应该更新

我会真正地帮助我,因为我完全迷失在这里

我试过这个形式Muthub Ram

- (void) checkIfInRegion{
Restaurant * currentRestaurant;
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

[locationManager startUpdatingLocation];
for(int i=0;i<[restaurantsArray count];i++){
    currentRestaurant = [restaurantsArray objectAtIndex:i];
    //NSLog(@"%@",currentRestaurant.coordinates);
    CLLocation * currentRestLoc = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake([[currentRestaurant.coordinates objectAtIndex:0] floatValue], [[currentRestaurant.coordinates objectAtIndex:1] floatValue])
                                                             altitude:0
                                                   horizontalAccuracy:0
                                                     verticalAccuracy:0
                                                            timestamp:[NSDate date]];
    NSLog(@"%@",currentRestLoc);
    if([locationManager.location distanceFromLocation:currentRestLoc] <100)
    {
        //if less than 100meters
        //then add object in this array
        [near_by_user addObject:[restaurantsArray objectAtIndex:i]];
    }
}
NSLog(@"%@",locationManager.location);
NSLog(@"restaurants near%@",near_by_user);
[locationManager stopUpdatingLocation];

}

我得到所有坐标形式的位置,但我没有得到near_by_user数组中的任何对象(它仍然是空的)

http://pastebin.com/fc8q69Le这里是日志,最后一组坐标是我的位置,与餐馆之一相同

1 个答案:

答案 0 :(得分:2)

   for(i=0;i<[array_res count];i++){
    if([clloc distanceFromLocation:[array_res objectatindex:i]<100])
    {
    //if less than 100meters
    //then add object in this array
    [near_by_user addobject:[array_res objectatindex:i];
    }
    }

please add this code.if you have any clarification update here