地图导航应用几乎是每一步手机中必备的软件。在地图中可以进行很多操作,例如通过 GPS 获取用户当前位置,指定某一经纬度的位置,添加标注等。今天只是总结一种关于地图类型的切换。
本实例的关键是地图的类型改变。地图的类型是通过MKMapView的 mapType 属性指定的
MKMapTypeStandard//标准地图模式
MKMapTypeSatellite//卫星地图模式
MKMapTypeHybrid//具有街道等信息的卫星地图模式(云图模式)
还要注意:1.拖出来的pickerView 要遵守 dataSource 和 delegate 代理,与 ViewController 连线。
2.添加 MapKitFrame到创建的项目中。
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()
{
NSArray *array;//存放地图类型名称的数组
__weak IBOutlet MKMapView *mapView;//声明关于 mapView的插座变量
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//视图加载后调用,实现初始化
array = [[NSArray alloc] initWithObjects:@"标准模式",@"卫星模式",@"云图模式", nil];//创建数组
}
#pragma mark - 获取块数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
#pragma mark - 获取行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return array.count;
}
#pragma mark - 获取内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [array objectAtIndex:row];
}
#pragma mark - 实现选择器的响应
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
int i = (int)[pickerView selectedRowInComponent:0];//获取选中行数的索引值
if (i == 0) {
mapView.mapType = MKMapTypeStandard;//标准地图模式
}else if (i == 1){
mapView.mapType = MKMapTypeSatellite;//卫星地图模式
}else{
mapView.mapType = MKMapTypeHybrid;//具有街道等信息的卫星地图模式(云图模式)
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end