自定義地圖添加覆蓋物 - 劃線(Obj-C)

添加地圖覆蓋物后還需要通過代理方法來設置覆蓋物樣式,否則是看不到效果的

簡單的UI搭建:

UI.png

演示代碼:

#import "ViewController.h"
#import <MapKit/MapKit.h>

@interface ViewController () <MKMapViewDelegate>
// 目標地址
@property (weak, nonatomic) IBOutlet UITextField *destination_TF;
// 地圖
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
// 位置管理者
@property (strong,nonatomic) CLLocationManager *manager;
@end

@implementation ViewController

// 開始導航按鈕點擊事件
- (IBAction)navigationBtnClick:(id)sender {
    
    // 創(chuàng)建導航對象請求,設置起始點
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc]init];
    request.source = [MKMapItem mapItemForCurrentLocation];

    CLGeocoder *geocoder = [[CLGeocoder alloc]init];
    [geocoder geocodeAddressString:self.destination_TF.text completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
       
        
        if (placemarks.count == 0 || error) {
            
            return ;
        }
        CLPlacemark *clPlacemark = placemarks.lastObject;
        MKPlacemark *mkPlacemark = [[MKPlacemark alloc]initWithPlacemark:clPlacemark];
        request.destination = [[MKMapItem alloc]initWithPlacemark:mkPlacemark];
        
        // 創(chuàng)建導航對象
        MKDirections *directions = [[MKDirections alloc]initWithRequest:request];
        
        // 計算坐標
        [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
            
            if (error) {
                return ;
            }
            
            // 遍歷路線
            for (MKRoute *route in response.routes) {
                
                // 添加覆蓋物(折線)
                [self.mapView addOverlay:route.polyline];
                
            }
            
        }];
        
    }];
}

/**
 *  當設置覆蓋物的樣式時調(diào)用
 *
 *  @param mapView 地圖視圖
 *  @param overlay 覆蓋物
 *
 *  @return 覆蓋物樣式
 */
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay{
    
    // 創(chuàng)建折線樣式(子類)
    /*
        實際這里傳遞的overlay就是polyline  -->  [self.mapView addOverlay:route.polyline];
     */
    MKPolylineRenderer *render = [[MKPolylineRenderer alloc]initWithOverlay:overlay];
    
    // 設置樣式
    render.lineWidth = 2;
    render.strokeColor = [UIColor purpleColor];
    
    return render;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 授權
    self.manager = [[CLLocationManager alloc]init];
    if ([self.manager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.manager requestWhenInUseAuthorization];
    }
    
    // 設置代理(設置覆蓋物的展示樣式)
    self.mapView.delegate = self;
    
}

效果:

res.png
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,323評論 25 708
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,688評論 19 139
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,631評論 4 61
  • 1,在iOS逆向工程中,用 class-dump 可以導出App頭文件;但是,從App Store 下載的App,...
    藍色天空524閱讀 3,925評論 0 1
  • 噼里啪啦的鞭炮聲中迎來金雞報曉,對于像我一樣漂泊于大城市和小鄉(xiāng)村之間的人來說這才是真正的新年,從學子的懵懂到游子的...
    釣魚實驗室閱讀 310評論 0 0

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