Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // MapViewController.m
- // CPOP Test 4
- //
- // Created by Will Kerswell on 03/10/2012.
- // Copyright (c) 2012 Will Kerswell. All rights reserved.
- //
- #import "MapViewController.h"
- #import "UIDevice+Resolutions.h"
- #import "DataBase.h"
- @implementation MapViewController
- @synthesize showMapView;
- @synthesize mapSgementedControl;
- /////////////////////////////
- #pragma mark - Pin Display
- /////////////////////////////
- -(void)pins:(NSString *)name lat:(NSString *)lat lon:(NSString *)lon poiID:(NSString *)poiID catID:(NSString *)catID
- {
- NSLog(@"catids: %@",catID);
- //cast string to float
- CGFloat Lat = (CGFloat)[lat floatValue];
- CGFloat Lon = (CGFloat)[lon floatValue];
- //Set coordinates of pin
- CLLocationCoordinate2D coordinate;
- coordinate.latitude = Lat;
- coordinate.longitude = Lon;
- //custom anotation class
- MapAnnotation* annotation = [[MapAnnotation alloc]init];
- annotation.coordinate = coordinate;
- annotation.title = name;
- annotation.categoryID = catID;
- annotation.poiID = poiID;
- // Add pin to map
- [self.showMapView addAnnotation:annotation];
- }
- - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
- //create annotation
- MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];
- if (!pinView) {
- pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
- pinView.animatesDrop = FALSE;
- pinView.canShowCallout = YES;
- NSString* catID =[[NSString alloc]init];
- catID = [(MapAnnotation*)annotation categoryID];
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- //set the path where the file is
- NSString *filePath = [NSString stringWithFormat:@"%@/cat%@.png", documentsDirectory,catID];
- if([ catID isEqualToString: @"1"])
- {
- //pinView.pinColor = MKPinAnnotationColorGreen;
- pinView.image = [UIImage imageWithContentsOfFile:filePath];
- }else if([catID isEqualToString: @"2"]){
- pinView.image = [UIImage imageWithContentsOfFile:filePath];
- //pinView.pinColor = MKPinAnnotationColorPurple;
- }else if([catID isEqualToString: @"3"]){
- pinView.image = [UIImage imageWithContentsOfFile:filePath];
- //pinView.pinColor = MKPinAnnotationColorRed;
- }else {
- pinView.image = [UIImage imageWithContentsOfFile:filePath];
- //pinView.pinColor = MKPinAnnotationColorRed;
- }
- //details button
- UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
- pinView.rightCalloutAccessoryView = rightButton;
- } else {
- pinView.annotation = annotation;
- }
- return pinView;
- }
- //IMPLEMENT THIS ONE (WHEN DISCLOSURE BUTTON IS PRESSED):
- - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
- //create and annotation
- MapAnnotation *annotation = view.annotation;
- //set global var eq to the id
- self.POIID = annotation.poiID;
- //NSLog(@"Pin button pressed: %@",temp);
- [self performSegueWithIdentifier:@"showDetail" sender:self];
- }
- -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
- if ([[segue identifier] isEqualToString:@"showDetail"]) {
- //say where its going
- DetailsViewController *detailViewController = [segue destinationViewController];
- //set the poiID in the next view eq to the row pressed.
- detailViewController.poiID = self.POIID;
- }
- if ([[segue identifier] isEqualToString:@"showAlternate"]) {
- [[segue destinationViewController] setDelegate:self];
- }
- }
- /////////////////////////////
- #pragma mark - Map Didplay
- /////////////////////////////
- - (IBAction)mapSize:(id)sender {
- //Chane between local and wide view.
- switch (self.mapSgementedControl.selectedSegmentIndex) {
- case 0:
- NSLog(@"Wide");
- [self wideMap];
- break;
- case 1:
- NSLog(@"Local");
- [self localMap];
- default:
- break;
- }
- }
- -(void)wideMap{
- //zooms the map out so the user can see the surrondings
- showMapView.mapType= MKMapTypeStandard;
- MKCoordinateRegion newRegion;
- newRegion.center.latitude = LAT_POSN ;
- newRegion.center.longitude = LON_POSN;
- newRegion.span.latitudeDelta = 0.181168;
- newRegion.span.longitudeDelta = 0.43911;
- [self.showMapView setRegion:newRegion animated:YES];
- }
- -(void) localMap{
- //zooms the user into the local view so they can see the ground.
- showMapView.mapType= MKMapTypeStandard;
- MKCoordinateRegion newRegion;
- newRegion.center.latitude = LAT_POSN ;
- newRegion.center.longitude = LON_POSN;
- newRegion.span.latitudeDelta = 0.002053;
- newRegion.span.longitudeDelta = 0.005747;
- //old span
- //newRegion.span.latitudeDelta = 0.011323;
- //newRegion.span.longitudeDelta = 0.027444;
- [self.showMapView setRegion:newRegion animated:YES];
- }
- -(void)addPins{
- //create an instance of the database oject
- DataBase * dataBase = [[DataBase alloc] init];
- //open the database connection
- [dataBase openDB];
- //get the user defualts
- NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
- // getting an NSString
- NSDictionary *catToggle = [prefs dictionaryForKey:@"pinFilters"];
- //loop of the catids and toggles from the user defaults
- for (NSString *keys in catToggle) {
- //get on or off
- NSString *toggle = [[NSString alloc]init];
- toggle = [catToggle objectForKey:keys];
- if (toggle == @"ON"){
- NSMutableDictionary *poisForOneCat = [[NSMutableDictionary alloc]init ];
- //if on get the id name lat and long for the pois in that cat
- poisForOneCat = [dataBase getPOIInfoForMap:keys];
- for(NSString *key in poisForOneCat) {
- //loop over the pois in that cat can put them on the map!
- NSMutableArray *arrayOfPinInformation = [poisForOneCat objectForKey:key];
- NSLog(@"arrayOfPinInformation: %@",arrayOfPinInformation);
- [self pins:key lat:arrayOfPinInformation[1] lon:arrayOfPinInformation[2] poiID:arrayOfPinInformation[0] catID:keys] ;
- }
- }
- }
- }
- /////////////////////////////
- #pragma mark - View Licfcycle
- /////////////////////////////
- - (void)viewDidLoad
- {
- //makes the top title black in the more or the tabbar view
- self.tabBarController.moreNavigationController.navigationBar.tintColor = [UIColor blackColor];
- //Test to see if the user is using an iPhone 5
- //This can be to change things depending on the screen size if ([UIDevice deviceType] & iPhone5){
- NSLog(@"iPhone 5");
- //initialise array
- self.daoDS = [[POIDataDAO alloc] init];
- //set array of ojects to loacl array
- // self.ds = self.daoDS.PopulateDataSource;
- //set map delegate
- showMapView.delegate = (id)self;
- [self addPins];
- //force the map to start on the main queue
- dispatch_async(dispatch_get_main_queue(), ^{[self localMap];});
- [super viewDidLoad];
- }
- //other lifecycle methods.
- -(void)mapPopulate {
- //initialise array
- self.daoDS = [[POIDataDAO alloc] init];
- //set array of ojects to loacl array
- // self.ds = self.daoDS.PopulateDataSource;
- //set map delegate
- showMapView.delegate = (id)self;
- [self addPins];
- dispatch_async(dispatch_get_main_queue(), ^{[self localMap];});
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)viewDidUnload {
- [super viewDidUnload];
- }
- - (void)tabBarController:(UITabBarController *)controller willBeginCustomizingViewControllers:(NSArray *)viewControllers {
- UIView *editView = [controller.view.subviews objectAtIndex:1];
- UINavigationBar *modalNavBar = [editView.subviews objectAtIndex:0];
- modalNavBar.tintColor = [UIColor blackColor];
- }
- #pragma mark - Flipside View
- - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
- {
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- @end
- //
- // FlipsideViewController.m
- // lol
- //
- // Created by Will Kerswell on 29/10/2012.
- // Copyright (c) 2012 Will Kerswell. All rights reserved.
- //
- #import "FlipsideViewController.h"
- @interface FlipsideViewController ()
- @end
- @implementation FlipsideViewController
- //////////////////////////////////
- #pragma mark - View Lifecycle
- //////////////////////////////////
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- //Change the colour of the top navigation on the navigationViewController.
- self.navigationController.navigationBar.tintColor = [UIColor blackColor];
- //create an instance of the database oject
- DataBase * dataBase = [[DataBase alloc] init];
- //open the database connection
- [dataBase openDB];
- self.categoriesDic = [dataBase getCatNames];
- //create array for the names and ids
- NSMutableArray *namesHolder = [[NSMutableArray alloc]init];
- NSMutableArray *catIDsHolder = [[NSMutableArray alloc]init];
- //loop through dic and get names.
- for(NSString *key in self.categoriesDic) {
- //add the names to array
- [namesHolder addObject:key];
- }
- //sort the names
- [namesHolder sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
- //get the id from the dic with the key
- for (int i=0; i<namesHolder.count; i++) {
- //add id to the holder using key from dic to make sure the order is correct
- [catIDsHolder addObject:[self.categoriesDic objectForKey:namesHolder[i]]];
- }
- //set global arrya = to loacl array
- self.catNames = namesHolder;
- self.catIDs = catIDsHolder;
- //get the user defualts
- NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
- //get dic
- NSDictionary *Dic = [prefs dictionaryForKey:@"pinFilters"];
- NSLog(@"This is what is stored in the users settings: %@",Dic);
- //create new dic with old dic content.
- NSMutableDictionary *newDic = [[NSMutableDictionary alloc]init];
- for (NSString *keys in Dic) {
- [newDic setObject:[Dic objectForKey:keys] forKey:keys];
- }
- self.filterDic = newDic;
- //create array for the pin toggle mode in sorted order like cat names and ids
- NSMutableArray *toggleHolder = [[NSMutableArray alloc]init];
- for (int i=0; i<self.catIDs.count; i++) {
- //add id to the holder using key from dic to make sure the order is correct
- [toggleHolder addObject:[self.filterDic objectForKey:self.catIDs[i]]];
- }
- self.toggleArray = toggleHolder;
- NSLog(@"Toggle array: %@",self.toggleArray);
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- //////////////////////////////////
- #pragma mark - Populate TableView
- //////////////////////////////////
- -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
- return 1;
- }
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
- return @"Categories";
- }
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return self.catNames.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- static NSString *CellIdentifier = @"POICell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
- //set the cell text to the
- cell.textLabel.text = [self.catNames objectAtIndex:[indexPath row]];
- NSString *toggle = [[NSString alloc]init];
- toggle = [self.toggleArray objectAtIndex:[indexPath row]];
- //add switch
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
- UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
- cell.accessoryView = switchView;
- if (toggle == @"OFF") {
- [switchView setOn:NO animated:NO];
- }else{
- [switchView setOn:YES animated:NO];
- }
- [switchView addTarget:self action:@selector(switchChanged: ) forControlEvents:UIControlEventValueChanged];
- // Configure the cell...
- return cell;
- }
- - (void) switchChanged:(id)sender {
- //get the switch that it was sent from
- UISwitch *switchInCell = (UISwitch *)sender;
- //get the cell it was sent from
- UITableViewCell * cell = (UITableViewCell *) switchInCell.superview;
- //get the row it was sent from
- NSIndexPath * indexpath = [self.inputTableView indexPathForCell:cell];
- //cast the indexpath to int
- NSInteger variable = indexpath.row;
- //set the catID from the row index
- NSString *StrCatID =[[NSString alloc]init];
- StrCatID = [self.catIDs objectAtIndex:variable];
- //set the filter as off in the user defualts.
- [self.filterDic setValue:switchInCell.on ? @"ON" : @"OFF" forKey:StrCatID];
- //store the newdic in the user defualts
- NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
- //save the dic to user defaults
- [prefs setObject:self.filterDic forKey:@"pinFilters"];
- //NSLog(@"New dic Changed: %@",self.filterDic);
- //NSLog( @"The switch for item %@ is %@",StrCatID, switchInCell.on ? @"ON" : @"OFF" );
- }
- #pragma mark - Actions
- - (IBAction)done:(id)sender
- {
- [self.delegate flipsideViewControllerDidFinish:self];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment