我需要从一个 JSON 文件 atURL 中读取并为每个“名称、纬度、经度”做一个注释。这是到目前为止我得到的代码,它可以编译但不显示任何内容。和想法?
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
**来自 NSLog 对 _downloadData 的响应
来自 _downloadData 的字符串 =
{
"name": "Waipio"
"address": "100 oak"
"lat": "28.00"
"lon": "-125.00"
"phonenumber": "555"
}
{
"name": "Murrietta"
"address": "100 Elm"
"lat": "83.560852"
"lon": "-134.135674"
"phonenumber": "555"
}