iOS百度地圖的相關開發(一)

我是最近接觸的百度讀圖開發,百度地圖的相關sdk api開發寫的很簡單,有不少都是寫的不是很詳細,可是若是咱們想深究就會遇到不少問題,有許多咱們想得到都東西每每都無法得到。直接步入正題吧,遇到問題以下:git

一:我新申請的key,爲啥沒有用?地圖無法加載,這個頗有多是你申請的key,有了,可是沒有認證開發者,這就致使你的key沒有用,地圖沒有加載出來,這個問題困擾了我好久才發現。
api

二:第一次使用百度地圖的時候,必要的就是看Demo怎麼使用這些方法。在使用官方Demo的時候地圖真機測試就會出錯,這個錯誤我大概記得,一個別忘申請新的key,二,別忘了選擇證書,每每這個會致使錯誤app

CodeSign error: code signing is required for product type 'Application' in SDK 'iOS 9.2'測試

這個就是證書沒有選擇的錯誤ui

三:按照官方文檔建立地圖後以及寫了相關方法,這裏我就很少說了,值得注意的是若是咱們若是想獲取當前的經緯度的話就得引入定位服務,經過atom

 [self.myLocationService startUserLocationService];spa

設置BMKLocationService 代理

的代理人經過代理方法開啓定位服務後就會執行updatelocation方法獲取當前的經緯度,設置爲地圖中心點。值得注意的事,要在這中止定位服務,要否則的你的如何移動地圖,地圖的中心點會在隔幾秒會回到原處。這是我我的的方法,不對的話,給我提出來。同事顯示定位的圖層code

 self.myLocationService = [[BMKLocationService alloc]init];
    [self.myLocationService startUserLocationService];//開始定位服務
//    self.mapView.showsUserLocation = NO;
//    self.mapView.userTrackingMode = BMKUserTrackingModeFollow;
//    self.mapView.showsUserLocation = YES;
    self.myMapView.showsUserLocation = NO;
    self.myLocationService.delegate = self;
    self.myMapView.userTrackingMode = BMKUserTrackingModeFollow;//設置定位的狀態
    self.myMapView.showsUserLocation = YES;//顯示定位圖層

四:自定義定位圖層的圖標,也許大家應該使用過百度的導航服務,仔細的人就會發現,地位圖標是跟隨樣式,若是要由於這個圖標旋轉,就引入導航的SDK那就有點大材小用,致使APP的包過大,其實在咱們能夠重寫這個orm

BMKLocationViewDisplayParam

這個類對象,根據將其中的imagename改爲你的那個替換圖片就行

 BMKLocationViewDisplayParam *param = [[BMKLocationViewDisplayParam alloc] init];
    param.isRotateAngleValid = YES;
    param.isAccuracyCircleShow = YES;
    param.locationViewImgName = @"icon_center_point1";
    param.locationViewOffsetX = 0;
    param.locationViewOffsetY = 0;
    [self.myMapView updateLocationViewWithParam:param];

icon_center_poin這個是boudle資源中的定位圖層的那個圖片名稱,能夠換成你想要的圖片名稱就行

五:自定義大投標,這個就須要你本身寫一個類繼承

BMKAnnotationView

這個類就行,而後就在其中建立你想要的一些信息,也能夠建立paopaoView這個就是你點擊這個大頭針彈出的時候,你就能夠自定義視圖,想展現什麼內容就展現什麼內容。這個是我自定義大投標的的那個類。m文件中主要的方法

@interface LL_MapViewAnnotationView()
@property(nonatomic,strong)UIImageView *myHeadImageView;
@end
@implementation LL_MapViewAnnotationView
@synthesize headViewName;
-(id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setBounds:CGRectMake(0, 0, 50, 55)];
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 55)];
        [button setBackgroundImage:[UIImage imageNamed:@"btn_肯定"] forState:UIControlStateNormal];
        [self addSubview:button];
        self.myHeadImageView = [[UIImageView alloc]initWithFrame:CGRectMake(2, 5, 45, 45)];
        self.myHeadImageView.clipsToBounds = YES;
        self.myHeadImageView.layer.cornerRadius = 22.5;
        [button addSubview:self.myHeadImageView];
        button.userInteractionEnabled = NO;
        BMKActionPaopaoView *paopaoView = [[BMKActionPaopaoView alloc]initWithCustomView:[self createPaopaoViewWithModel]];
        self.paopaoView = paopaoView;
    }
    return self;
}
-(UIView *)createPaopaoViewWithModel
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 250,143)];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, 143)];
    [view addSubview:lineView];
    
    UIView *allView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 250, 143)];
    [lineView addSubview:allView];
//    view.alpha = 0.4;
    allView.backgroundColor = [[Unity getColor:@"#000000"] colorWithAlphaComponent:0.4];
   
    
    
    //頭像
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(15, 8, 100, 100)];
    [imageView setImage:[UIImage imageNamed:@"img1"]];
    imageView.clipsToBounds = YES;
    imageView.layer.cornerRadius = 50;
    [view addSubview:imageView];
    
    //刪除button
    UIButton *deleateBut =[[UIButton alloc]initWithFrame:CGRectMake(view.frame.size.width -5-20, 8, 20, 20)];
    [deleateBut setBackgroundImage:[UIImage imageNamed:@"ic_btn_Deleate"] forState:UIControlStateNormal];
    deleateBut.tag = 1001;
    [view addSubview:deleateBut];
    [deleateBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
    
    //距離Lable
    UILabel *distence = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame)+20, 14, 101, 20)];
    [view addSubview:distence];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc]init];
    //添加圖片的
    NSTextAttachment *attach = [[NSTextAttachment alloc]init];
    attach.image = [UIImage imageNamed:@"ic_find_Distence"];
    attach.bounds =CGRectMake(0, -5, 30, 20);
    NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
    [string appendAttributedString:attachString];
    
    NSString *tempStr = [NSString stringWithFormat:@"  距離"];
    NSMutableAttributedString *textStr = [[NSMutableAttributedString alloc]initWithString:tempStr];
    [textStr addAttribute:NSForegroundColorAttributeName value:[Unity getColor:@"#FFFFFF"] range:NSMakeRange(0, tempStr.length)];
    [textStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, tempStr.length)];
    [string appendAttributedString:textStr];
    distence.attributedText = string;

    
    //距離數量Lable
    UILabel *distenceNum = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(distence.frame), CGRectGetMaxY(distence.frame), view.frame.size.width-CGRectGetMinX(distence.frame)-40, 30)];
    NSString *distenceNumText = [NSString stringWithFormat:@"153  米"];
    NSMutableAttributedString *distenceString = [[NSMutableAttributedString alloc]initWithString:distenceNumText];
    [distenceString addAttribute:NSForegroundColorAttributeName value:[Unity getColor:@"#FFFFFF"] range:NSMakeRange(0, 3)];
    [distenceString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:19] range:NSMakeRange(0, 3)];
    [distenceString addAttribute:NSForegroundColorAttributeName value:[Unity getColor:@"#FDB100"] range:NSMakeRange(5, 1)];
    [distenceString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:11] range:NSMakeRange(5, 1)];
    distenceNum.attributedText = distenceString;
    distenceNum.textAlignment = NSTextAlignmentCenter;
    [view addSubview:distenceNum];
    
    
    UIView *downLoadView = [[UIView alloc]initWithFrame:CGRectMake(0, 90, 250, 53)];
    downLoadView.backgroundColor = [[Unity getColor:@"#4D4D4D"] colorWithAlphaComponent:0.5];
    [view addSubview:downLoadView];
    
    UIView *sexBackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 35, 53)];
    [sexBackView setBackgroundColor:[Unity getColor:@"#FFB300"]];
    [downLoadView addSubview:sexBackView];
    UIImageView *sexImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 14, 25, 25)];
    [sexBackView addSubview:sexImageView];
    sexImageView.clipsToBounds = YES;
    sexImageView.layer.cornerRadius = 12.5;
    [sexBackView addSubview:sexImageView];
    [sexImageView setImage:[UIImage imageNamed:@"ic_男"]];
    
    //姓名lable
    UILabel *nameLable = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(sexBackView.frame)+12, 8, 120, 25)];
    nameLable.textColor = [Unity getColor:@"#FFFFFF"];
    nameLable.font = [UIFont systemFontOfSize:18];
    nameLable.text = @"張三";
    [downLoadView addSubview:nameLable];
    
    
    //個性簽名
    UILabel *mySignText = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(nameLable.frame), CGRectGetMaxY(nameLable.frame), 120, 20)];
    mySignText.textColor = [Unity getColor:@"#FFFFFF"];
    mySignText.font = [UIFont systemFontOfSize:12];
    mySignText.text = @"個性簽名";
    [downLoadView addSubview:mySignText];
    
    //查看button
    UIButton *lookButton = [[UIButton alloc]initWithFrame:CGRectMake(downLoadView.frame.size.width-10-80, 20, 80, 30)];
    [lookButton setImage:[UIImage imageNamed:@"ic_look_detail"] forState:UIControlStateNormal];
    [lookButton setTitle:@"查看" forState:UIControlStateNormal];
    [lookButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    lookButton.tag = 1002;
    [lookButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchDown];
    [downLoadView addSubview:lookButton];
    
    
    return view;
}

六:在建立大頭針的時候也很簡單和官方的說法同樣,值得注意的就是添加其中的一些方法就行

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    
    NSString *AnnotationViewID = @"AnimatedAnnotation";
    LL_MapViewAnnotationView *annotationView = nil;
    if (annotationView == nil) {
        
        annotationView = [[LL_MapViewAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
        [annotationView upHeadViewNameWithString:@"img1"];
        annotationView.myBlock = ^(NSInteger tag){
            switch (tag) {
                case 1001:
                {
                    NSLog(@"刪除按鈕");
                }
                    break;
                case 1002:
                {
                    NSLog(@"查看按鈕");
                }
                    break;
                default:
                    break;
            }
        };
    }
    
    return annotationView;
    
}

這段代碼是加大頭針的方法,裏面不要管別的,其餘的不用想,想多了就很麻煩,我以前就是那樣覺得經過什麼方法調用就行,其實這個跟tableView同樣給個數據,設定高度,cell的數量,而後就是在代理方法中寫返回的cell的樣式就行

BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor;
    coor.latitude = 39.915;
    coor.longitude = 116.404;
    annotation.coordinate = coor;
    annotation.title = @"這裏是北京";
    [self.myMapView addAnnotation:annotation];

其實還有別的問題,明天再說,今天有點累了,要睡覺了

相關文章
相關標籤/搜索