申明:此为本人学习笔记,若有纰漏错误之处的可留言共同探讨
/*
地图功能
思路
一个MKMapView(地图页面) 一个UISegmentedControl(分段控件) 一个MKCoordinateRegion(区域)
地图页面和分段控件在self.view上,区域在地图页面上,区域有中心点和精确度
操作步骤
1.加入库MapKit、CoreGraphics 导入头文件<MapKit/MapKit.h>
2.创建一个全局变量MKMapView
3.初始化mapView,加入视图
4.创建分段控件UISegmentedControl(有时称为按钮栏),设置属性和方法
5.设置地图中心点、可视范围、加入区域内
6.把设置好的区域加入到mapView内
*/
具体代码部分:
#import "ViewController.h"
#import <MapKit/MapKit.h>
@interface ViewController ()
{
MKMapView *mapView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化mapView
mapView = [[MKMapView alloc]initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height-60)];
[self.view addSubview:mapView];
// 分段控件
UISegmentedControl *type = [[UISegmentedControl alloc]initWithItems:@[@"普通地图",@"卫星地图",@"混合地图"]];
type.frame =CGRectMake(0, 20, self.view.frame.size.width, 40);
[self.view addSubview:type];
[type addTarget:self action:@selector(ChangeType:) forControlEvents:UIControlEventValueChanged];
/* 设置地图的初始位置 参数
1 纬度
2 经度
*/
// 中心点的坐标
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(22.545734, 113.987004);
// 设置可视范围 数字越小显示的范围越小(内容越详细)
MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.01);
// 数据显示在地图上
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[mapView setRegion:region animated:YES];
}
#pragma mark - 分段控件点击事件
-(void)ChangeType:(UISegmentedControl*)type
{
switch (type.selectedSegmentIndex) {
case 0:
mapView.mapType = MKMapTypeStandard; // 普通
break;
case 1:
mapView.mapType = MKMapTypeSatellite; // 卫星
break;
case 2:
mapView.mapType = MKMapTypeHybrid; // 混合
break;
default:
break;
}
}
附上完整Demo:http://download.csdn.net/detail/csdn_hhg/9217933