您的位置:首页 > 移动开发 > IOS开发

iOS笔记25

2015-12-01 09:16 281 查看
1

//设置地图显示类型

地图的样式可以手动设置, 在iOS9.0之前有3种, iOS9.0之后增加了2种

设置方式

self.mapView.mapType = MKMapTypeStandard;

枚举类型 对应含义

MKMapTypeStandard 标准地图

MKMapTypeSatellite 卫星地图

MKMapTypeHybrid 混合模式(标准+卫星)

MKMapTypeSatelliteFlyover 3D立体卫星(iOS9.0)

MKMapTypeHybridFlyover 3D立体混合(iOS9.0)

2

//地图的旋转, 缩放, 移动等等操作行为都可以开启或者关闭

设置方式

self.customMapView.zoomEnabled = YES; // 是否缩放

self.customMapView.scrollEnabled = YES; // 是否滚动

self.customMapView.rotateEnabled = YES; // 是否旋转

self.customMapView.pitchEnabled = NO; // 是否显示3DVIEW

3

//地图上的指南针, 比例尺, 建筑物, POI点都可以控制是否显示

设置方式

self.customMapView.showsCompass = YES; // 是否显示指南针

self.customMapView.showsScale = YES; // 是否显示比例尺

self.customMapView.showsTraffic = YES; // 是否显示交通

self.customMapView.showsBuildings = YES; // 是否显示建筑物

4

//显示用户位置

可以设置显示用户当前所在位置, 以一个蓝点的形式呈现在地图上

设置方式

//方案1:

self.customMapView.showsUserLocation = YES;

效果:

会在地图上显示一个蓝点, 标识用户所在位置; 但地图不会缩放, 而且当用户位置移动时, 地图不会跟随用户位置移动而移动

//方案2:

self.customMapView.userTrackingMode = MKUserTrackingModeFollowWithHeading;

效果:

会在地图上显示一个蓝点, 标识用户所在位置; 而且地图缩放到合适比例,显示用户位置, 当用户位置移动时, 地图会跟随用户位置移动而移动; 但是有时候失效;

5

//查看当前用户位置信息

//设置地图代理

self.mapView.delegate = self;

//实现代理方法

-(void)mapView:(MKMapView )mapView didUpdateUserLocation:(MKUserLocation )userLocation

{

NSLog(@”%@”, userLocation);

}

6

//调整地图显示中心

//确定地图中心经纬度坐标

CLLocationCoordinate2D center = CLLocationCoordinate2DMake(21.123, 121.345);

//设置地图中心为给定的经纬度坐标

[mapView setCenterCoordinate:center animated:YES];

7

// 调整地图显示区域

//—————————————————————–

//1.获取合适的区域跨度

实现当地图区域发生改变时调用的代理代理方法, 并调整地图区域到合适比例, 并在对应的方法中, 获取对应的跨度信息

代码如下:

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated

{

NSLog(@”%f—%f”, mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta);

}

//—————————————————————–

//2.创建一个区域(包含区域中心, 和区域跨度)

CLLocationCoordinate2D center = CLLocationCoordinate2DMake(21.123, 121.345);

MKCoordinateSpan span = MKCoordinateSpanMake(0.1, 0.1);

MKCoordinateRegion region = MKCoordinateRegionMake(center, span);

//—————————————————————–

//3.设置地图显示区域

[self.mapView setRegion:region animated:YES];

//—————————————————————–

//4.概念解释

MKCoordinateSpan 跨度解释:
latitudeDelta:纬度跨度,因为南北纬各90.0度,所以此值的范围是(0.0---180.0);此值表示,整个地图视图宽度,显示多大跨度;

longitudeDelta:经度跨度,因为东西经各180.0度,所以此值范围是(0.0---360.0):此值表示,整个地图视图高度,显示多大跨度;

注意:地图视图显示,不会更改地图的比例,会以地图视图高度或宽度较小的那个为基准,按比例调整


8

//MKUserLocation 大头针数据模型详解

MKUserLocation : 被称作“大头针(数据)模型”;

其实喊什么都行,本质就是一个数据模型,只不过此模型遵循了大头针要遵循的协议(MKAnnotation)

重要属性:

location : 用户当前所在位置信息(CLLocation对象)

title : 大头针标注要显示的标题(NSString对象)

subtitle : 大头针标注要显示的子标题(NSString对象)

9

//地图高级-大头针基本使用

1. 理论支撑(必须掌握)
按照MVC的原则
*  在地图上操作大头针,实际上是控制大头针数据模型
1. 添加大头针就是添加大头针数据模型
2. 删除大头针就是删除大头针数据模型


//—————————————————————–

2. 在地图上添加大头针视图

//1> 自定义大头针数据模型
1) 创建继承自NSObject的数据模型XMGAnnotation, 遵循大头针数据模型必须遵循的协议(MKAnnotation)
2) 注意将协议@property 中的readonly 去掉;

//2> 创建大头针数据模型, 并初始化参数

XMGAnnotation *annotation = [[XMGAnnotation alloc] init];
annotation.coordinate = coordinate;
annotation.title = @"小码哥";
annotation.subtitle = @"小码哥分部";

//3> 调用地图的添加大头针数据模型方法
[self.customMapView addAnnotation:annotation];


//—————————————————————–

3. 移除大头针(所有大头针)

NSArray *annotations = self.customMapView.annotations;

[self.customMapView removeAnnotations:annotations];

10

//场景模拟

场景描述:

鼠标点击在地图哪个位置, 就在对应的位置添加一个大头针, 并在标注弹框中显示对应的城市和街道;
实现步骤

1. 获取触摸点在地图上对应的坐标
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.customMapView];

2. 将坐标转换成为经纬度
CLLocationCoordinate2D center = [self.customMapView convertPoint:touchPoint toCoordinateFromView:self.customMapView];

3. 根据经纬度创建大头针数据模型, 并添加在地图上
XMGAnnotation *annotation = [[XMGAnnotation alloc] init];
annotation.coordinate = coordinate;
annotation.title = @"小码哥";
annotation.subtitle = @"小码哥分部";
[self.customMapView addAnnotation:annotation];

4. 利用反地理编码, 获取该点对应的城市和街道名称, 然后修改大头针数据模型
注意: 设置弹框数据时, 对应的大头针数据模型应有对应的占位数据(这样对应的UI才会生成,后面才能重新修改数据)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios