1. 程式人生 > >iOS中設定百度地圖自定義標註圖片,自定義泡泡

iOS中設定百度地圖自定義標註圖片,自定義泡泡

#pragma mark - BMKMapViewDelegate

// 根據anntation生成對應的View

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

{

    //普通annotation

    NSString *AnnotationViewID = @"ClusterMark";

    ClusterAnnotation *cluster = (ClusterAnnotation*)annotation;

    ClusterAnnotationView *annotationView = [[ClusterAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];

    annotationView.size = cluster.size;

    annotationView.canShowCallout = NO;//在點選大頭針的時候會彈出那個黑框框

    annotationView.draggable = NO;//禁止標註在地圖上拖動

    annotationView.annotation = cluster;

//    annotationView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:annotation.subtitle]]];

    annotationView.centerOffset=CGPointMake(0,0);

    

    UIView *viewForImage=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 110, 110)];

    UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 110, 110)];

    [imageview setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:annotation.subtitle]]]];

    imageview.layer.masksToBounds=YES;

    imageview.layer.cornerRadius = 10;

    [viewForImage addSubview:imageview];

    annotationView.image=[self getImageFromView:viewForImage];

    return annotationView;

}


-(UIImage *)getImageFromView:(UIView *)view{

    UIGraphicsBeginImageContext(view.bounds.size);

    [view.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;

}


//氣泡框左側顯示的View,可自定義
annotationView.leftCalloutAccessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_location.png"]];
//氣泡框右側顯示的View 可自定義
annotationView.rightCalloutAccessoryView =selectButton;
//讓標註在進入介面時就處於彈出氣泡框的狀態
[annotationView setSelected:YES animated:YES];