apple mapkit map style - implement style change using segmented controller [closed]

StackOverflow https://stackoverflow.com/questions/20591345

  •  01-09-2022
  •  | 
  •  

Domanda

I have several map views within our app. Trying to add a simple segmented controller switch to enable user choice of map, satellite, hybrid maps. All instruction seems to relate to google maps; I am using Apple mapkit.

UPDATED

.h file

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface TrucksViewController : UIViewController {

}

- (IBAction)setMap:(id)sender;

@property (weak, nonatomic) IBOutlet MKMapView *myMapView;
@property (weak, nonatomic) IBOutlet UISegmentedControl *mapType;

@end

.m file

#import "TrucksViewController.h"

@interface TrucksViewController ()


@end

@implementation TrucksViewController
@synthesize myMapView;

#pragma mark -
#pragma mark Initialisation

- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
    // Set the title for this view controller
    // Note: In future we will copy over the title from any created     UINavigationBar objects
    self.title = @"Trucks";

    [[UIApplication sharedApplication] setStatusBarHidden:NO     withAnimation:UIStatusBarAnimationNone];
}
return self;
}

#pragma mark -
#pragma mark UIViewController Delegates

- (IBAction)setMap:(id)sender {

switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
    case 0:
        myMapView.mapType = MKMapTypeStandard ;
        break;
    case 1:
        myMapView.mapType = MKMapTypeSatellite ;
        break;
    case 2:
        myMapView.mapType = MKMapTypeHybrid ;
        break;

    default:
        break;
}
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
[super viewDidUnload];
}

-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

// Update support iOS 7
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationController.navigationBar.translucent = NO;
}
}

-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

// Revert to default settings
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
    self.edgesForExtendedLayout = UIRectEdgeAll;
}
}

- (void)viewDidLoad 
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}


@end
È stato utile?

Soluzione

here you go man..

declare a ibaction in your .h file like the following:

- (IBAction)setMap:(id)sender;

and then in your .m file create the method for it that has the segmented controller within it. like follows:

- (IBAction)setMap:(id)sender {

switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
    case 0:
        myMapView.mapType = MKMapTypeStandard ;
        break;
    case 1:
        myMapView.mapType = MKMapTypeSatellite ;
        break;
    case 2:
        myMapView.mapType = MKMapTypeHybrid ;
        break;

    default:
        break;
}
}

don't forget to connect the segmented control in your ib. also the above has three segments so you have to add the third one by yourself since the segmented control from the object library has two by default.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top