百度地圖總體封裝大頭針

    版本迭代須要集成百度地圖,產品需求是每一個大頭針上方都須要固定展現大頭針先關的信息,而在集成過程當中,若是經過百度原裝方法點擊大頭針,彈出氣泡,會出現以下幾個問題:git

    1.能夠經過[mapView selectAnnotation:annotation animated:YES]方法在初始化時顯示大頭針氣泡,可是從方法中很容易的看到,若是添加多個大頭針,多個都須要初始化展現氣泡,而它只能展現最後一個添加大頭針的氣泡,沒法實現產品的需求ide

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

    而針對產品的需求,須要顯示多條大頭針信息,百度地圖sdk原裝方法行不通,經過查閱相關資料,能夠將大頭針和睦泡封裝成一個總體,統一當成大頭針使用,並取消點擊大頭針彈出氣泡的方法,這樣有一個小問題就是會出現大頭針偏移的問題,須要用戶根據須要本身調整大頭針顯示位置,設置偏移量;而若是須要點擊大頭針進行相關的操做,能夠經過在大頭針上方添加一個button,設定tag值綁定點擊事件,下面是部分代碼,能夠參考:3d

  在百度地圖的代理方法中建立封裝大頭針代理

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

{事件

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

        

        

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

        

        

        newAnnotationView.backgroundColor = [UIColor clearColor];string

        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;

}

    建立大頭針,並設置大頭針的數據,必須依次添加大頭針到地圖上,不能總體添加

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];

                        //建立一個大頭針,添加一個,防止統一添加代理方法裏面數據混亂

                        [self.mapView addAnnotation:schoolBusAnnotation];

                    }

相關文章
相關標籤/搜索