从URL读取JSON并添加MKAnnotations [英] Reading JSON from URL and adding MKAnnotations
问题描述
我已经尝试了几种不同的教程来使它起作用,但是它们似乎掩盖了初学者可能不知道的一些关键步骤.
I've been through several different tutorials trying to get this working, but they seem to gloss over some crucial steps that a beginner might not know.
我在URL上有一个JSON文件,其中列出了名称,纬度和经度.我如何将其导入到数组或字典中(我不知道它们之间的区别),然后对其进行迭代并在每次迭代中创建一个新的注释.
I have a JSON file at a URL with name, latitude, and longitude listed. How can I import that to an array or dictionary (I don't know the difference), and then iterate over it and create a new annotation with each iteration.
IOS6,情节提要
_添加了代码_
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@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类,可以将原始JSON数据从您的URL转换为适当的数组或字典.
With iOS 5 there's a JSONSerializer class that can convert the raw JSON data from your URL into an array or dictionary as appropriate.
您需要从服务器下载数据:
You'll need to download the data from the server:
NSURL *requestURL = [NSURL URLWithString:@"<your url here>"];
NSURLRequest *request = [NSURLRequest requestWithURL:requestURL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
然后,您将添加以下委托方法:
Then you'll add these delegate methods:
- (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的类的实例变量或属性.
_downloadData is an instance variable or property of your class of type NSMutableData.
该parsed
变量将包含服务器中的数据.如果它是点列表,则可能是一个数组,因此您可以使用快速枚举对其进行迭代:
That parsed
variable will contain your data from the server. It's probably an array if it's a list of points, so you can iterate through it using fast enumeration:
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
}
这篇关于从URL读取JSON并添加MKAnnotations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!