2

我正在尝试使用 MapKit 映射 JSON 数组。我可以使用下面的代码在地图上获得一个点,但是我需要标记几十个图钉,并且我准备了一个 JSON 数组。我的单点代码如下。

在我的 .h 文件中:

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapViewController : UIViewController {

MKMapView *mapView;
NSData *data;

}

@property (nonatomic, retain) IBOutlet MKMapView *mapView;

@end

在我的 .m 文件中:

NSData *data = @"[{"id":"1","name":"Jiffy Lube","lat":"21.306","lon":"-157.861"},    
{"id":"2","name":"Bills
Oil","lat":"21.301","lon":"-157.863"},{"id":"3","name":"Auto Zone","lat":"21.307","lon":"-
157.862"}]";

// parse the JSON into a NSArray

NSError *error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:data
                                                 options:0
                                                   error:&error];
4

1 回答 1

7

您的 JSON 是一个字典项数组。因此,您可以通过 检索整个数组NSJSONSerialization,然后遍历结果数组中的字典条目。

首先,您最初说您有如下 JSON:

[{"id":"1","name":"Jiffy Lube","lat":"21.306","lon":"-157.861"},    
{"id":"2","name":"Bills Oil","lat":"21.301","lon":"-157.863"},
{"id":"3","name":"Auto Zone","lat":"21.307","lon":"-157.862"}]

因此,如果它位于您的捆绑包中包含的文件“test.json”中,您可以这样加载它:

// load the data from local file

NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"json"];
NSData *data = [NSData dataWithContentsOfFile:path];

如果你在网络服务器上有这个,你会像这样检索它:

// load the data from web server

NSURL *url = [NSURL URLWithString:@"http://insert.your.server/and/url/here/test.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    // use NSData here
}];

假设您将 JSON 提要加载到NSData名为 的对象data中,您可以执行以下操作:

// parse the JSON into a NSArray

NSError *error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:data 
                                                 options:0
                                                   error:&error];
if (error != nil)
{
    // handle the error as you want
}

// a few variables to be used as we iterate through the array of results

CLLocationCoordinate2D location;                         // coordinates of the annotation
NSMutableArray *newAnnotations = [NSMutableArray array]; // an array in which we'll save our annotations temporarily
MKPointAnnotation *newAnnotation;                        // the pointer to the annotation we're adding

// iterate through the array, adding an annotation to our our array of new annotations

for (NSDictionary *dictionary in array)
{
    // retrieve latitude and longitude from the dictionary entry

    location.latitude = [dictionary[@"lat"] doubleValue];
    location.longitude = [dictionary[@"lon"] doubleValue];

    // create the annotation

    newAnnotation = [[MKPointAnnotation alloc] init];
    newAnnotation.title = dictionary[@"name"];
    newAnnotation.coordinate = location;

    // add it to our array
    //
    // incidentally, generally I just add it to the mapview directly, but
    // given that you have a didAddAnnotationViews, we'll just build up 
    // an array and add them all to the map view in one step after we're 
    // done iterating through the JSON results

    [newAnnotations addObject:newAnnotation];

    // clean up

    [newAnnotation release];
}

// when done, add the annotations

[self.mapView addAnnotations:newAnnotations];
于 2013-02-10T21:48:14.800 回答