相关类的介绍:
MKAnnotation(大头针协议)
大头针数据类(自定义的大头针需要遵守大头针协议)
MKPointAnnotation
MKUserLocation
大头针展示类
MKPinAnnotationView<pre>//大头针的颜色
@property (NS_NONATOMIC_IOSONLY, strong, null_resettable) UIColor *pinTintColor NS_AVAILABLE(10_11, 9_0) UI_APPEARANCE_SELECTOR;
//设置添加时是否显示降落动画
@property (nonatomic) BOOL animatesDrop;</pre>MKAnnotationView<pre>
//设置图片
@property (nonatomic, strong, nullable) UIImage *image;
//大头针中心位置的偏移量
@property (nonatomic) CGPoint centerOffset;
//弹出试图的偏移量
@property (nonatomic) CGPoint calloutOffset;
//是否弹出试图(默认是NO)
@property (nonatomic) BOOL canShowCallout;
//弹出视图的左视图
@property (strong, nonatomic, nullable) UIView *leftCalloutAccessoryView;
//弹出视图的右视图
@property (strong, nonatomic, nullable) UIView *rightCalloutAccessoryView;
//是否能拖拽
@property (nonatomic, getter=isDraggable) BOOL draggable NS_AVAILABLE(10_9, 4_0) __TVOS_PROHIBITED;</pre>MKPlacemark(地标)
MKMapItem
<pre>
//导航
//1.可以将需要导航的位置让系统的地图App进行导航
//2.发送网络请求到公司服务器获取导航数据,然后自己手动绘制导航
//3.利用三方SDK实现导航
//初始位置
MKMapItem *myLocation = [MKMapItem mapItemForCurrentLocation];
//目的位置
CLLocationCoordinate2D toLocationCoor = CLLocationCoordinate2DMake(40, 117);
MKPlacemark *placemack = [[MKPlacemark alloc] initWithCoordinate:toLocationCoor addressDictionary:nil];
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:placemack];
toLocation.name = @"目的地";
NSDictionary *launchDic = @{
//设置导航模式参数
MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
//设置地图类型
MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard),
//设置是否显示交通
MKLaunchOptionsShowsTrafficKey:@(YES)
};
[MKMapItem openMapsWithItems:@[myLocation,toLocation] launchOptions:launchDic];</pre>MKMapCamera(地图街景)
MKMapSnapshotter(地图截图)
<pre>
//截图附加选项
MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
//设置截图区域
options.region = _mapView.region;
options.size = _mapView.frame.size;
//设置截图后的图片比例(默认是屏幕比例)
options.scale = [[UIScreen mainScreen] scale];
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot * _Nullable snapshot, NSError * _Nullable error) {
NSLog(@"截图结束");
if (error) {
NSLog(@"截图错误 : %@",[error description]);
}
else {
//图片保存在相册
UIImageWriteToSavedPhotosAlbum(snapshot.image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}
}];</pre>MKDirections(获取实际路线信息)
<pre>
/* MKDirectionsResponse
source:开始位置
destination:结束位置
routes:路线信息
MKRoute
name:路的名称
advisoryNotices:注意警告信息
distance:路线长度(实际物理距离,单位为m)
polyline:路线对应的在地图上的几何路线(由很多点组成,可绘制在地图上)
steps:多个行走步骤组成的数组
MKRouteStep
instructions:步骤说明
transportType:通过方式(驾车,步行)
polyline:路线对应的在地图上的几何线路(由很多点组成,可绘制在地图上)
注意:
MKRoute是一整条长路;MKRouteStep是这条长路中的每一截;
*/
//创建请求
//设置开始地标
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = [MKMapItem mapItemForCurrentLocation];
//设置结束地标
MKPlacemark *endPlacemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(40, 117) addressDictionary:nil];
MKMapItem *endMapItem = [[MKMapItem alloc] initWithPlacemark:endPlacemark];
endMapItem.name = @"目的地";
request.destination = endMapItem;
//根据请求,获取实际路线信息
MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
[directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
[response.routes enumerateObjectsUsingBlock:^(MKRoute * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@-- %f",obj.name,obj.distance);//实际路线距离
[obj.steps enumerateObjectsUsingBlock:^(MKRouteStep * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@",obj.instructions);
}];
}];
}];
</pre>覆盖物 MKPolyline(折线),MKCircle(圆形),MKPolygon(边形)
<pre>#if 1
CLLocationCoordinate2D coor;
coor = malloc(sizeof(CLLocationCoordinate2D)5);
for (int i = 0; i < 5; i++) {
// CLLocationCoordinate2D po = CLLocationCoordinate2DMake(33.23+i0.01, 113.112);
CLLocationCoordinate2D po = CLLocationCoordinate2DMake(_bjCoor.latitude+i0.01, _bjCoor.longitude);
coor[i] = po;
}
//创建一个折线对象
MKPolyline *line = [MKPolyline polylineWithCoordinates:coor count:5];
[_mapView addOverlay:line];
endif
if 0
MKCircle *circle = [MKCircle circleWithCenterCoordinate:_bjCoor radius:500];
[_mapView addOverlay:circle];
endif
if 0
CLLocationCoordinate2D *coor;
coor = malloc(sizeof(CLLocationCoordinate2D)*6);
for (int i = 0; i < 5; i++) {
CLLocationCoordinate2D po = CLLocationCoordinate2DMake(_bjCoor.latitude+i*0.01, _bjCoor.longitude+((i/2==0)?0.01:-0.01));
coor[i] = po;
}
// coor[5] = CLLocationCoordinate2DMake(33.23, 113.112);
coor[5] = _bjCoor;
MKPolygon *gon = [MKPolygon polygonWithCoordinates:coor count:6];
[_mapView addOverlay:gon];
endif
pragma mark MKMapDelegate
//覆盖物绘制的代理
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
if 1
//折线覆盖物提供类
MKPolylineRenderer *render = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
//设置线宽
render.lineWidth = 3;
//设置颜色
render.strokeColor = [UIColor redColor];
return render;
endif
if 0
MKCircleRenderer *render = [[MKCircleRenderer alloc] initWithCircle:overlay];
// MKCircleRenderer *render = [[MKCircleRenderer alloc] initWithOverlay:overlay];
render.lineWidth = 3;
//填充颜色
render.fillColor = [UIColor greenColor];
render.strokeColor = [UIColor redColor];
return render;
endif
if 0
MKPolygonRenderer *render = [[MKPolygonRenderer alloc] initWithPolygon:overlay];
render.lineWidth = 3;
render.strokeColor = [UIColor redColor];
return render;
endif
}</pre>MKDirections+MKPolyline
<pre> CLGeocoder *geocoder = [CLGeocoder new];
[geocoder geocodeAddressString:@"天津" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count == 0 || error) {
return ;
}
CLPlacemark *pm = placemarks.lastObject;
MKPlacemark *mkPm = [[MKPlacemark alloc] initWithPlacemark:pm];
//起点
MKMapItem *sourceItem = [MKMapItem mapItemForCurrentLocation];
//终点
MKMapItem *destinationItem = [[MKMapItem alloc] initWithPlacemark:mkPm];
//发请求到苹果服务器请求数据
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = sourceItem;
request.destination = destinationItem;
//创建方法对象
MKDirections *direction = [[MKDirections alloc] initWithRequest:request];
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
if (response.routes.count == 0 || error) {
NSLog(@"没有找到对应的路线");
return ;
}
//从返回的response中获取一组MKRoute路线对象
for (MKRoute *route in response.routes) {
//NSLog(@"%@",route.name);
//从路线对象中获取折线对象
MKPolyline *polyline = route.polyline;
//将折线对象通过渲染方式添加到地图上,注意在渲染的代理方法中为折线设置相关属性
[_mapView addOverlay:polyline];
}
}];
}];
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
//1.创建一个折线渲染物对象(MKOverlayRenderer的子类)
MKPolylineRenderer *polyline = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
//2.设置线条颜色,必须设置。
// polyline.fillColor = [UIColor redColor];
polyline.strokeColor = [UIColor blueColor];
//3.设置线条宽度
polyline.lineWidth = 10;
return polyline;
}</pre>
网友评论