Advertisement
wkerswell

map view.

Nov 9th, 2012
149
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.  
  33. -(void) localMap{
  34.     //set toggle so we know when to lock the map
  35.     self.toggle = @"local";
  36.    
  37.     //zooms the user into the local view so they can see the ground.
  38.     showMapView.mapType= MKMapTypeStandard;
  39.     MKCoordinateRegion newRegion;
  40.     newRegion.center.latitude = LAT_POSN ;
  41.     newRegion.center.longitude = LON_POSN;
  42.     newRegion.span.latitudeDelta = 0.009743;
  43.     newRegion.span.longitudeDelta = 0.012808;
  44.    [self.showMapView setRegion:newRegion animated:YES];
  45.    
  46. }
  47.  
  48.  
  49.  
  50. /////////////////////////////
  51. #pragma mark -
  52. #pragma mark - View Licfcycle
  53. /////////////////////////////
  54.  
  55. - (void)viewDidLoad{
  56.     //makes the top title black in the more or the tabbar view
  57.     self.tabBarController.moreNavigationController.navigationBar.tintColor = [UIColor blackColor];
  58.  
  59.     //create a new insatnce of the map overlay object
  60.     MapOverlay * mapOverlay = [[MapOverlay alloc] init];
  61.     //added the over lay
  62.     [self.showMapView addOverlay:mapOverlay];
  63.    
  64.     //set map delegate
  65.     showMapView.delegate = (id)self;
  66.    
  67.     //set the two corner coords so that we can move back to them when the user moves too far away from the map
  68.     CLLocationCoordinate2D overlayLatLongTL;
  69.     overlayLatLongTL.latitude  = OVERLAY_T;
  70.     overlayLatLongTL.longitude = OVERLAY_L;
  71.    
  72.     overlayPosnTL = MKMapPointForCoordinate(overlayLatLongTL);
  73.    
  74.     CLLocationCoordinate2D overlayLatLongBR;
  75.     overlayLatLongBR.latitude  = OVERLAY_B;
  76.     overlayLatLongBR.longitude = OVERLAY_R;
  77.    
  78.     overlayPosnBR = MKMapPointForCoordinate(overlayLatLongBR);
  79.  
  80.     //set the region on map load. also force it to be on the main queue
  81.     dispatch_async(dispatch_get_main_queue(), ^{[self localMap];});
  82.  
  83. [super viewDidLoad];
  84. }
  85.  
  86. -(void)mapPopulate{
  87.    
  88.     //remove any pins still on the map so that we can add the ones that the user has filtered
  89.     [showMapView removeAnnotations:showMapView.annotations];
  90.     //run the function that adds all the pins to the map.
  91.     [self addPins];
  92.    
  93.  
  94. }
  95.  
  96.  
  97. -(void)viewDidAppear:(BOOL)animated{
  98.     //this method is run when the flipview has been dismissed and the view appears
  99.     //this method Re-Adds the pins to the map according to the filters
  100.     [self mapPopulate];
  101.     //load the map again
  102.     [self showMapView];
  103.    
  104.     //set the rect fo the visible map
  105.     MKMapRect visibleMap = self.showMapView.visibleMapRect;
  106.    
  107.     //get the min zoom
  108.     minZoom = visibleMap.size.width / self.showMapView.bounds.size.width;
  109.    
  110.     //sent the min width and height to the initial size of the map
  111.     minZoomWidth  = visibleMap.size.width;
  112.     minZoomHeight = visibleMap.size.height;
  113.    
  114.     //This is what im getting on my iPhone 5
  115.     //    minZoom= 29.846548;
  116.     //    minZoomWidth = 9550.895107;
  117.     //    minZoomHeight = 29.846548;
  118.    
  119.     //this is what im getting on the sim
  120.     //minZoom = 262143.968750;
  121.     //minZoomWidth = 83886070.283277;
  122.     //minZoomHeight = 262143.968750;
  123.    
  124.     NSLog(@"minZoom: %f",minZoom);
  125.     NSLog(@"minZoomWidth: %f",minZoomWidth);
  126.     NSLog(@"minZoomHeight: %f",minZoom);
  127.    
  128.     [super viewDidAppear:animated];
  129.    
  130. }
  131.  
  132. /////////////////////////////
  133. #pragma mark -
  134. #pragma mark - Flipside View
  135. /////////////////////////////
  136.  
  137. - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller{
  138.     [self dismissViewControllerAnimated:YES completion:Nil];
  139. }
  140.  
  141. /////////////////////////////
  142. #pragma mark -
  143. #pragma mark - restrictions
  144. /////////////////////////////
  145.  
  146.  
  147. - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
  148. {
  149.     //Check to see if the view is local or wide. (Only lock the map if local)
  150.     if ([self.toggle isEqualToString:@"local"]) {
  151.        
  152.    
  153.         if (manuallyChangingMapRect) return; // Prevent infinite loops!
  154.        
  155.         //get the map rect and use it to find the current zoomlevel
  156.         MKMapRect visibleMap = self.showMapView.visibleMapRect;
  157.         MKZoomScale zl = visibleMap.size.width / self.showMapView.bounds.size.width;
  158.              
  159.         if (zl > minZoom)
  160.         {
  161.             NSLog(@"zoom too far out! = %f", zl);
  162.             //set the visible maps izes back to the mins
  163.             visibleMap.size.width  = minZoomWidth;
  164.             visibleMap.size.height = minZoomHeight;
  165.            
  166.             manuallyChangingMapRect = YES;
  167.             [self.showMapView setVisibleMapRect: visibleMap animated: YES];
  168.             manuallyChangingMapRect = NO;
  169.         }
  170.        
  171.         //this bit moves the user back to the map.
  172.         //first move them in the x plain
  173.         if (visibleMap.origin.x < overlayPosnTL.x)
  174.         {
  175.             visibleMap.origin.x = overlayPosnTL.x + MAP_LEFT_CORRECT;
  176.         }
  177.         else
  178.             if (visibleMap.origin.x + visibleMap.size.width > overlayPosnBR.x)
  179.                 visibleMap.origin.x = overlayPosnBR.x - visibleMap.size.width + MAP_RIGHT_CORRECT;
  180.        
  181.        
  182.         //then move them in the y plain
  183.         if (visibleMap.origin.y < overlayPosnTL.y)
  184.         {
  185.             visibleMap.origin.y = overlayPosnTL.y + MAP_TOP_CORRECT;
  186.         }
  187.         else
  188.             if (visibleMap.origin.y + visibleMap.size.height > overlayPosnBR.y)
  189.                 visibleMap.origin.y = overlayPosnBR.y - visibleMap.size.height + MAP_BOTTOM_CORRECT;
  190.        
  191.  
  192.        
  193.         manuallyChangingMapRect = YES;
  194.         [self.showMapView setVisibleMapRect: visibleMap animated: YES];
  195.         manuallyChangingMapRect = NO;
  196.     }
  197.    
  198. }
  199.  
  200.  
  201.  
  202.  
  203. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement