百度地图自定义marker

turboksiOS132

Simulator Screen Shot - iPhone 12 - 2021-08-05 at 18.38.36.png


#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












标签: 地图

相关文章

iOS程序的内存布局

iOS程序的内存布局

    代码段:编译之后的代码    数据段:字符串常量:比如NSString * str = @“123”已初始化数据:已初始化的全局变量...

iOS多线程

iOS多线程

多线程常见方案一、GCD的函数:GCD中有2个用来执行任务的函数queue:队列block:任务1.    用同步的方式执行任务dispatch_sync(di...

iOS的KVO、KVC

iOS的KVO、KVC

一:KVO  在实际的开发过程中,我们可能会遇到这种需求,就是需要根据一个对象某个属性值的改变,来作出不同的处理。但是实现起来比较麻烦,需要在很多地方进行判断处理,为了方便我们的开...

iOS的分类(Category)

iOS的分类(Category)

一:Category的底层结构定义在objc-runtime-new.h中:struct category_t {    const char *name;   ...

iOS音视频之屏幕录制

iOS音视频之屏幕录制

苹果提供了一个自带的录屏功能、但是每次录屏的上边那个小红条的录制状态栏能忍?当然、平时的一些简单的录制就可以了、但是我们开发的程序要求就不一样了、要求高清和全屏。关于这个功能苹果也是提供了对应的类库供...

RunLoop

RunLoop

RunLoop一、基础理论顾名思义1.    运行循环2.    在程序运行过程中循环做一些事情3.    是用户态和内核态之间的交互模式应...