3

我已经通过几个不同的教程试图让这个工作,但它们似乎掩盖了初学者可能不知道的一些关键步骤。

我在 URL 处有一个 JSON 文件,其中列出了名称、纬度和经度。如何将其导入数组或字典(我不知道区别),然后对其进行迭代并在每次迭代时创建一个新注释。

IOS6,故事板

_ 添加代码 _

视图控制器.h

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


@interface ViewController : UIViewController {}

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

@property (nonatomic, strong) NSMutableData *downloadData;

@end

视图控制器.m

#import "ViewController.h"
#import "MapViewAnnotation.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _downloadData = [NSMutableData new];

    NSURL *requestURL = [NSURL URLWithString:@"OMITTED/apptest/locations.json"];
    NSURLRequest *request = [NSURLRequest requestWithURL:requestURL];
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
    [connection start];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_downloadData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    id parsed = [NSJSONSerialization JSONObjectWithData:_downloadData options:kNilOptions error:nil];
    for (NSDictionary *pointInfo in parsed)
    {
        NSLog([parsed objectForKey:@"name"]);
        double xCoord = [(NSNumber*)[parsed objectForKey:@"lat"] doubleValue];
        double yCoord = [(NSNumber*)[parsed objectForKey:@"lon"] doubleValue];
        CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(xCoord, yCoord);


        MKPointAnnotation *point = [MKPointAnnotation new];
        point.coordinate = coords;
        point.title = [parsed objectForKey:@"name"];

        [self.mapView addAnnotation:point]; // or whatever your map view's variable name is
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidUnload {
    [super viewDidUnload];
    // ARC Problem --- [_mapView release];
    self.mapView = nil;
}

@end
4

2 回答 2

2

在 iOS 5 中,有一个 JSONSerializer 类可以将原始 JSON 数据从您的 URL 转换为适当的数组或字典。

您需要从服务器下载数据:

NSURL *requestURL = [NSURL URLWithString:@"<your url here>"];
NSURLRequest *request = [NSURLRequest requestWithURL:requestURL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];

然后您将添加这些委托方法:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_downloadData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    id parsed = [NSJSONSerialization JSONObjectWithData:_downloadData options:kNilOptions error:nil];
}

_downloadData 是 NSMutableData 类型的类的实例变量或属性。

parsed变量将包含来自服务器的数据。如果它是一个点列表,它可能是一个数组,因此您可以使用快速枚举对其进行迭代:

for (NSDictionary *pointInfo in parsed) {
    double xCoord = [(NSNumber*)[parsed objectForKey:@"<key for lat coord>"] doubleValue];
    double yCoord = [(NSNumber*)[parsed objectForKey:@"<key for long coord>"] doubleValue];
    CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(xCoord, yCoord);


    MKPointAnnotation *point = [[MKPointAnnotation new] autorelease];
    point.coordinate = coords;        
    point.title = [parsed objectForKey:@"<key for title>"];  

    [self.mapView addAnnotation:point]; // or whatever your map view's variable name is
}
于 2013-02-13T23:21:09.093 回答
0

我在 GitHub 上有一个开源项目,它使用带有 NSCoding 协议的序列化程序,因此您可以直接从 JSON 流自动创建实例。

它在这里

于 2013-02-13T23:25:05.627 回答