Skip to content

Instantly share code, notes, and snippets.

@wenhuizhang
Created May 4, 2014 00:27
Show Gist options
  • Save wenhuizhang/834b55788883e36078af to your computer and use it in GitHub Desktop.
Save wenhuizhang/834b55788883e36078af to your computer and use it in GitHub Desktop.
EmergMap
//
// Prefix header
//
// The contents of this file are implicitly included at the beginning of every source file.
//
#import <Availability.h>
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
//
// EmergMapAnnotation.h
// EmergMap
//
// Created by Wenhui Zhang on 5/2/14.
// Copyright (c) 2014 Wenhui Zhang. All rights reserved.
//
#ifndef EmergMap_EmergMapAnnotation_h
#define EmergMap_EmergMapAnnotation_h
#endif
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface EmergMapAnnotation : NSObject <MKAnnotation>{
NSString *title;
NSString *subtitle;
NSString *note;
CLLocationCoordinate2D coordinate;
}
@property (nonatomic, copy) NSString * title;
@property (nonatomic, copy) NSString * subtitle;
@property (nonatomic, assign)CLLocationCoordinate2D coordinate;
@end
//
// EmergMapAnnotation.m
// EmergMap
//
// Created by Wenhui Zhang on 5/2/14.
// Copyright (c) 2014 Wenhui Zhang. All rights reserved.
//
#ifndef EmergMap_EmergMapAnnotation_m
#define EmergMap_EmergMapAnnotation_m
#endif
#import "EmergMapAnnotation.h"
@implementation EmergMapAnnotation
@synthesize title;
@synthesize subtitle;
@synthesize coordinate;
@end
//
// EmergMapAppDelegate.h
// EmergMap
//
// Created by Wenhui Zhang on 5/1/14.
// Copyright (c) 2014 Wenhui Zhang. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface EmergMapAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
//
// EmergMapAppDelegate.m
// EmergMap
//
// Created by Wenhui Zhang on 5/1/14.
// Copyright (c) 2014 Wenhui Zhang. All rights reserved.
//
#import "EmergMapAppDelegate.h"
@implementation EmergMapAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
//
// EmergMapTests.m
// EmergMapTests
//
// Created by Wenhui Zhang on 5/1/14.
// Copyright (c) 2014 Wenhui Zhang. All rights reserved.
//
#import <XCTest/XCTest.h>
@interface EmergMapTests : XCTestCase
@end
@implementation EmergMapTests
- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testExample
{
XCTFail(@"No implementation for \"%s\"", __PRETTY_FUNCTION__);
}
@end
//
// EmergMapViewController.h
// EmergMap
//
// Created by Wenhui Zhang on 5/1/14.
// Copyright (c) 2014 Wenhui Zhang. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "EmergMapAnnotation.h"
@interface EmergMapViewController : UIViewController <MKMapViewDelegate>
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
/*
On the background thread, retrieve the Array of Annotations from the JSON from the next function.
On the main thread, add the annotations to the map.
*/
- (IBAction)addCitiesToMap:(id)sender;
- (void)addGestureRecogniserToMapView;
- (void)addPinToMap:(UIGestureRecognizer *) gestureRecognizer;
- (IBAction)addGestureRecogniserToMapView:(id)sender;
- (NSMutableArray *)parseJSONCities;
@end
//
// EmergMapViewController.m
// EmergMap
//
// Created by Wenhui Zhang on 5/1/14.
// Copyright (c) 2014 Wenhui Zhang. All rights reserved.
//
#import "EmergMapViewController.h"
#import "EmergMapAnnotation.h"
#import <MapKit/MapKit.h>
@interface EmergMapViewController ()
@end
@implementation EmergMapViewController
- (void)mapViewWillStartLoadingMap:(MKMapView *)mapView
{
self.mapView = mapView;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidLoad
{
[super viewDidLoad];
//[self.mapView setDelegate:self];
// Do any additional setup after loading the view, typically from a nib.
[self addGestureRecogniserToMapView];
}
- (void)addGestureRecogniserToMapView{
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(addPinToMap:)];
lpgr.minimumPressDuration = 0.5; //
[self.mapView addGestureRecognizer:lpgr];
}
- (void)addPinToMap:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];
CLLocationCoordinate2D touchMapCoordinate =
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];
EmergMapAnnotation *toAdd = [[EmergMapAnnotation alloc]init];
toAdd.coordinate = touchMapCoordinate;
toAdd.subtitle = @"Subtitle";
toAdd.title = @"Title";
[self.mapView addAnnotation:toAdd];
}
/*
On the background thread, retrieve the Array of Annotations from the JSON from the next function.
On the main thread, add the annotations to the map.
*/
- (IBAction)addCitiesToMap:(id)sender{
__block NSArray *annoations;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
annoations = [self parseJSONCities];
dispatch_async(dispatch_get_main_queue(), ^(void) {
// [self.mapView addAnnotations:annoations];
[self.mapView addAnnotations:annoations];
});
});
}
/*read json file*/
/*
Convert raw JSON to Objective-C Foundation Objects
Iterate over each returned object and create a JFMapAnnotationObject from it
Add each new Annotation to an Array and then return it.
*/
- (NSMutableArray *)parseJSONCities{
NSMutableArray *retval = [[NSMutableArray alloc]init];
NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"Pin_Cities" ofType:@"json"];
//NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"Pin_Cities" ofType:@"json" inDirectory:@"EmergMap/Pin_Cities.json"];
// /Users/WenhuiZhang/Desktop/EmergMap/EmergMap/Pin_Cities.json
NSData *data = [NSData dataWithContentsOfFile:jsonPath];
NSError *error = nil;
NSArray *json = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
for (EmergMapAnnotation *record in json) {
EmergMapAnnotation *temp = [[EmergMapAnnotation alloc]init];
[temp setTitle:[record valueForKey:@"Capital"]];
[temp setSubtitle:[record valueForKey:@"Country"]];
[temp setCoordinate:CLLocationCoordinate2DMake([[record valueForKey:@"Latitude"]floatValue], [[record valueForKey:@"Longitude"]floatValue])];
[retval addObject:temp];
}
return retval;
}
@end
//
// main.m
// EmergMap
//
// Created by Wenhui Zhang on 5/1/14.
// Copyright (c) 2014 Wenhui Zhang. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "EmergMapAppDelegate.h"
int main(int argc, char * argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([EmergMapAppDelegate class]));
}
}
[
{
"Country":"Washington, D.C.",
"Capital":"😱Amber Alert: 1-800-843-5678 ",
"Latitude":38.90,
"Longitude":-77.04
},
{
"Country":"Queens, N.Y.",
"Capital":"🚦Traffic Jam",
"Latitude":40.73,
"Longitude":-73.79
},
{
"Country":"Vilonia, AR.",
"Capital":"🌀Tornado",
"Latitude":35.08,
"Longitude":-92.21
},
{
"Country":"Baltimore, MD.",
"Capital":"☔️Flooding",
"Latitude":39.29,
"Longitude":-76.61
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment