2012-06-27 87 views
0

我解析此JSON文件(正常,它的工作原理與UITextFields):添加註解爲座標的MKMapView

{"longitude":["37.786793","39.388528"],"latitude":["-122.395416","-78.887734"]} 

IND創建MapViews,以這種方式在相應註解:

NSArray *allKeys2 = [DictionaryMap allKeys]; 

for (int h = 0; h < [allKeys2 count]; h++) { 


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]; 

    [MapViewArray addObject:mapView2]; 

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


    }} 

}while(g < f); 

...我想要第一個地圖視圖顯示第一個座標引腳,第二個顯示第二對座標。但現在,它在所有地圖視圖上繪製了相同的引腳,分別對應於最後一個座標,而不是第一個和第二個。此方法適用於UITextField文本,所以我找不到問題。

請幫忙!!

編輯:

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"]; 


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

回答

0

另取:

你可能不希望您的緯度映射到DictionaryMap經度。如何製作一個NSDirearies的NSArray,其中每個字典都有一個「緯度」鍵和一個「經度」鍵?

此外,您在代碼中有兩次「[MapViewArray addObject:mapView2]」。

什麼是「g」?也許增加一些日誌到你的循環,這樣你就可以看到創建的是什麼?

+0

不,我實際上想創建許多地圖視圖,每個都有一個註釋。第一個地圖視圖具有第一個針腳座標,第二個地圖視圖具有第二個針腳座標... – Alessandro

+0

看看我的編輯,我添加了用於解析文件的代碼,也許它有幫助,因爲您提供的代碼給了我錯誤(不兼容的ID) – Alessandro

+0

我有一個NSMutableArray問題...解決了 – Alessandro