0
我讀這個JSON文件:排序一個NSDictionary
{"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];
}
所以,如果值是團結,我怎麼能annign一個經度和緯度值?
看到編輯上面 – Alessandro
看到編輯我已經發布,它繼續代碼 – Alessandro