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)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 = [catToggle objectForKey:keys];
- if ([toggle isEqualToString:@"ON"]){
- //if on get the id name lat and long for the pois in that cat
- NSMutableDictionary *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];
- [self pins:key lat:arrayOfPinInformation[1] lon:arrayOfPinInformation[2] poiID:arrayOfPinInformation[0] catID:keys] ;
- }
- }
- }
- }
- -(void)pins:(NSString *)name lat:(NSString *)lat lon:(NSString *)lon poiID:(NSString *)poiID catID:(NSString *)catID{
- //Set coordinates of pin
- CLLocationCoordinate2D coordinate;
- coordinate.latitude = [lat floatValue];
- coordinate.longitude = [lon floatValue];
- //custom anotation class
- MapAnnotation* annotation = [[MapAnnotation alloc]init];
- annotation.coordinate = coordinate;
- annotation.title = name;
- annotation.categoryID = catID;
- annotation.poiID = poiID;
- annotation.pinURL = [NSURL adnURLInDocumentDirectoryForFilename:[NSString stringWithFormat:@"cat%@.png", catID]];
- // Add pin to map
- [self.showMapView addAnnotation:annotation];
- }
- -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay{
- //added the image to the over and return it.
- MapOverlay *mapOverlay = (MapOverlay *)overlay;
- MapOverlayView *mapOverlayView = [[MapOverlayView alloc] initWithOverlay:mapOverlay] ;
- return mapOverlayView;
- }
- - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
- //this makes sure that the user location looks the same and does not get replace with a different pin
- if ([annotation isKindOfClass:[MKUserLocation class]]) {
- //Don't trample the user location annotation (pulsing blue dot).
- return nil;
- }
- //create new anotation with content of annotation object.
- MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
- //set this off to stop the map zooming in each time the filter button is pressed
- pinView.animatesDrop = FALSE;
- pinView.canShowCallout = YES;
- //set the pin image
- pinView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[(MapAnnotation*)annotation pinURL]]];
- //details button
- UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
- pinView.rightCalloutAccessoryView = rightButton;
- return pinView;
- }
- - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
- //IMPLEMENT THIS ONE (WHEN DISCLOSURE BUTTON IS PRESSED)
- //create and annotation
- MapAnnotation *annotation = view.annotation;
- //set global var eq to the id
- self.POIID = annotation.poiID;
- [self performSegueWithIdentifier:@"showDetail" sender:self];
- }
- /////////////////////////////
- #pragma mark -
- #pragma mark - Map Size
- /////////////////////////////
- - (IBAction)mapSize:(id)sender {
- //Chane between local and wide view.
- switch (self.mapSgementedControl.selectedSegmentIndex) {
- case 0:
- [self wideMap];
- break;
- case 1:
- [self localMap];
- default:
- break;
- }
- }
- -(void)wideMap{
- //set toggle so we know when to lock the map
- self.toggle = @"wide";
- //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{
- //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)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];
- }
- -(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;
- }
- }
- /////////////////////////////
- #pragma mark -
- #pragma mark - prepareForSegue
- /////////////////////////////
- -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
- //details view
- 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;
- }
- //flip view
- if ([[segue identifier] isEqualToString:@"showAlternate"]) {
- [[segue destinationViewController] setDelegate:self];
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement