关于地图切换器的总结

本文介绍了一个简单的iOS应用实例,展示了如何使用MapKit框架在三种不同的地图模式之间进行切换:标准模式、卫星模式和云图模式。该应用还包含了用于选择地图类型的pickerView组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       地图导航应用几乎是每一步手机中必备的软件。在地图中可以进行很多操作,例如通过 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值