Created
December 23, 2012 21:33
-
-
Save ohayon/4366279 to your computer and use it in GitHub Desktop.
Ray Wenderlich's MapKit Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment