iOS高德地圖使用-搜索,路徑規(guī)劃

最近趕項目,好久沒更新。抽出時間把高德地圖的搜索和路徑規(guī)劃整理一下
項目中想加入地圖功能,使用高德地圖第三方,想要實現(xiàn)確定一個位置,搜索路線并且顯示的方法。耗了一番功夫,總算實現(xiàn)了。

效果

WeChat_1462507820.jpeg

一、配置工作

1.申請key

訪問 http://lbs.amap.com/dev/key/ 在高度地圖第三方開發(fā)平臺申請一個key,注冊賬戶,新建應(yīng)用,這個沒什么門檻。
得到這個key

屏幕快照 2016-05-06 上午10.34.15.png

提示一下,這個key對應(yīng)的bundle ID 要和工程里面的bundle ID 相同,不然每次打開地圖都會報一個Invalid_user_scode的提示。

2.導(dǎo)入第三方

方便起見 pod導(dǎo)入

pod 'AMap3DMap' #3D地圖SDK
#pod 'AMap2DMap' #2D地圖SDK(2D地圖和3D地圖不能同時使用,2選1)
pod 'AMapSearch' #搜索服務(wù)SDK
3.打開工程的后臺定位功能 更改info.plist

增加兩條,一條請求位置時提示,一條https


屏幕快照 2016-05-06 上午11.02.06.png
屏幕快照 2016-05-06 上午10.48.19.png

二、地圖基本顯示

把地圖添加到view即可顯示

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //配置用戶Key
    [MAMapServices sharedServices].apiKey = @"76bb9bc3718375ad03acba7c333694c4";
    
    //把地圖放在底層
    [self.view insertSubview:self.mapView atIndex:0];
    
}
//地圖懶加載
- (MAMapView *)mapView
{
    if (!_mapView) {
        _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
        _mapView.delegate = self;
        _mapView.showsUserLocation = YES;    //YES 為打開定位,NO為關(guān)閉定位
        
        [_mapView setUserTrackingMode: MAUserTrackingModeFollow animated:NO]; //地圖跟著位置移動
        
        //自定義定位經(jīng)度圈樣式
        _mapView.customizeUserLocationAccuracyCircleRepresentation = NO;
        //地圖跟蹤模式
        _mapView.userTrackingMode = MAUserTrackingModeFollow;
        
        //后臺定位
        _mapView.pausesLocationUpdatesAutomatically = NO;
        
        _mapView.allowsBackgroundLocationUpdates = YES;//iOS9以上系統(tǒng)必須配置
    
    }
    return _mapView;
}

三、地圖搜索功能

遵守協(xié)議 AMapSearchDelegate

高德提供了多種搜索方式,POI搜索(關(guān)鍵字查詢、周邊搜索、多邊形查詢),檢索現(xiàn)實中真實存在的地物。

提示搜索,就是在還沒有輸入完全時,根據(jù)已有字符進(jìn)行的搜索

//搜索框激活時,使用提示搜索
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    //發(fā)起輸入提示搜索
    AMapInputTipsSearchRequest *tipsRequest = [[AMapInputTipsSearchRequest alloc] init];
   //關(guān)鍵字
    tipsRequest.keywords = _searchController.searchBar.text;
   //城市
    tipsRequest.city = _currentCity;

    //執(zhí)行搜索
    [_search AMapInputTipsSearch: tipsRequest];

}

//實現(xiàn)輸入提示的回調(diào)函數(shù)
-(void)onInputTipsSearchDone:(AMapInputTipsSearchRequest*)request response:(AMapInputTipsSearchResponse *)response
{
    if(response.tips.count == 0)
    {
        return;
    }
    //通過AMapInputTipsSearchResponse對象處理搜索結(jié)果
    //先清空數(shù)組
    [self.searchList removeAllObjects];
    for (AMapTip *p in response.tips) {    
        //把搜索結(jié)果存在數(shù)組
        [self.searchList addObject:p];
    }
    _isSelected = NO;
    //刷新表視圖
    [self.tableView reloadData];
}

點(diǎn)擊進(jìn)行poi搜索


//周邊搜索
- (IBAction)searchAction:(id)sender {
    //初始化檢索對象
    _search = [[AMapSearchAPI alloc] init];
    _search.delegate = self;
    
    //構(gòu)造AMapPOIAroundSearchRequest對象,設(shè)置周邊請求參數(shù)
    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
  
    //當(dāng)前位置
    request.location = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
    
    //關(guān)鍵字
    request.keywords = _searchController.searchBar.text;
    NSLog(@"%@",_searchController.searchBar.text);
    // types屬性表示限定搜索POI的類別,默認(rèn)為:餐飲服務(wù)|商務(wù)住宅|生活服務(wù)
    // POI的類型共分為20種大類別,分別為:
    // 汽車服務(wù)|汽車銷售|汽車維修|摩托車服務(wù)|餐飲服務(wù)|購物服務(wù)|生活服務(wù)|體育休閑服務(wù)|
    // 醫(yī)療保健服務(wù)|住宿服務(wù)|風(fēng)景名勝|(zhì)商務(wù)住宅|政府機(jī)構(gòu)及社會團(tuán)體|科教文化服務(wù)|
    // 交通設(shè)施服務(wù)|金融保險服務(wù)|公司企業(yè)|道路附屬設(shè)施|地名地址信息|公共設(shè)施
    //    request.types = @"餐飲服務(wù)|生活服務(wù)";
    request.radius =  5000;//<! 查詢半徑,范圍:0-50000,單位:米 [default = 3000]
    request.sortrule = 0;
    request.requireExtension = YES;
    
    //發(fā)起周邊搜索
    [_search AMapPOIAroundSearch:request];
}

//實現(xiàn)POI搜索對應(yīng)的回調(diào)函數(shù)
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    if(response.pois.count == 0)
    {
        return;
    }
    
    //通過 AMapPOISearchResponse 對象處理搜索結(jié)果
  
    [self.dataList removeAllObjects];
    for (AMapPOI *p in response.pois) {
        NSLog(@"%@",[NSString stringWithFormat:@"%@\nPOI: %@,%@", p.description,p.name,p.address]);
        
        //搜索結(jié)果存在數(shù)組
        [self.dataList addObject:p];
    }
    
    _isSelected = YES;
    [self.tableView reloadData];
  
}

四、路徑規(guī)劃

規(guī)劃路徑查詢(駕車路線搜索、公交換成方案查詢、步行路徑檢索),提前知道出行路線

//規(guī)劃線路查詢
- (IBAction)findWayAction:(id)sender {
    //構(gòu)造AMapDrivingRouteSearchRequest對象,設(shè)置駕車路徑規(guī)劃請求參數(shù)
    AMapWalkingRouteSearchRequest *request = [[AMapWalkingRouteSearchRequest alloc] init];
    //設(shè)置起點(diǎn),我選擇了當(dāng)前位置,mapView有這個屬性
    request.origin = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
    //設(shè)置終點(diǎn),可以選擇手點(diǎn)
    request.destination = [AMapGeoPoint locationWithLatitude:_destinationPoint.coordinate.latitude longitude:_destinationPoint.coordinate.longitude];
        
//    request.strategy = 2;//距離優(yōu)先
//    request.requireExtension = YES;
    
    //發(fā)起路徑搜索,發(fā)起后會執(zhí)行代理方法
    //這里使用的是步行路徑
    [_search AMapWalkingRouteSearch: request]; 
}

//長按手勢響應(yīng)方法,選擇路徑規(guī)劃的終點(diǎn),手勢自己加
- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
       //在地圖上長按的位置
        CGPoint p = [gesture locationInView:_mapView];
        NSLog(@"press on (%f, %f)", p.x, p.y);
    }
     //轉(zhuǎn)換成經(jīng)緯度
    CLLocationCoordinate2D coordinate = [_mapView convertPoint:[gesture locationInView:_mapView] toCoordinateFromView:_mapView];
     
   //賦值給目標(biāo)點(diǎn)
    _destinationPoint = [[MAPointAnnotation alloc] init];
    _destinationPoint.coordinate = coordinate;
}

執(zhí)行路徑搜索后會執(zhí)行代理方法


//實現(xiàn)路徑搜索的回調(diào)函數(shù)
- (void)onRouteSearchDone:(AMapRouteSearchBaseRequest *)request response:(AMapRouteSearchResponse *)response
{
    if(response.route == nil)
    {
        return;
    }
    
    //通過AMapNavigationSearchResponse對象處理搜索結(jié)果
    NSString *route = [NSString stringWithFormat:@"Navi: %@", response.route];
    
    AMapPath *path = response.route.paths[0]; //選擇一條路徑
    AMapStep *step = path.steps[0]; //這個路徑上的導(dǎo)航路段數(shù)組
    NSLog(@"%@",step.polyline);   //此路段坐標(biāo)點(diǎn)字符串
      
    if (response.count > 0)
    {
        //移除地圖原本的遮蓋
        [_mapView removeOverlays:_pathPolylines];
        _pathPolylines = nil;
        
        // 只顯?示第?條 規(guī)劃的路徑
        _pathPolylines = [self polylinesForPath:response.route.paths[0]];
        NSLog(@"%@",response.route.paths[0]);
        //添加新的遮蓋,然后會觸發(fā)代理方法進(jìn)行繪制
        [_mapView addOverlays:_pathPolylines];
    }
}

每次添加路線,區(qū)域,或者大頭針等都會觸發(fā)下面的代理方法

//繪制遮蓋時執(zhí)行的代理方法
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
    /* 自定義定位精度對應(yīng)的MACircleView. */
    
    //畫路線
    if ([overlay isKindOfClass:[MAPolyline class]])
    {
       //初始化一個路線類型的view
        MAPolylineRenderer *polygonView = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
        //設(shè)置線寬顏色等
        polygonView.lineWidth = 8.f;
        polygonView.strokeColor = [UIColor colorWithRed:0.015 green:0.658 blue:0.986 alpha:1.000];
        polygonView.fillColor = [UIColor colorWithRed:0.940 green:0.771 blue:0.143 alpha:0.800];
        polygonView.lineJoinType = kMALineJoinRound;//連接類型
        //返回view,就進(jìn)行了添加
        return polygonView;
    }
    return nil;
    
}

最后,這是demo,最好真機(jī)測試,模擬器的定位不好用
https://github.com/DaLiWangCC/MyOpen

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

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

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