Reading JSON from URL and adding MKAnnotations
I've gone through several different tutorials trying to get this to work, but they seem to have masked some important steps that a beginner might not be aware of.
I have a JSON file with a URL with name, latitude and longitude. How can I import this into an array or dictionary (I don't know the difference) and then iterate over it and create a new annotation with each iteration.
IOS6, storyboards
_ Added code _
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 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
source to share
IOS 5 has a JSONSerializer class that can convert the raw JSON data from your URL to an array or dictionary as needed.
You need to download 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 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 is a variable or property of an instance of your class of type NSMutableData.
This variable parsed
will contain your data from the server. It's probably an array if it's a list of points, so you can iterate over it with a quick 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 variable name is
}
source to share