百度地图自定义marker
#import "HomeViewController.h"
#import "PaopaoView.h"
@interface HomeViewController ()<BMKMapViewDelegate>
@property (nonatomic, strong) BMKMapView * mapView;
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.hidden = YES;
_mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT)];
_mapView.delegate = self;
//设置地图显示比例。
[_mapView setZoomLevel:20];
//显示比例尺
_mapView.showMapScaleBar = YES;
//设置显示类型(0 空白 1 地图 2卫星)
_mapView.mapType = 2;
//设置显示楼层效果
_mapView.buildingsEnabled = YES;
//设置CLLocationCoordinate2D 位置
CLLocationCoordinate2D location;
location.latitude = 39.915111;
location.longitude = 116.403945;
_mapView.centerCoordinate = location;
[self.view addSubview:_mapView];
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
annotation.coordinate = CLLocationCoordinate2DMake(39.915111, 116.403945);
[_mapView addAnnotation:annotation];
}
#pragma mark - BMKMapViewDelegate
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]])
{
static NSString *reuseIndetifier = @"annotationReuseIndetifier";
BMKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
if (annotationView == nil)
{
annotationView = [[BMKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:reuseIndetifier];
}
// annotationView.image = [UIImage imageNamed:@""];
[annotationView setSelected:YES];
annotationView.canShowCallout = YES;
PaopaoView *customPopView = [[PaopaoView alloc] init];
customPopView.frame = CGRectMake(0, 0, 98, 84);
customPopView.image = [UIImage imageNamed:@"ppima"];
// customPopView.title = @"北京";
customPopView.subtitle = @"查看高清街景>>";
customPopView.controller = self;
BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc] initWithCustomView:customPopView];
pView.frame = customPopView.frame;
annotationView.paopaoView = pView;
return annotationView;
}
return nil;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.navigationBar.hidden = YES;
[_mapView viewWillAppear];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationController.navigationBar.hidden = NO;
[_mapView viewWillDisappear];
}
@end
PaopaoView
#import <UIKit/UIKit.h>
@interface PaopaoView : UIView
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) NSString *subtitle;
@property (nonatomic, strong) UIViewController *controller;
@end
#import "PaopaoView.h"
@interface PaopaoView ()
@property (nonatomic, strong) UIImageView *backView;
@property (nonatomic, strong) UIImageView *portraitView;
@property (nonatomic, strong) UILabel *subtitleLabel;
@end
@implementation PaopaoView
#pragma mark - draw rect
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
}
return self;
}
- (void)initSubViews
{
// 添加背景图片
self.backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 98, 84)];
self.backView.image = [UIImage imageNamed:@"ppback"];
[self addSubview:self.backView];
// 添加图片
self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 88, 49)];
self.portraitView.backgroundColor = [UIColor blackColor];
[self addSubview:self.portraitView];
// 添加标题
self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 54, 98, 20)];
self.subtitleLabel.textColor = [UIColor whiteColor];
self.subtitleLabel.text = self.subtitle;
self.subtitleLabel.font = smallFontStyle;
self.subtitleLabel.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.subtitleLabel];
}
- (void)setSubtitle:(NSString *)subtitle
{
self.subtitleLabel.text = subtitle;
}
- (void)setImage:(UIImage *)image
{
self.portraitView.image = image;
}
@end