iOS 使用高德地圖正確姿勢(shì)(三)

iOS 使用高德地圖正確姿勢(shì)(一)
iOS 使用高德地圖正確姿勢(shì)(二)

實(shí)現(xiàn)大頭針始終在地圖中心,拖動(dòng)地圖實(shí)時(shí)poi出中心地址


地圖.png

頭文件

#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
//協(xié)議
<MAMapViewDelegate,AMapSearchDelegate,AMapLocationManagerDelegate>
@property (nonatomic, strong) AMapLocationManager *locationManager;//定位管理者
@property (nonatomic, strong) MAMapView * mapView; //地圖
@property(nonatomic,strong)AMapSearchAPI *search;
@property (nonatomic, strong) CLLocation *currentLocation;//定位坐標(biāo)

一、初始化設(shè)置

    //高德key 初始化
    [AMapServices sharedServices].apiKey =GDkey;
    self.locationManager = [[AMapLocationManager alloc] init];
    self.locationManager.delegate = self;
    //初始化_search 用于逆編碼
    self.search =[[AMapSearchAPI alloc] init];
    self.search.delegate=self;
    // 帶逆地理信息的一次定位(返回坐標(biāo)和地址信息)
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
    //   定位超時(shí)時(shí)間,最低2s,此處設(shè)置為2s
    self.locationManager.locationTimeout =2;
    //   逆地理請(qǐng)求超時(shí)時(shí)間,最低2s,此處設(shè)置為2s
    self.locationManager.reGeocodeTimeout = 2;
    [self GetLococation];//定位返回結(jié)果

二、地圖界面

//加載地圖
- (void)setmap{
    TSMapView *MapView=[[TSMapView alloc]init];
    MapView.frame=CGRectMake(0, SafeAreaTopHeight, kScreenWidth, kScreenHeigth-SafeAreaBoHttomHeight-SafeAreaTopHeight-160);
    [self.view addSubview:MapView];
     NSString *str=@"提示:如果定位不準(zhǔn),請(qǐng)拖動(dòng)地圖,標(biāo)記出準(zhǔn)確的店鋪位置,業(yè)務(wù)員和騎手才能更快找到";
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:str];
    [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(10,16)];
  MapView.TipsLab.attributedText=string;    
    ///地圖需要v4.5.0及以上版本才必須要打開此選項(xiàng)(v4.5.0以下版本,需要手動(dòng)配置info.plist)
    [AMapServices sharedServices].enableHTTPS = YES;
    ///初始化地圖
    _mapView = [[MAMapView alloc] initWithFrame:MapView.bounds];
    _mapView.mapType = MKMapTypeStandard;
    ///把地圖添加至view
    [MapView addSubview:_mapView];
    //設(shè)置地圖縮放比例,即顯示區(qū)域
    [_mapView setZoomLevel:15.1 animated:YES];
    _mapView.delegate = self;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;//追蹤用戶的location更新
    _mapView.showsUserLocation = NO;    //定位小藍(lán)點(diǎn)
    //設(shè)置定位精度
    _mapView.desiredAccuracy = kCLLocationAccuracyBest;
    //設(shè)置定位距離
    _mapView.distanceFilter = 5.0f;
    //地圖加載后調(diào)用 保證在地圖上方
    [MapView bringSubviewToFront:MapView.DTZimag];
    [MapView bringSubviewToFront:MapView.TipsView];
}

三、進(jìn)入地圖的一次定位回調(diào),把定位坐標(biāo)設(shè)成地圖中心點(diǎn)

- (void)GetLococation{
    // 帶逆地理(返回坐標(biāo)和地址信息)。將下面代碼中的 YES 改成 NO ,則不會(huì)返回地址信息。
    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        if (error)
        {
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
            if (error.code == AMapLocationErrorLocateFailed)
            {
                return;
            }
        }
        /**location 當(dāng)前定位地理信息*/
        NSLog(@"location:%@", location);
        self.currentLocation=location;
       //把中心點(diǎn)設(shè)成自己的坐標(biāo)
        self.mapView.centerCoordinate = self.currentLocation.coordinate;
        //當(dāng)前位置逆地理信息
        if (regeocode)
        {
            NSLog(@"reGeocode:%@", regeocode);
        }
    }];
}

四、拖動(dòng)地圖完成后回調(diào)

#pragma mark - MAMapViewDelegate
//地圖區(qū)域改變完成后調(diào)用的接口
- (void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
    NSLog(@"%f",mapView.region.center.latitude); //拿到中心點(diǎn)的經(jīng)緯度
    NSLog(@"%f/n",mapView.region.center.longitude);
    //根據(jù)中心坐標(biāo)進(jìn)行周邊搜索 */
    [self searchAround:mapView.region.center.latitude getLong:mapView.region.center.longitude];
}

五、根據(jù)中心坐標(biāo)進(jìn)行周邊搜索

- (void)searchAround:(CLLocationDegrees )location getLong:(CLLocationDegrees )Longitude{
    //構(gòu)造AMapPOIAroundSearchRequest對(duì)象,設(shè)置周邊請(qǐng)求參數(shù)
    AMapPOIAroundSearchRequest *request = [[AMapPOIAroundSearchRequest alloc] init];
    request.location = [AMapGeoPoint locationWithLatitude:location longitude:Longitude];
    // types屬性表示限定搜索POI的類別,默認(rèn)為:餐飲服務(wù)|商務(wù)住宅|生活服務(wù)
    // POI的類型共分為20種大類別,分別為:
    // 汽車服務(wù)|汽車銷售|汽車維修|摩托車服務(wù)|餐飲服務(wù)|購(gòu)物服務(wù)|生活服務(wù)|體育休閑服務(wù)|
    // 醫(yī)療保健服務(wù)|住宿服務(wù)|風(fēng)景名勝|(zhì)商務(wù)住宅|政府機(jī)構(gòu)及社會(huì)團(tuán)體|科教文化服務(wù)|
    // 交通設(shè)施服務(wù)|金融保險(xiǎn)服務(wù)|公司企業(yè)|道路附屬設(shè)施|地名地址信息|公共設(shè)施
    request.types = @"餐飲服務(wù)|生活服務(wù)|商務(wù)住宅|公司企業(yè)|地名地址信息";
    request.sortrule = 0;
    request.requireExtension = YES;    
    //發(fā)起周邊搜索
    [self.search AMapPOIAroundSearch: request];
}

六、實(shí)現(xiàn)POI搜索對(duì)應(yīng)的回調(diào)

- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response{
    NSLog(@"周邊搜索回調(diào)");
    if(response.pois.count == 0)
    {
        return;
    }
//返回10組數(shù)據(jù),想要什么自己拿
    self.dataArray = [NSMutableArray arrayWithArray:response.pois];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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