简体   繁体   English

iOS JSON数组和MapKit

[英]iOS JSON Array and MapKit

I am trying to map a JSON array using the MapKit. 我正在尝试使用MapKit映射JSON数组。 I can get a single point on the map with the code below, but I have dozens of pins I need to mark, and I have a JSON array prepared. 使用下面的代码,我可以在地图上得到一个点,但是我需要标记很多针,并且准备了一个JSON数组。 My code for a single point is below. 我的单点代码如下。

In my .h file: 在我的.h文件中:

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

@interface MapViewController : UIViewController {

MKMapView *mapView;
NSData *data;

}

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

@end

In my .m file: 在我的.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];

Your JSON is an array of dictionary items. 您的JSON是一个字典项数组。 So you can just retrieve the full array via NSJSONSerialization , and then iterate through the dictionary entries in the resulting array. 因此,您可以通过NSJSONSerialization检索完整数组,然后遍历生成的数组中的字典条目。

First, you originally said you had JSON like the following: 首先,你最初说你有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"}]

So, if that's sitting in a file, "test.json" that you've included in your bundle, you could load it as so: 因此,如果它位于您已包含在捆绑包中的文件“test.json”中,则可以将其加载为:

// load the data from local file

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

If you have this on a web server, you'd retrieve it like so: 如果你在Web服务器上有这个,你可以像这样检索它:

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

Assuming you loaded your JSON feed into a NSData object called data , you could just do something like: 假设您将JSON供稿加载到名为dataNSData对象中,则可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM