public
Created

Ray Wenderlich's MapKit Tutorial

  • Download Gist
MyLocation.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
//
// MyLocation.m
// MapTutorial
//
// Created by David Ohayon on 12/22/12.
// Copyright (c) 2012 David Ohayon. All rights reserved.
//
 
#import "MyLocation.h"
#import <AddressBook/AddressBook.h>
 
@interface MyLocation()
 
@property (nonatomic, copy) NSString* name;
@property (nonatomic, copy) NSString* address;
@property (nonatomic, assign) CLLocationCoordinate2D theCoordinate;
 
@end
 
@implementation MyLocation
 
- (id)initWithName:(NSString *)name address:(NSString *)address coordinate:(CLLocationCoordinate2D)coordinate{
if (self = [super init]) {
if([name isKindOfClass:[NSString class]]){
self.name = name;
} else{
self.name = @"Unknown Charge";
}
self.address = address;
self.theCoordinate = coordinate;
}
return self;
}
 
- (NSString*)title{
return _name;
}
 
- (NSString*)subtitle{
return _address;
}
 
- (CLLocationCoordinate2D)coordinate{
return _theCoordinate;
}
 
- (MKMapItem*)mapItem{
NSDictionary* addressDict = @{
(NSString*) kABPersonAddressStreetKey: _address
};
MKPlacemark* placemark = [[MKPlacemark alloc] initWithCoordinate:self.coordinate addressDictionary:addressDict];
MKMapItem* mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
mapItem.name = self.title;
return mapItem;
}
 
@end
ViewController.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
//
// ViewController.m
// MapTutorial
//
// Created by David Ohayon on 12/22/12.
// Copyright (c) 2012 David Ohayon. All rights reserved.
//
 
#import "ViewController.h"
#import "ASIHTTPRequest.h"
#import "MyLocation.h"
#define METERS_PER_MILE 1609.344
@interface ViewController ()
 
@end
 
@implementation ViewController
 
- (void)plotCrimePositions:(NSData*)responseData{
for (id<MKAnnotation> annotation in _mapView.annotations) {
[_mapView removeAnnotation:annotation];
}
NSDictionary* root = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSArray* data = [root objectForKey:@"data"];
for (NSArray* row in data) {
NSNumber* latitute = row[21][1];
NSNumber* longitude = row [21][2];
NSString* crimeDescription = row[17];
NSString* address = row[13];
CLLocationCoordinate2D coordinate;
coordinate.latitude = latitute.doubleValue;
coordinate.longitude = longitude.doubleValue;
MyLocation* annotation = [[MyLocation alloc] initWithName:crimeDescription address:address coordinate:coordinate];
[_mapView addAnnotation:annotation];
}
}
 
- (IBAction)refreshTapped:(id)sender{
MKCoordinateRegion mapRegion = [_mapView region];
CLLocationCoordinate2D centerLocation = mapRegion.center;
NSString* jsonFile = [[NSBundle mainBundle] pathForResource:@"command" ofType:@"json"];
NSString* formatString = [NSString stringWithContentsOfFile:jsonFile encoding:NSUTF8StringEncoding error:nil];
NSString* json = [NSString stringWithFormat:formatString, centerLocation.latitude, centerLocation.longitude, 0.5*METERS_PER_MILE];
NSURL* url = [NSURL URLWithString:@"http://data.baltimorecity.gov/api/views/INLINE/rows.json?method=index"];
ASIHTTPRequest* _request = [ASIHTTPRequest requestWithURL:url];
__weak ASIHTTPRequest* request = _request;
request.requestMethod = @"POST";
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request appendPostData:[json dataUsingEncoding:NSUTF8StringEncoding]];
[request setDelegate:self];
[request setCompletionBlock:^{
NSString* responseString = [request responseString];
NSLog(@"Response: %@", responseString);
}];
[request setFailedBlock:^{
NSError* error = [request error];
NSLog(@"Error %@", error.localizedDescription);
}];
[request startAsynchronous];
[self plotCrimePositions:request.responseData];
}
 
- (void) viewWillAppear:(BOOL)animated{
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 39.281516;
zoomLocation.longitude = -76.580806;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5* METERS_PER_MILE);
[_mapView setRegion:viewRegion animated:YES];
}
 
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
 
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
 
@end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.