溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

百度地圖整體封裝大頭針

發(fā)布時間:2020-06-29 18:10:53 來源:網絡 閱讀:965 作者:完美壞蛋911 欄目:開發(fā)技術

    版本迭代需要集成百度地圖,產品需求是每個大頭針上方都需要固定展示大頭針先關的信息,而在集成過程中,如果通過百度原裝方法點擊大頭針,彈出氣泡,會出現(xiàn)如下幾個問題:

    1.可以通過[mapView selectAnnotation:annotation animated:YES]方法在初始化時顯示大頭針氣泡,但是從方法中很容易的看到,如果添加多個大頭針,多個都需要初始化展示氣泡,而它只能展示最后一個添加大頭針的氣泡,無法實現(xiàn)產品的需求

    2.點擊大頭針,彈出氣泡,點擊第二個時第一個大頭針的氣泡會消失,歸結起來就是使用氣泡的方式顯示大頭針相關信息只會顯示一條,不能同時顯示多條信息

    而針對產品的需求,需要顯示多條大頭針信息,百度地圖sdk原裝方法行不通,通過查閱相關資料,可以將大頭針和氣泡封裝成一個整體,統(tǒng)一當成大頭針使用,并取消點擊大頭針彈出氣泡的方法,這樣有一個小問題就是會出現(xiàn)大頭針偏移的問題,需要用戶根據(jù)需要自己調整大頭針顯示位置,設置偏移量;而如果需要點擊大頭針進行相關的操作,可以通過在大頭針上方添加一個button,設定tag值綁定點擊事件,下面是部分代碼,可以參考:

  在百度地圖的代理方法中創(chuàng)建封裝大頭針

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation

{

    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {

        

        

        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];

        

        

        newAnnotationView.backgroundColor = [UIColor clearColor];

        newAnnotationView.p_w_picpath = [UIImage p_w_picpathNamed:@"qiP.png"]; //設置大頭針占位圖片

        newAnnotationView.frame = CGRectMake(-70, -3514070);   //占位圖片為空,需要強制設置大頭針的范圍

        newAnnotationView.userInteractionEnabled = YES;

        newAnnotationView.enabled = YES;

        UIView *view = [[UIView allocinitWithFrame:CGRectMake(0000)];

        //氣泡view

        newAnnotationView.paopaoView = [[BMKActionPaopaoView allocinitWithCustomView:view];

        UIImageView *paoPaoImage = [[UIImageView alloc]init];

        paoPaoImage.frame = CGRectMake(-45, -3514035);

        paoPaoImage.p_w_picpath = [UIImage p_w_picpathNamed:@"qiPao.png"];

        [newAnnotationView addSubview:paoPaoImage];

        //氣泡view上大頭針的信息    

        UILabel *busNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(-35, -355030)];

        busNameLabel.text =busNameStr;

        busNameLabel.textColor = [UIColor whiteColor];

        busNameLabel.backgroundColor = [UIColor clearColor];

        [newAnnotationView addSubview:busNameLabel];    

        UILabel *totalNumLab = [[UILabel alloc]initWithFrame:CGRectMake(25, -357030)];

        totalNumLab.text = bustotalNum;

        totalNumLab.textAlignment = NSTextAlignmentCenter;

        totalNumLab.textColor = [UIColor colorWithRed:245.0/255 green:153.0/255 blue:38.0/255 alpha:1];

        totalNumLab.backgroundColor = [UIColor clearColor];

        [newAnnotationView addSubview:totalNumLab];

        UIImageView *schoolBusImage = [[UIImageView alloc]init];

        schoolBusImage.frame = CGRectMake(005050);

        schoolBusImage.p_w_picpath = [UIImage p_w_picpathNamed:@"schoolBus.png"];

        [newAnnotationView addSubview:schoolBusImage];

        //點擊事件的button

        UIButton *backgroundBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        backgroundBtn.frame = CGRectMake(-15, -10, 70, 70);

        backgroundBtn.tag = busTag;

        [newAnnotationView addSubview:backgroundBtn];

        [backgroundBtn addTarget:self action:@selector(backgroundBtnClick:) forControlEvents:UIControlEventTouchUpInside];

        return newAnnotationView;

        

    }

    return nil;

}

    創(chuàng)建大頭針,并設置大頭針的數(shù)據(jù),必須依次添加大頭針到地圖上,不能整體添加

for (int i = 0; i < self.schoolLeaderArr.count; i++) {

                        

                        double schoolBusLatitude = [self.schoolLeaderArr[i][@"latitude"] doubleValue];

                        double schoolBusLongitude = [self.schoolLeaderArr[i][@"longitude"] doubleValue];

                        schoolBusLocation = CLLocationCoordinate2DMake(schoolBusLatitude, schoolBusLongitude);

                        schoolBusAnnotation = [[BMKPointAnnotation alloc]init];

                        schoolBusAnnotation.coordinate = schoolBusLocation;

                        busNameStr = [NSString stringWithFormat:@"%@",self.schoolLeaderArr[i][@"busName"]];

                        bustotalNum = [NSString stringWithFormat:@"%@/%@",[NSString stringWithFormat:@"%@",self.schoolLeaderArr[i][@"upNum"]],[NSString stringWithFormat:@"%@",self.schoolLeaderArr[i][@"totalNum"]]];

                        busTag = [self.schoolLeaderArr[i][@"busId"] intValue];

                        [self.annotationArr addObject:schoolBusAnnotation];

                        //創(chuàng)建一個大頭針,添加一個,防止統(tǒng)一添加代理方法里面數(shù)據(jù)混亂

                        [self.mapView addAnnotation:schoolBusAnnotation];

                    }

  

 

     

附件:http://down.51cto.com/data/2366532
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI