相關(guān)類的介紹:
MKAnnotation(大頭針協(xié)議)
大頭針數(shù)據(jù)類(自定義的大頭針需要遵守大頭針協(xié)議)
MKPointAnnotation
MKUserLocation
大頭針展示類
MKPinAnnotationView<pre>//大頭針的顏色
@property (NS_NONATOMIC_IOSONLY, strong, null_resettable) UIColor *pinTintColor NS_AVAILABLE(10_11, 9_0) UI_APPEARANCE_SELECTOR;
//設(shè)置添加時是否顯示降落動畫
@property (nonatomic) BOOL animatesDrop;</pre>MKAnnotationView<pre>
//設(shè)置圖片
@property (nonatomic, strong, nullable) UIImage *image;
//大頭針中心位置的偏移量
@property (nonatomic) CGPoint centerOffset;
//彈出試圖的偏移量
@property (nonatomic) CGPoint calloutOffset;
//是否彈出試圖(默認(rèn)是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(地標(biāo))
MKMapItem
<pre>
//導(dǎo)航
//1.可以將需要導(dǎo)航的位置讓系統(tǒng)的地圖App進行導(dǎo)航
//2.發(fā)送網(wǎng)絡(luò)請求到公司服務(wù)器獲取導(dǎo)航數(shù)據(jù)嫌术,然后自己手動繪制導(dǎo)航
//3.利用三方SDK實現(xiàn)導(dǎo)航
//初始位置
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 = @{
//設(shè)置導(dǎo)航模式參數(shù)
MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
//設(shè)置地圖類型
MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard),
//設(shè)置是否顯示交通
MKLaunchOptionsShowsTrafficKey:@(YES)
};
[MKMapItem openMapsWithItems:@[myLocation,toLocation] launchOptions:launchDic];</pre>MKMapCamera(地圖街景)
MKMapSnapshotter(地圖截圖)
<pre>
//截圖附加選項
MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
//設(shè)置截圖區(qū)域
options.region = _mapView.region;
options.size = _mapView.frame.size;
//設(shè)置截圖后的圖片比例(默認(rèn)是屏幕比例)
options.scale = [[UIScreen mainScreen] scale];
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot * _Nullable snapshot, NSError * _Nullable error) {
NSLog(@"截圖結(jié)束");
if (error) {
NSLog(@"截圖錯誤 : %@",[error description]);
}
else {
//圖片保存在相冊
UIImageWriteToSavedPhotosAlbum(snapshot.image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}
}];</pre>MKDirections(獲取實際路線信息)
<pre>
/* MKDirectionsResponse
source:開始位置
destination:結(jié)束位置
routes:路線信息
MKRoute
name:路的名稱
advisoryNotices:注意警告信息
distance:路線長度(實際物理距離,單位為m)
polyline:路線對應(yīng)的在地圖上的幾何路線(由很多點組成噪生,可繪制在地圖上)
steps:多個行走步驟組成的數(shù)組
MKRouteStep
instructions:步驟說明
transportType:通過方式(駕車覆获,步行)
polyline:路線對應(yīng)的在地圖上的幾何線路(由很多點組成材鹦,可繪制在地圖上)
注意:
MKRoute是一整條長路汤踏;MKRouteStep是這條長路中的每一截葛圃;
*/
//創(chuàng)建請求
//設(shè)置開始地標(biāo)
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = [MKMapItem mapItemForCurrentLocation];
//設(shè)置結(jié)束地標(biāo)
MKPlacemark *endPlacemark = [[MKPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(40, 117) addressDictionary:nil];
MKMapItem *endMapItem = [[MKMapItem alloc] initWithPlacemark:endPlacemark];
endMapItem.name = @"目的地";
request.destination = endMapItem;
//根據(jù)請求诬烹,獲取實際路線信息
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;
}
//創(chuàng)建一個折線對象
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];
//設(shè)置線寬
render.lineWidth = 3;
//設(shè)置顏色
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];
//發(fā)請求到蘋果服務(wù)器請求數(shù)據(jù)
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = sourceItem;
request.destination = destinationItem;
//創(chuàng)建方法對象
MKDirections *direction = [[MKDirections alloc] initWithRequest:request];
[direction calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
if (response.routes.count == 0 || error) {
NSLog(@"沒有找到對應(yīng)的路線");
return ;
}
//從返回的response中獲取一組MKRoute路線對象
for (MKRoute *route in response.routes) {
//NSLog(@"%@",route.name);
//從路線對象中獲取折線對象
MKPolyline *polyline = route.polyline;
//將折線對象通過渲染方式添加到地圖上型檀,注意在渲染的代理方法中為折線設(shè)置相關(guān)屬性
[_mapView addOverlay:polyline];
}
}];
}];
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
//1.創(chuàng)建一個折線渲染物對象(MKOverlayRenderer的子類)
MKPolylineRenderer *polyline = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
//2.設(shè)置線條顏色冗尤,必須設(shè)置。
// polyline.fillColor = [UIColor redColor];
polyline.strokeColor = [UIColor blueColor];
//3.設(shè)置線條寬度
polyline.lineWidth = 10;
return polyline;
}</pre>