apple mapkit地图样式 - 使用分段控制器实现样式更改

时间:2013-12-15 04:55:17

标签: ios maps apple-maps

我的应用程序中有几个地图视图。尝试添加一个简单的分段控制器开关,以便用户选择地图,卫星,混合地图。所有说明似乎都与谷歌地图有关;我正在使用Apple mapkit。

已更新

.h文件

#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文件

#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

1 个答案:

答案 0 :(得分:0)

你去男人..

在.h文件中声明一个ibaction,如下所示:

- (IBAction)setMap:(id)sender;

然后在.m文件中为其创建具有分段控制器的方法。如下:

- (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;
}
}

不要忘记连接你的ib中的分段控件。上面有三个段,所以你必须自己添加第三个段,因为默认情况下对象库中的分段控件有两个。