Advertisement
wkerswell

Full map view

Nov 14th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  MapViewController.m
  3. //  CPOP Test 4
  4. //
  5. //  Created by Will Kerswell on 03/10/2012.
  6. //  Copyright (c) 2012 Will Kerswell. All rights reserved.
  7. //
  8.  
  9. #import "MapViewController.h"
  10. #import "MKMapView+ZoomLevel.h"
  11.  
  12. bool    manuallyChangingMapRect = NO;
  13.  
  14. MKMapPoint  overlayPosnTL;
  15. MKMapPoint  overlayPosnBR;
  16. MKZoomScale minZoom ;
  17.  
  18. double minZoomWidth;
  19. double minZoomHeight;
  20.  
  21. @implementation MapViewController
  22. @synthesize showMapView;
  23. @synthesize mapSgementedControl;
  24.  
  25.  
  26.  
  27. /////////////////////////////
  28. #pragma mark -
  29. #pragma mark - Map Display (pins and overlay)
  30. /////////////////////////////
  31.  
  32. -(void)addPins{
  33.    
  34.     //create an instance of the database oject
  35.     DataBase * dataBase = [[DataBase alloc] init];
  36.     //open the database connection
  37.     [dataBase openDB];
  38.     //get the user defualts
  39.     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  40.     // getting an NSString
  41.     NSDictionary *catToggle = [prefs dictionaryForKey:@"pinFilters"];
  42.    
  43.     //loop of the catids and toggles from the user defaults
  44.     for (NSString *keys in catToggle) {
  45.        
  46.         //get on or off
  47.         NSString *toggle = [catToggle objectForKey:keys];
  48.        
  49.         if ([toggle isEqualToString:@"ON"]){
  50.             //if on get the id name lat and long for the pois in that cat
  51.             NSMutableDictionary *poisForOneCat = [dataBase getPOIInfoForMap:keys];
  52.            
  53.            
  54.             for(NSString *key in poisForOneCat) {
  55.                 //loop over the pois in that cat can put them on the map!
  56.                 NSMutableArray *arrayOfPinInformation = [poisForOneCat objectForKey:key];
  57.                 [self pins:key lat:arrayOfPinInformation[1] lon:arrayOfPinInformation[2] poiID:arrayOfPinInformation[0] catID:keys] ;
  58.                
  59.             }
  60.         }
  61.     }
  62.    
  63.    
  64.    
  65. }
  66.  
  67. -(void)pins:(NSString *)name lat:(NSString *)lat lon:(NSString *)lon poiID:(NSString *)poiID catID:(NSString *)catID{
  68.    
  69.     //Set coordinates of pin
  70.     CLLocationCoordinate2D coordinate;
  71.     coordinate.latitude = [lat floatValue];
  72.     coordinate.longitude = [lon floatValue];
  73.    
  74.     //custom anotation class
  75.     MapAnnotation* annotation = [[MapAnnotation alloc]init];
  76.     annotation.coordinate = coordinate;
  77.     annotation.title = name;
  78.     annotation.categoryID = catID;
  79.     annotation.poiID = poiID;
  80.     annotation.pinURL = [NSURL adnURLInDocumentDirectoryForFilename:[NSString stringWithFormat:@"cat%@.png", catID]];
  81.    
  82.     // Add pin to map
  83.     [self.showMapView addAnnotation:annotation];
  84.    
  85.    
  86.    
  87. }
  88.  
  89. -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{
  90.    
  91.     //added the image to the over and return it.
  92.     MapOverlay *mapOverlay = (MapOverlay *)overlay;
  93.     MapOverlayView *mapOverlayView = [[MapOverlayView alloc] initWithOverlay:mapOverlay] ;
  94.    
  95.     return mapOverlayView;
  96.    
  97. }
  98.  
  99. - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
  100.    
  101.     //this makes sure that the user location looks the same and does not get replace with a different pin
  102.     if ([annotation isKindOfClass:[MKUserLocation class]]) {
  103.         //Don't trample the user location annotation (pulsing blue dot).
  104.         return nil;
  105.     }
  106.    
  107.    
  108.     //create new anotation with content of annotation object.
  109.     MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
  110.     //set this off to stop the map zooming in each time the filter button is pressed
  111.     pinView.animatesDrop = FALSE;
  112.     pinView.canShowCallout = YES;
  113.    
  114.     //set the pin image
  115.     pinView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[(MapAnnotation*)annotation pinURL]]];
  116.    
  117.    
  118.     //details button
  119.     UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  120.     pinView.rightCalloutAccessoryView = rightButton;
  121.    
  122.     return pinView;
  123. }
  124.  
  125. - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
  126.     //IMPLEMENT THIS ONE (WHEN DISCLOSURE BUTTON IS PRESSED)
  127.    
  128.     //create and annotation
  129.     MapAnnotation *annotation = view.annotation;
  130.     //set global var eq to the id
  131.     self.POIID = annotation.poiID;
  132.     [self performSegueWithIdentifier:@"showDetail" sender:self];
  133.    
  134. }
  135.  
  136. /////////////////////////////
  137. #pragma mark -
  138. #pragma mark - Map Size
  139. /////////////////////////////
  140.  
  141. - (IBAction)mapSize:(id)sender {
  142.     //Chane between local and wide view.
  143.     switch (self.mapSgementedControl.selectedSegmentIndex) {
  144.         case 0:
  145.            
  146.             [self wideMap];
  147.             break;
  148.         case 1:
  149.            
  150.             [self localMap];
  151.         default:
  152.             break;
  153.     }
  154. }
  155.  
  156. -(void)wideMap{
  157.     //set toggle so we know when to lock the map
  158.     self.toggle = @"wide";
  159.    
  160.     //zooms the map out so the user can see the surrondings
  161.     showMapView.mapType= MKMapTypeStandard;
  162.     MKCoordinateRegion newRegion;
  163.     newRegion.center.latitude = LAT_POSN ;
  164.     newRegion.center.longitude = LON_POSN;
  165.     newRegion.span.latitudeDelta = 0.181168;
  166.     newRegion.span.longitudeDelta = 0.43911;
  167.     [self.showMapView setRegion:newRegion animated:YES];
  168.    
  169. }
  170.  
  171. -(void) localMap{
  172.     //set toggle so we know when to lock the map
  173.     self.toggle = @"local";
  174.    
  175.     //zooms the user into the local view so they can see the ground.
  176.     showMapView.mapType= MKMapTypeStandard;
  177.     MKCoordinateRegion newRegion;
  178.     newRegion.center.latitude = LAT_POSN ;
  179.     newRegion.center.longitude = LON_POSN;
  180.     newRegion.span.latitudeDelta = 0.009743;
  181.     newRegion.span.longitudeDelta = 0.012808;
  182.     [self.showMapView setRegion:newRegion animated:YES];
  183.    
  184. }
  185.  
  186.  
  187.  
  188. /////////////////////////////
  189. #pragma mark -
  190. #pragma mark - View Licfcycle
  191. /////////////////////////////
  192.  
  193. - (void)viewDidLoad{
  194.     //makes the top title black in the more or the tabbar view
  195.     self.tabBarController.moreNavigationController.navigationBar.tintColor = [UIColor blackColor];
  196.    
  197.     //create a new insatnce of the map overlay object
  198.     MapOverlay * mapOverlay = [[MapOverlay alloc] init];
  199.     //added the over lay
  200.     [self.showMapView addOverlay:mapOverlay];
  201.    
  202.     //set map delegate
  203.     showMapView.delegate = (id)self;
  204.    
  205.     //set the two corner coords so that we can move back to them when the user moves too far away from the map
  206.     CLLocationCoordinate2D overlayLatLongTL;
  207.     overlayLatLongTL.latitude  = OVERLAY_T;
  208.     overlayLatLongTL.longitude = OVERLAY_L;
  209.    
  210.     overlayPosnTL = MKMapPointForCoordinate(overlayLatLongTL);
  211.    
  212.     CLLocationCoordinate2D overlayLatLongBR;
  213.     overlayLatLongBR.latitude  = OVERLAY_B;
  214.     overlayLatLongBR.longitude = OVERLAY_R;
  215.    
  216.     overlayPosnBR = MKMapPointForCoordinate(overlayLatLongBR);
  217.    
  218.     //set the region on map load. also force it to be on the main queue
  219.     dispatch_async(dispatch_get_main_queue(), ^{[self localMap];});
  220.    
  221.     [super viewDidLoad];
  222. }
  223.  
  224. -(void)mapPopulate{
  225.    
  226.     //remove any pins still on the map so that we can add the ones that the user has filtered
  227.     [showMapView removeAnnotations:showMapView.annotations];
  228.     //run the function that adds all the pins to the map.
  229.     [self addPins];
  230.    
  231.    
  232. }
  233.  
  234. - (void)didReceiveMemoryWarning{
  235.     [super didReceiveMemoryWarning];
  236.     // Dispose of any resources that can be recreated.
  237. }
  238.  
  239. - (void)viewDidUnload {
  240.    
  241.     [super viewDidUnload];
  242. }
  243.  
  244. - (void)tabBarController:(UITabBarController *)controller willBeginCustomizingViewControllers:(NSArray *)viewControllers {
  245.     UIView *editView = [controller.view.subviews objectAtIndex:1];
  246.     UINavigationBar *modalNavBar = [editView.subviews objectAtIndex:0];
  247.     modalNavBar.tintColor = [UIColor blackColor];
  248. }
  249.  
  250. -(void)viewDidAppear:(BOOL)animated{
  251.     //this method is run when the flipview has been dismissed and the view appears
  252.     //this method Re-Adds the pins to the map according to the filters
  253.     [self mapPopulate];
  254.     //load the map again
  255.     [self showMapView];
  256.    
  257.     //set the rect fo the visible map
  258.     MKMapRect visibleMap = self.showMapView.visibleMapRect;
  259.    
  260.     //get the min zoom
  261.     minZoom = visibleMap.size.width / self.showMapView.bounds.size.width;
  262.    
  263.     //sent the min width and height to the initial size of the map
  264.     minZoomWidth  = visibleMap.size.width;
  265.     minZoomHeight = visibleMap.size.height;
  266.    
  267.     //This is what im getting on my iPhone 5
  268.     //    minZoom= 29.846548;
  269.     //    minZoomWidth = 9550.895107;
  270.     //    minZoomHeight = 29.846548;
  271.    
  272.     //this is what im getting on the sim
  273.     //minZoom = 262143.968750;
  274.     //minZoomWidth = 83886070.283277;
  275.     //minZoomHeight = 262143.968750;
  276.    
  277.     NSLog(@"minZoom: %f",minZoom);
  278.     NSLog(@"minZoomWidth: %f",minZoomWidth);
  279.     NSLog(@"minZoomHeight: %f",minZoom);
  280.    
  281.     [super viewDidAppear:animated];
  282.    
  283. }
  284.  
  285. /////////////////////////////
  286. #pragma mark -
  287. #pragma mark - Flipside View
  288. /////////////////////////////
  289.  
  290. - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller{
  291.     [self dismissViewControllerAnimated:YES completion:Nil];
  292. }
  293.  
  294. /////////////////////////////
  295. #pragma mark -
  296. #pragma mark - restrictions
  297. /////////////////////////////
  298.  
  299.  
  300. - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
  301. {
  302.     //Check to see if the view is local or wide. (Only lock the map if local)
  303.     if ([self.toggle isEqualToString:@"local"]) {
  304.        
  305.        
  306.         if (manuallyChangingMapRect) return; // Prevent infinite loops!
  307.        
  308.         //get the map rect and use it to find the current zoomlevel
  309.         MKMapRect visibleMap = self.showMapView.visibleMapRect;
  310.         MKZoomScale zl = visibleMap.size.width / self.showMapView.bounds.size.width;
  311.        
  312.         if (zl > minZoom)
  313.         {
  314.             NSLog(@"zoom too far out! = %f", zl);
  315.             //set the visible maps izes back to the mins
  316.             visibleMap.size.width  = minZoomWidth;
  317.             visibleMap.size.height = minZoomHeight;
  318.            
  319.             manuallyChangingMapRect = YES;
  320.             [self.showMapView setVisibleMapRect: visibleMap animated: YES];
  321.             manuallyChangingMapRect = NO;
  322.         }
  323.        
  324.         //this bit moves the user back to the map.
  325.         //first move them in the x plain
  326.         if (visibleMap.origin.x < overlayPosnTL.x)
  327.         {
  328.             visibleMap.origin.x = overlayPosnTL.x + MAP_LEFT_CORRECT;
  329.         }
  330.         else
  331.             if (visibleMap.origin.x + visibleMap.size.width > overlayPosnBR.x)
  332.                 visibleMap.origin.x = overlayPosnBR.x - visibleMap.size.width + MAP_RIGHT_CORRECT;
  333.        
  334.        
  335.         //then move them in the y plain
  336.         if (visibleMap.origin.y < overlayPosnTL.y)
  337.         {
  338.             visibleMap.origin.y = overlayPosnTL.y + MAP_TOP_CORRECT;
  339.         }
  340.         else
  341.             if (visibleMap.origin.y + visibleMap.size.height > overlayPosnBR.y)
  342.                 visibleMap.origin.y = overlayPosnBR.y - visibleMap.size.height + MAP_BOTTOM_CORRECT;
  343.        
  344.        
  345.        
  346.         manuallyChangingMapRect = YES;
  347.         [self.showMapView setVisibleMapRect: visibleMap animated: YES];
  348.         manuallyChangingMapRect = NO;
  349.     }
  350.  
  351. }
  352.  
  353. /////////////////////////////
  354. #pragma mark -
  355. #pragma mark - prepareForSegue
  356. /////////////////////////////
  357.  
  358. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  359.    
  360.     //details view
  361.     if ([[segue identifier] isEqualToString:@"showDetail"]) {
  362.        
  363.         //say where its going
  364.         DetailsViewController *detailViewController = [segue destinationViewController];
  365.         //set the poiID in the next view eq to the row pressed.
  366.         detailViewController.poiID = self.POIID;
  367.        
  368.     }
  369.    
  370.     //flip view
  371.     if ([[segue identifier] isEqualToString:@"showAlternate"]) {
  372.         [[segue destinationViewController] setDelegate:self];
  373.     }
  374.    
  375.    
  376. }
  377.  
  378.  
  379.  
  380. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement