百度地圖使用-大頭針的自定義

最近老看到iOS開(kāi)發(fā)群里有人問(wèn)有關(guān)百度地圖的問(wèn)題,正好研究過(guò)百度地圖,今天我就把自己集成百度地圖相關(guān)功能和遇到問(wèn)題和大家分享。

效果圖.gif

<h5>百度地圖的導(dǎo)入和初始化:</h5>
1.1 集成百度地圖

  使用cocoPod集成百度地圖,在百度地圖API頁(yè)面配置相關(guān)參數(shù)!

1.1.2 初始化百度地圖可能遇到的問(wèn)題


錯(cuò)誤1.png

這個(gè)問(wèn)題是你的地圖控制器和百度地圖里面的文件重名了,所以你只需要把重名的文件改名就行

1.1.3 遇到地圖加載的是空白地圖或者是網(wǎng)格地圖
解決辦法:使兩者的值為同一個(gè)

項(xiàng)目修改.png
圖3.png

1.2百度地圖的定位問(wèn)題(沒(méi)作用)
解決方法:(在iOS10中,如果你的App想要訪問(wèn)用戶的相機(jī)、相冊(cè)、麥克風(fēng)、通訊錄,位置等等權(quán)限,都需要進(jìn)行相關(guān)的配置)

圖4.png

<h5>自定義大頭針</h5>
1.1 我的位置(有兩種方式)
1.1.2 動(dòng)態(tài)設(shè)置我的位置樣式

 /***動(dòng)態(tài)定制我的位置樣式        */
        
        BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc] init];
        
        displayParam.locationViewOffsetX=0;//定位偏移量(經(jīng)度)
        
        displayParam.locationViewOffsetY=0;//定位偏移量(緯度)
        
        displayParam.isAccuracyCircleShow=NO;//經(jīng)度圈是否顯示
        
        //這里替換自己的圖標(biāo)路徑,必須把圖片放到百度地圖SDK的Resources/mapapi.bundle/images 下面
        
        //還有一種方法就是獲取到_locationView之后直接設(shè)置圖片
        
        displayParam.locationViewImgName=@"hzb_dtdw_dw";
        
        [_mapView updateLocationViewWithParam:displayParam];

1.1.3 自定義大頭針(我的位置)

    /*******************************設(shè)置我的位置************************************************/
    
    BMKPointAnnotation* userAnnotation = [[BMKPointAnnotation alloc]init];
    
    userAnnotation.coordinate = userLocation.location.coordinate;
    
    [_mapView addAnnotation:userAnnotation];

1.2設(shè)置多種不同樣式的大頭針
首先定義大頭針樣式:它繼承于BMKAnnotation(在這里的模枚舉主要是用來(lái)判斷大頭針樣式,如果你的model里有判斷的你可以不寫(xiě)這個(gè))

#import <Foundation/Foundation.h>
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相關(guān)所有的頭文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地圖功能所有的頭文件
#import "MyModel.h"
/**
 *  大頭針枚舉
 */
typedef NS_ENUM(NSInteger,PinType) {
    /**
     *  超市
     */
    SUPER_MARKET = 0,
    /**
     *  火場(chǎng)
     */
    CREMATORY,
    /**
     *  景點(diǎn)
     */
    INTEREST,
};

@interface MyAnnotation : NSObject<BMKAnnotation>

@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
//主要用來(lái)識(shí)別圖
@property (nonatomic,retain) NSNumber *type;//類型

@property (nonatomic, strong) MyModel *model;

其次定義一個(gè)model 這里面主要是你從后臺(tái)請(qǐng)求的數(shù)據(jù),(如果后臺(tái)返回了怎樣區(qū)別不同大頭針,你可以在這里寫(xiě)上,我這里Isvalid是區(qū)別判斷是否有車位和remainder是否斷線)

#import <Foundation/Foundation.h>

@interface MyModel : NSObject

//經(jīng)緯度
@property (nonatomic,copy) NSString * longitude;
@property (nonatomic,copy) NSString * latitude;
///詳細(xì)地址
@property (nonatomic,copy) NSString * address;
//停車場(chǎng)名稱
@property (nonatomic,copy) NSString *name;
//停車場(chǎng)id
@property (nonatomic,copy) NSString * ID;
//收費(fèi)規(guī)則
@property (nonatomic,copy) NSString * typenamed;
//剩余車位
@property (nonatomic,copy) NSString * remainder;
//停車場(chǎng)圖片
@property (nonatomic,copy) NSString * parkimg;
//停車場(chǎng)規(guī)模
@property (nonatomic,copy) NSString *scale;

@property (nonatomic,copy)NSString *isvalid;

@property (nonatomic,copy) NSString *subtitle;
@end

我們看下網(wǎng)絡(luò)請(qǐng)求里該怎樣處理

 for ( NSDictionary *Detaildict in dictArray) {
            //模型初始化
            MyModel *model = [[MyModel alloc]init];
            
            [model setValuesForKeysWithDictionary:Detaildict];
            
            //添加其他大頭針
            MyAnnotation* annotation = [[MyAnnotation alloc]init];
            CLLocationCoordinate2D coor;
            coor = CLLocationCoordinate2DMake([model.latitude doubleValue],   [model.longitude doubleValue]);
           
            // annotation.title = model.name;
            annotation.coordinate = coor;
            annotation.model = model;
            //保存數(shù)據(jù)
            [self.annotationArray addObject:annotation];
            [_mapView addAnnotations:self.annotationArray];
        }
        

數(shù)據(jù),樣式都已經(jīng)有了下面是顯示

#pragma mark 數(shù)據(jù)顯示
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
//這里的判斷你可把它看做和UItableVIew判斷一樣(先去看是不是我的類型)
//這里我的位置使用的是自定義大頭針,判斷是不是我的我的位置
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        
        BMKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"userLocation"];
        
        if(annotationView == nil){
            
            annotationView = [[BMKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"UserLocation"];
        }
        
        annotationView.image = [UIImage imageNamed:@"LOGO.png"];
        return annotationView;
    }
    //不是我的位置加載服務(wù)器提供數(shù)據(jù)(其他類型大頭針)
    CustomPinAnnotationView *annotationView = (CustomPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"otherAnnotationView"];
    if (annotationView == nil) {
        annotationView = [[CustomPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"otherAnnotationView"];
    }
    
    MyAnnotation *myAnnotation = (id)annotation;
    
    //這里開(kāi)始判斷其他大頭針 不同的顯示
    switch ([myAnnotation.model.isvalid intValue]) {
        case 0://斷線
            
            annotationView.image = [UIImage imageNamed:@"gps_me"];
            
            break;
        case 1:
            
            //判斷是否有車位
            
            if([myAnnotation.model.remainder intValue] <= 0){
                
                annotationView.image = [UIImage imageNamed:@"gps_close"];
                
            }else{
                annotationView.image = [UIImage imageNamed:@"gps_open"];
                
            }
            break;
        default:
            break;
    }
    
    NSLog(@"車位:%@" ,myAnnotation.model.remainder);
    
    annotationView.canShowCallout = NO;
    return annotationView;
}

點(diǎn)擊大頭針進(jìn)行業(yè)務(wù)

#pragma mark --private Method--當(dāng)點(diǎn)擊大頭針時(shí)
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view{
    //為了防止點(diǎn)擊我的位置也進(jìn)行跳轉(zhuǎn),所以我們要判斷它哪一種大頭針
    if ([ view isKindOfClass:[CustomPinAnnotationView class ]])
    {
        _MyAnnotation = (id)view.annotation;
        
        TwoViewController *detailCtr = [[TwoViewController alloc]init];
        detailCtr.parkingId = _MyAnnotation.model.ID;
        
        [self presentViewController:detailCtr animated:YES completion:nil];
    }
    
    
}

區(qū)域滑動(dòng)(兩種方式)
第一種,滑到那加載那的數(shù)據(jù),之前加載的不消失

#pragma mark 區(qū)域滑動(dòng)
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    
    //裝換坐標(biāo)
    CLLocationCoordinate2D carLocation = [_mapView convertPoint:self.view.center toCoordinateFromView:self.view];
    BMKReverseGeoCodeOption *option = [[BMKReverseGeoCodeOption alloc] init];
    option.reverseGeoPoint = CLLocationCoordinate2DMake(carLocation.latitude, carLocation.longitude);
    NSLog(@"%f - %f", option.reverseGeoPoint.latitude, option.reverseGeoPoint.longitude);
    
     [self getData:option.reverseGeoPoint];
    
    
}

第二種 滑動(dòng)那加載那數(shù)據(jù),之前加載的大頭針,移除不想要的大頭針

#pragma mark 區(qū)域滑動(dòng)
- (void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    
    //裝換坐標(biāo)
    CLLocationCoordinate2D carLocation = [_mapView convertPoint:self.view.center toCoordinateFromView:self.view];
    BMKReverseGeoCodeOption *option = [[BMKReverseGeoCodeOption alloc] init];
    option.reverseGeoPoint = CLLocationCoordinate2DMake(carLocation.latitude, carLocation.longitude);
    NSLog(@"%f - %f", option.reverseGeoPoint.latitude, option.reverseGeoPoint.longitude);
    //調(diào)用發(fā)地址編碼方法,讓其在代理方法onGetReverseGeoCodeResult中輸出
    
    
    [self.annotationArray removeAllObjects];
    
//    //刪除指定的標(biāo)注
        NSArray* array = [NSArray arrayWithArray:mapView.annotations];
        for (int i = 0; i < [array count]; i ++) {
            if ([array[i] isKindOfClass:[MyAnnotation class]]) {
                [mapView removeAnnotation:array[i]];
            }
        }
    
    
       [_mapView removeAnnotation: _MyAnnotation];
  
//根據(jù)滑動(dòng)坐標(biāo)請(qǐng)求數(shù)據(jù)  
     [self getData:option.reverseGeoPoint];
    
    
}

Demo: https://github.com/HZJason/BdMapstest

最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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