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 个引脚。但是地图应该是有序的:所以第一张地图必须显示第一对坐标,第二张地图,第二对坐标......而不是当我按下刷新时,地图的顺序会改变,如果我按下再改变刷新...

所以我希望 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

字典没有顺序的概念,因此如果您需要按原始顺序排列的经度/纬度对,则不适合使用一个字典。

您可以通过多种方式解决此问题,一种是创建一个字典数组,其中每个字典代表对经度/纬度:

//...
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]);
    //...
}

您也可以简单地保留这两个数组,然后通过访问两个数组中的相同索引来重建这些对。

于 2012-06-28T16:58:54.503 回答