从URL读取JSON并添加MKAnnotations
我已经通过几个不同的教程试图让这个工作,但他们似乎掩盖了初学者可能不知道的一些关键步骤。
我在列出名称,纬度和经度的URL上有一个JSON文件。 如何将其导入数组或字典(我不知道区别),然后迭代它并在每次迭代时创建一个新的注释。
IOS6,故事板
_添加代码_
ViewController.h
#import #import @interface ViewController : UIViewController {} @property (weak, nonatomic) IBOutlet MKMapView *mapView; @property (nonatomic, strong) NSMutableData *downloadData; @end
ViewController.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
在iOS 5中,有一个JSONSerializer类,可以根据需要将URL中的原始JSON数据转换为数组或字典。
您需要从服务器下载数据:
NSURL *requestURL = [NSURL URLWithString:@""]; 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:@""] doubleValue]; double yCoord = [(NSNumber*)[parsed objectForKey:@" "] doubleValue]; CLLocationCoordinate2D coords = CLLocationCoordinate2DMake(xCoord, yCoord); MKPointAnnotation *point = [[MKPointAnnotation new] autorelease]; point.coordinate = coords; point.title = [parsed objectForKey:@" "]; [self.mapView addAnnotation:point]; // or whatever your map view's variable name is }
我在GitHub上有一个开源项目,它使用带有NSCoding协议的串行器,因此您可以直接从JSON流自动创建实例。
就在这里 。