百度地图自定义marker

turboksiOS183

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












标签: 地图

相关文章

WKWebView

WKWebView

<!DOCTYPE html><head>    <title>HTML</title>    <meta...

OC的实例对象、类对象、元类对象的关系

OC的实例对象、类对象、元类对象的关系

Objective-C中的对象、简称OC对象,主要分为三种:instance对象(实例对象)、class对象(类对象)、meta-class对象(元类对象)。一:instance对象instance对...

iOS一行代码搞定自定义AlertView

iOS一行代码搞定自定义AlertView

效果图走一走:使用方式: pod  'KSAlertView'举例使用,单按钮Alert: [[[KSAlertView alloc] initWithT...

iOS短视频开发之---AVPlayer

iOS短视频开发之---AVPlayer

基于原生的AVPlayer + ScrollView实现,包含视频播放、暂停、点赞动画实现。视频是采用的本地视频、需要网络视频请自行添加添加测试。添加方式:  //本地视频 &n...

GCD定时器

GCD定时器

NSTimer依赖于RunLoop,如果RunLoop的任务过于繁重,可能会导致NSTimer不准时。而GCD的定时器会更加准时。- (void)viewDidLoad {   ...

iOS的block

iOS的block

一:block的本质1.    block本质上是一个OC对象,它内部有个isa指针2.    block是封装了函数调用以及函数调用环境的OC对象3.&nbs...