iOS 地圖開發(fā)(MapKit)(二)

相關(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;
//設置添加時是否顯示降落動畫
@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.可以將需要導航的位置讓系統(tǒng)的地圖App進行導航
//2.發(fā)送網(wǎng)絡請求到公司服務器獲取導航數(shù)據(jù),然后自己手動繪制導航
//3.利用三方SDK實現(xiàn)導航
//初始位置
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ù)
                                MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
                                //設置地圖類型
                                MKLaunchOptionsMapTypeKey:@(MKMapTypeStandard),
                                //設置是否顯示交通
                                MKLaunchOptionsShowsTrafficKey:@(YES)
                                };
    [MKMapItem openMapsWithItems:@[myLocation,toLocation] launchOptions:launchDic];</pre>MKMapCamera(地圖街景)

MKMapSnapshotter(地圖截圖)
<pre>
//截圖附加選項
MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
//設置截圖區(qū)域
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(@"截圖結(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:路線對應的在地圖上的幾何路線(由很多點組成,可繪制在地圖上)
steps:多個行走步驟組成的數(shù)組
MKRouteStep
instructions:步驟說明
transportType:通過方式(駕車,步行)
polyline:路線對應的在地圖上的幾何線路(由很多點組成,可繪制在地圖上)
注意:
MKRoute是一整條長路;MKRouteStep是這條長路中的每一截;
*/
//創(chuàng)建請求
//設置開始地標
MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
request.source = [MKMapItem mapItemForCurrentLocation];

    //設置結(jié)束地標
    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+i
0.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];
//設置線寬
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];

        //發(fā)請求到蘋果服務器請求數(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(@"沒有找到對應的路線");
                return ;
            }
            //從返回的response中獲取一組MKRoute路線對象
            for (MKRoute *route in response.routes) {
                //NSLog(@"%@",route.name);
                //從路線對象中獲取折線對象
                MKPolyline *polyline = route.polyline;
                //將折線對象通過渲染方式添加到地圖上,注意在渲染的代理方法中為折線設置相關(guān)屬性
                [_mapView addOverlay:polyline];
            }
        }]; 
    }];
  • (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
    {
    //1.創(chuàng)建一個折線渲染物對象(MKOverlayRenderer的子類)
    MKPolylineRenderer *polyline = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
    //2.設置線條顏色,必須設置。
    // polyline.fillColor = [UIColor redColor];
    polyline.strokeColor = [UIColor blueColor];
    //3.設置線條寬度
    polyline.lineWidth = 10;
    return polyline;
    }</pre>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容