1

私のplistファイル:

   NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
   NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:@"data.plist"];
   dict = [NSDictionary dictionaryWithContentsOfFile:path];

配列をループし、注釈を追加して距離を計算します。

 ann = [dict objectForKey:@"Blue"];
 [resultArray addObject:@"Blue"];


 for(int i = 0; i < [ann count]; i++) {


 NSString *coordinates = [[ann objectAtIndex:i] objectForKey:@"Coordinates"];

 double realLatitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:1] doubleValue];
 double realLongitude = [[[coordinates componentsSeparatedByString:@","] objectAtIndex:0] doubleValue];

 MyAnnotation *myAnnotation = [[MyAnnotation alloc] init];
 CLLocationCoordinate2D theCoordinate;
 theCoordinate.latitude = realLatitude;
 theCoordinate.longitude = realLongitude;

 myAnnotation.coordinate=CLLocationCoordinate2DMake(realLatitude,realLongitude);
 myAnnotation.title = [[ann objectAtIndex:i] objectForKey:@"Name"];
 myAnnotation.subtitle = [[ann objectAtIndex:i] objectForKey:@"Address"];
 myAnnotation.icon = [[ann objectAtIndex:0] objectForKey:@"Icon"];

 [mapView addAnnotation:myAnnotation];
 [annotations addObject:myAnnotation];

 CLLocation *pinLocation = [[CLLocation alloc]
                                           initWithLatitude:realLatitude
                                           longitude:realLongitude];

 CLLocation *userLocation = [[CLLocation alloc]
                                 initWithLatitude:mapView.userLocation.coordinate.latitude
                                            longitude:mapView.userLocation.coordinate.longitude];

 CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation];

 NSLog(@"Distance: %4.0f m.", distance);

 }

私のplist構造:

ここに画像の説明を入力

Dictionariesここで、「青」というArray新しいKey名前の「距離」に距離を付けてすべてを追加する必要がありますstring

4

3 に答える 3

0

私が理解しているように、次のコードのようになります。

    NSDictionary *rootDictionary = ...; //Your initialization
    NSString *blueKey = @"Blue";
    NSArray *blueArr = [rootDictionary objectForKey:blueKey];
    NSMutableArray *newBlueArr = [NSMutableArray array];
    for (NSDictionary *childDictionary in blueArr) {

        NSMutableDictionary *newChildDictionary = [NSMutableDictionary dictionaryWithDictionary:childDictionary];
        [newChildDictionary setValue:@"distance" forKey:@"Distance"];
        [newBlueArr addObject:newChildDictionary];
    }
    NSMutableDictionary *tempDictionary = [NSMutableDictionary dictionaryWithDictionary:rootDictionary];
    [tempDictionary setValue:newBlueArr forKey:blueKey];
    rootDictionary = tempDictionary;

配列/辞書からオブジェクトを追加または削除しない場合、不変オブジェクトを可変オブジェクトに変換する必要がない場合があるため、誰かがより簡単な方法をアドバイスできると思います。

于 2013-05-04T11:30:26.660 に答える