0

私はこのJSONファイルを読んでいます:

{"longitude":["36.704765","46.704765", "47.704765"],"latitude":["-93.168274","-103.168274", "86.704765"]}

この上:

NSString *filenameMap = [NSString stringWithFormat:@"%@%@Map", destDir, NavBar.topItem.title];

NSString *MapPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@Map", NavBar.topItem.title]];

[self.restClient loadFile:filenameMap intoPath:MapPath];


NSString *fileContentMap = [[NSString alloc] initWithContentsOfFile:MapPath];  

SBJsonParser *parserMap = [[SBJsonParser alloc] init];

NSDictionary *dataMap = (NSDictionary *) [parserMap objectWithString:fileContentMap error:nil];  


NSArray *MaparrayLongitude = [dataMap objectForKey:@"longitude"];
NSArray *MaparrayLatitude = [dataMap objectForKey:@"latitude"];

//NSSortDescriptor *MaparrayLongitude = [[NSSortDescriptor alloc] initWithKey:@"longitude"  ascending:NO selector:@selector(compare:)];
//NSSortDescriptor *MaparrayLatitude = [[NSSortDescriptor alloc] initWithKey:@"latitude"  ascending:NO selector:@selector(compare:)];


NSDictionary* DictionaryMap = [NSDictionary dictionaryWithObjects:MaparrayLatitude forKeys:MaparrayLongitude];

JSON ファイル内の座標の各ペア (緯度と経度) は、MapView に注釈をプロットするため、3 つの異なるマップ ビューがあり、それぞれに 1 つのピンがあります。しかし、マップは順番に並んでいる必要があります。したがって、最初のマップは最初の座標のペア、2 番目のマップ、2 番目の座標のペアを表示する必要があります...代わりに、更新を押すとマップの順序が変更され、再押すと再び変更されますリフレッシュ...

したがって、JSON ファイルの座標順を尊重したいと思います。

私はこれを試しましたが、方法はありません:

NSSortDescriptor *MaparrayLongitude = [[NSSortDescriptor alloc] initWithKey:@"longitude"  ascending:NO selector:@selector(compare:)];
    NSSortDescriptor *MaparrayLatitude = [[NSSortDescriptor alloc] initWithKey:@"latitude"  ascending:NO selector:@selector(compare:)];

助けてください!!

編集:

それから私はこれが好きです:

 NSArray *allKeys = [Dictionary allKeys];
    for (int i = 0; i < [allKeys count]; i++) {
        NSString *key = [allKeys objectAtIndex:i];
        NSObject *obj = [Dictionary objectForKey:key];

NSArray *allKeys2 = [DictionaryMap allKeys];






CGRect mapFrame = CGRectMake( 400, e, 200, 110);

MKMapView *mapView2 = [[MKMapView alloc] initWithFrame:mapFrame];
[image1 addSubview:mapView2]; 







    NSString *key2 = [allKeys2 objectAtIndex:i];
    NSObject *obj2 = [DictionaryMap objectForKey:key2];




    NSString *address = [NSString stringWithFormat:@"%@", obj2];
    float stringFloat = [address floatValue];
    float stringFloat2 = [key2 floatValue];

    CLLocationCoordinate2D anyLocation;

    anyLocation.longitude = stringFloat;

    anyLocation.latitude  = stringFloat2;







    MKPointAnnotation *annotationPoint2 = [[MKPointAnnotation alloc] init]; annotationPoint2.coordinate = anyLocation;

    annotationPoint2.title = @"Event";
    annotationPoint2.subtitle = @"Microsoft's headquarters2";
    [mapView2 addAnnotation:annotationPoint2];  


    [mapView2.userLocation setTitle:@"I am here"];

    [mapView2.userLocation addObserver:self  
                                 forKeyPath:@"location"  
                                    options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)

                                    context:NULL];

    [mapView2 setShowsUserLocation:NO];

    if (MapViewArray == nil)MapViewArray = [[NSMutableArray alloc]init];
    [MapViewArray addObject: mapView2];


     }

それで、値が統一されている場合、経度と緯度の値をどのように修正できますか??

4

1 に答える 1

2

辞書には順序の概念がないため、経度と緯度のペアを元の順序で必要とする場合、1つの辞書を使用するのは適切なデータ構造ではありません。

これはさまざまな方法で解決できます。1つは、各辞書が経度/緯度の1つのペアを表す辞書の配列を作成することです。

//...
NSArray *mapArrayLongitude = [dataMap objectForKey:@"longitude"];
NSArray *mapArrayLatitude = [dataMap objectForKey:@"latitude"];
if (mapArrayLongitude.count != mapArrayLatitude.count) {
    //Error: Unequal number of longitude/latitude values
} else {
    NSMutableArray *pairs = [NSMutableArray array];
    for (int i = 0; i < mapArrayLongitude.count; i++) {
        NSDictionary *pair = [NSDictionary dictionaryWithObjectsAndKeys:
                              [mapArrayLongitude objectAtIndex:i], @"longitude",
                              [mapArrayLatitude objectAtIndex:i], @"latitude", nil];
        [pairs addObject:pair];
    }
    NSLog(@"First pair: %@", [pairs objectAtIndex:0]);
    //...
}

また、2つの配列を保持し、後で両方の配列の同じインデックスにアクセスしてペアを再構築することもできます。

于 2012-06-28T16:58:54.503 に答える