Advertisement
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 "MKMapView+ZoomLevel.h"
- bool manuallyChangingMapRect = NO;
- MKMapPoint overlayPosnTL;
- MKMapPoint overlayPosnBR;
- MKZoomScale minZoom ;
- double minZoomWidth;
- double minZoomHeight;
- @implementation MapViewController
- @synthesize showMapView;
- @synthesize mapSgementedControl;
- /////////////////////////////
- #pragma mark -
- #pragma mark - Map Display (pins and overlay)
- /////////////////////////////
- -(void) localMap{
- //set toggle so we know when to lock the map
- self.toggle = @"local";
- //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.009743;
- newRegion.span.longitudeDelta = 0.012808;
- [self.showMapView setRegion:newRegion animated:YES];
- }
- /////////////////////////////
- #pragma mark -
- #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];
- //create a new insatnce of the map overlay object
- MapOverlay * mapOverlay = [[MapOverlay alloc] init];
- //added the over lay
- [self.showMapView addOverlay:mapOverlay];
- //set map delegate
- showMapView.delegate = (id)self;
- //set the two corner coords so that we can move back to them when the user moves too far away from the map
- CLLocationCoordinate2D overlayLatLongTL;
- overlayLatLongTL.latitude = OVERLAY_T;
- overlayLatLongTL.longitude = OVERLAY_L;
- overlayPosnTL = MKMapPointForCoordinate(overlayLatLongTL);
- CLLocationCoordinate2D overlayLatLongBR;
- overlayLatLongBR.latitude = OVERLAY_B;
- overlayLatLongBR.longitude = OVERLAY_R;
- overlayPosnBR = MKMapPointForCoordinate(overlayLatLongBR);
- //set the region on map load. also force it to be on the main queue
- dispatch_async(dispatch_get_main_queue(), ^{[self localMap];});
- [super viewDidLoad];
- }
- -(void)mapPopulate{
- //remove any pins still on the map so that we can add the ones that the user has filtered
- [showMapView removeAnnotations:showMapView.annotations];
- //run the function that adds all the pins to the map.
- [self addPins];
- }
- -(void)viewDidAppear:(BOOL)animated{
- //this method is run when the flipview has been dismissed and the view appears
- //this method Re-Adds the pins to the map according to the filters
- [self mapPopulate];
- //load the map again
- [self showMapView];
- //set the rect fo the visible map
- MKMapRect visibleMap = self.showMapView.visibleMapRect;
- //get the min zoom
- minZoom = visibleMap.size.width / self.showMapView.bounds.size.width;
- //sent the min width and height to the initial size of the map
- minZoomWidth = visibleMap.size.width;
- minZoomHeight = visibleMap.size.height;
- //This is what im getting on my iPhone 5
- // minZoom= 29.846548;
- // minZoomWidth = 9550.895107;
- // minZoomHeight = 29.846548;
- //this is what im getting on the sim
- //minZoom = 262143.968750;
- //minZoomWidth = 83886070.283277;
- //minZoomHeight = 262143.968750;
- NSLog(@"minZoom: %f",minZoom);
- NSLog(@"minZoomWidth: %f",minZoomWidth);
- NSLog(@"minZoomHeight: %f",minZoom);
- [super viewDidAppear:animated];
- }
- /////////////////////////////
- #pragma mark -
- #pragma mark - Flipside View
- /////////////////////////////
- - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller{
- [self dismissViewControllerAnimated:YES completion:Nil];
- }
- /////////////////////////////
- #pragma mark -
- #pragma mark - restrictions
- /////////////////////////////
- - (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
- {
- //Check to see if the view is local or wide. (Only lock the map if local)
- if ([self.toggle isEqualToString:@"local"]) {
- if (manuallyChangingMapRect) return; // Prevent infinite loops!
- //get the map rect and use it to find the current zoomlevel
- MKMapRect visibleMap = self.showMapView.visibleMapRect;
- MKZoomScale zl = visibleMap.size.width / self.showMapView.bounds.size.width;
- if (zl > minZoom)
- {
- NSLog(@"zoom too far out! = %f", zl);
- //set the visible maps izes back to the mins
- visibleMap.size.width = minZoomWidth;
- visibleMap.size.height = minZoomHeight;
- manuallyChangingMapRect = YES;
- [self.showMapView setVisibleMapRect: visibleMap animated: YES];
- manuallyChangingMapRect = NO;
- }
- //this bit moves the user back to the map.
- //first move them in the x plain
- if (visibleMap.origin.x < overlayPosnTL.x)
- {
- visibleMap.origin.x = overlayPosnTL.x + MAP_LEFT_CORRECT;
- }
- else
- if (visibleMap.origin.x + visibleMap.size.width > overlayPosnBR.x)
- visibleMap.origin.x = overlayPosnBR.x - visibleMap.size.width + MAP_RIGHT_CORRECT;
- //then move them in the y plain
- if (visibleMap.origin.y < overlayPosnTL.y)
- {
- visibleMap.origin.y = overlayPosnTL.y + MAP_TOP_CORRECT;
- }
- else
- if (visibleMap.origin.y + visibleMap.size.height > overlayPosnBR.y)
- visibleMap.origin.y = overlayPosnBR.y - visibleMap.size.height + MAP_BOTTOM_CORRECT;
- manuallyChangingMapRect = YES;
- [self.showMapView setVisibleMapRect: visibleMap animated: YES];
- manuallyChangingMapRect = NO;
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement