前言:在上一篇中介绍了百度地图sdk的加入,以及定位功能的实现,在本篇将要介绍如何在地图上绘制线,效果如图所示

//

// CustomPaopaotView.h

// DaDa

//

// Created by apple on 2018/3/30.

// Copyright © 2018年 PinBaiHui. All rights reserved.

//添加自定义气泡

#import

@interface CustomPaopaotView : UIView

@property (nonatomic, strong) UIImage *image; //商户图

@property (nonatomic, copy) NSString *title; //商户名

@property (nonatomic, copy) NSString *subtitle; //地址

@end

//

// CustomPaopaotView.m

// DaDa

//

// Created by apple on 2018/3/30.

// Copyright © 2018年 PinBaiHui. All rights reserved.

//

#import "CustomPaopaotView.h"

#define kPortraitMargin 5

#define kPortraitWidth 70

#define kPortraitHeight 50

#define kTitleWidth 120

#define kTitleHeight 20

#define kArrorHeight 20

@interface CustomPaopaotView ()

//定义用于显示气泡内容的控件

@property (nonatomic, strong) UIImageView *portraitView;

@property (nonatomic, strong) UILabel *subtitleLabel;

@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation CustomPaopaotView

- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

self.backgroundColor = [UIColor clearColor];

[self initSubViews];

}

return self;

}

- (void)initSubViews

{

// 添加图片,即商户图

self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(kPortraitMargin, kPortraitMargin, kPortraitWidth, kPortraitHeight)];

self.portraitView.backgroundColor = [UIColor redColor];

[self addSubview:self.portraitView];

// 添加标题,即商户名

self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 + kPortraitWidth, kPortraitMargin, kTitleWidth, kTitleHeight)];

self.titleLabel.font = [UIFont boldSystemFontOfSize:14];

self.titleLabel.textColor = [UIColor whiteColor];

self.titleLabel.text = self.title;

[self addSubview:self.titleLabel];

// 添加副标题,即商户地址

self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 + kPortraitWidth, kPortraitMargin * 2 + kTitleHeight, kTitleWidth, kTitleHeight)];

self.subtitleLabel.font = [UIFont systemFontOfSize:12];

self.subtitleLabel.textColor = [UIColor lightGrayColor];

self.subtitleLabel.text = self.subtitle;

[self addSubview:self.subtitleLabel];

}

- (void)setTitle:(NSString *)title

{

self.titleLabel.text = title;

}

- (void)setSubtitle:(NSString *)subtitle

{

self.subtitleLabel.text = subtitle;

}

- (void)setImage:(UIImage *)image

{

self.portraitView.image = image;

}

//绘制弹出气泡的背景

- (void)drawRect:(CGRect)rect

{

[self drawInContext:UIGraphicsGetCurrentContext()];

self.layer.shadowColor = [[UIColor greenColor] CGColor];

self.layer.shadowOpacity = 1.0;

self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);

}

- (void)drawInContext:(CGContextRef)context

{

CGContextSetLineWidth(context, 2.0);

CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.8].CGColor);

[self getDrawPath:context];

CGContextFillPath(context);

}

- (void)getDrawPath:(CGContextRef)context

{

CGRect rrect = self.bounds;

CGFloat radius = 6.0;

CGFloat minx = CGRectGetMinX(rrect),

midx = CGRectGetMidX(rrect),

maxx = CGRectGetMaxX(rrect);

CGFloat miny = CGRectGetMinY(rrect),

maxy = CGRectGetMaxY(rrect)-kArrorHeight;

CGContextMoveToPoint(context, midx+kArrorHeight, maxy);

CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);

CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);

CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);

CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);

CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);

CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);

CGContextClosePath(context);

}

@end

//

// CustomAnnotationView.h

// DaDa

//

// Created by apple on 2018/3/30.

// Copyright © 2018年 PinBaiHui. All rights reserved.

//

#import

#import "CustomPaopaotView.h"

@interface CustomAnnotationView : BMKAnnotationView

@property (nonatomic, strong) CustomPaopaotView *paopaoViews;

@end

//

// CustomAnnotationView.m

// DaDa

//

// Created by apple on 2018/3/30.

// Copyright © 2018年 PinBaiHui. All rights reserved.

//

#import "CustomAnnotationView.h"

#define kCalloutWidth 150

#define kCalloutHeight 80

@interface CustomAnnotationView ()

@property (nonatomic, strong) UIImageView *bgImageView;

@end

@implementation CustomAnnotationView

-(id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier{

if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {

self.paopaoViews = [[CustomPaopaotView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];

self.paopaoViews.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,-CGRectGetHeight(self.paopaoView.bounds) / 2.f + self.calloutOffset.y);

self.paopaoViews.image = [UIImage imageNamed:@"头像"];

self.paopaoViews.title = self.annotation.title;

self.paopaoViews.subtitle = self.annotation.subtitle;

BMKActionPaopaoView *paopao=[[BMKActionPaopaoView alloc]initWithCustomView:self.paopaoViews];

self.paopaoView = paopao;

}

return self;

}

@end

//

// AnnotationController.m

// 地图

//

// Created by apple on 2018/4/2.

// Copyright © 2018年 zj. All rights reserved.

//

#import "AnnotationController.h"

#import "CustomAnnotationView.h"

@interface AnnotationController (){

BMKLocationService* _locService;

BMKMapView* _mapView;

BMKGeoCodeSearch *_geoCodeSearch;

}

@end

@implementation AnnotationController

-(void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];

_locService.delegate = self;

_mapView.delegate = self;

}

-(void)viewWillDisappear:(BOOL)animated {

[super viewWillDisappear:animated];

_locService.delegate = nil;// 此处记得不用的时候需要置nil,否则影响内存的释放

_mapView.delegate = nil; // 不用时,置nil

}

- (void)viewDidLoad {

[super viewDidLoad];

self.title = @"定位";

self.view.backgroundColor = [UIColor whiteColor];

// 设置地图定位

[self setupBMKLocation];

}

- (void)setupBMKLocation {

//初始化地图

_mapView = [[BMKMapView alloc]init];

_mapView.frame = self.view.bounds;

_mapView.delegate = self;

[self.view addSubview:_mapView];

//初始化BMKLocationService

_locService = [[BMKLocationService alloc]init];

_locService.delegate = self;

// 初始化编码服务

_geoCodeSearch = [[BMKGeoCodeSearch alloc] init];

_geoCodeSearch.delegate = self;

// 启动LocationService

[_locService startUserLocationService];

_mapView.showsUserLocation = YES;//显示定位图层

_mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态为定位跟随模式

}

#pragma mark - BMKLocationServiceDelegate 实现相关delegate 处理位置信息更新

/**

*在地图View将要启动定位时,会调用此函数

*@param mapView 地图View

*/

- (void)willStartLocatingUser

{

NSLog(@"start locate");

}

/**

*用户方向更新后,会调用此函数

*@param userLocation 新的用户位置

*/

- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation

{

NSLog(@"heading is %@",userLocation.heading);

}

/**

*用户位置更新后,会调用此函数

*@param userLocation 新的用户位置

*/

- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation

{

NSLog(@"didUpdateUserLocation lat %f,long %f", userLocation.location.coordinate.latitude, userLocation.location.coordinate.longitude);

BMKCoordinateRegion region;

region.center.latitude = userLocation.location.coordinate.latitude;

region.center.longitude = userLocation.location.coordinate.longitude;

region.span.latitudeDelta = 0.2;

region.span.longitudeDelta = 0.2;

if (_mapView)

{

_mapView.region = region;

}

[_mapView setZoomLevel:20.0];

[_locService stopUserLocationService];//定位完成停止位置更新

//添加当前位置的标注

CLLocationCoordinate2D coord;

coord.latitude = userLocation.location.coordinate.latitude;

coord.longitude = userLocation.location.coordinate.longitude;

BMKPointAnnotation *_pointAnnotation = [[BMKPointAnnotation alloc] init];

_pointAnnotation.coordinate = coord;

_pointAnnotation.title = @"我的位置";// 要显示的标题;注意:如果不设置title,无法点击annotation,也无法使用回调函数

_pointAnnotation.subtitle = @"hello";//副标题

//反地理编码出地理位置

CLLocationCoordinate2D pt=(CLLocationCoordinate2D){0,0};

pt=(CLLocationCoordinate2D){coord.latitude,coord.longitude};

BMKReverseGeoCodeOption *reverseGeoCodeOption = [[BMKReverseGeoCodeOption alloc] init];

reverseGeoCodeOption.reverseGeoPoint = pt;

//发送反编码请求.并返回是否成功

BOOL flag = [_geoCodeSearch reverseGeoCode:reverseGeoCodeOption];

if (flag) {

NSLog(@"反geo检索发送成功");

} else {

NSLog(@"反geo检索发送失败");

}

dispatch_async(dispatch_get_main_queue(), ^{

[_mapView setCenterCoordinate:coord animated:true];

[_mapView addAnnotation:_pointAnnotation];

[_mapView selectAnnotation:_pointAnnotation animated:NO]; //默认选中大头针

});

}

//设置标注样式

-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation{

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

static NSString *pointReuseIndentifier = @"pointReuseIndentifier";

CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];

if (annotationView == nil) {

annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];

}

annotationView.canShowCallout= YES; //设置气泡可以弹出,默认为NO

annotationView.draggable = YES; //设置标注可以拖动,默认为NO

annotationView.image = [UIImage imageNamed:@"头像"];

return annotationView;

}

return nil;

}

/**

*在地图View停止定位后,会调用此函数

*

*/

- (void)didStopLocatingUser

{

NSLog(@"stop locate");

}

/**

*定位失败后,会调用此函数

*

*@param error 错误号,参考CLError.h中定义的错误号

*/

- (void)didFailToLocateUserWithError:(NSError *)error

{

NSLog(@"location error");

}

// 反地理编码

- (void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error {

if (error == 0) {

NSLog(@"%@, %@", [result.poiList.firstObject city], result.address);

}

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

到此就结束了在自定义大头针,后续会更新关于地图的别的功能,尽请期待.....

demo地址

有没有帮到你呢?

android 百度地图大头针,百度地图的集成 ---自定义大头针和弹窗相关推荐

  1. android 百度地图大头针,百度地图自定义大头针大头针

    百度地图自定义大头针&&大头针 查看次数:6802 下载次数:938 上传时间:2016-08-01 大小:491 B # YWLJMapView 利用百度地图实现自定义大头针和气泡 ...

  2. 基于android的百度地图_百度的71个炸天的开源项目

    本文为大家整理了百度开源的70+项目,看看有没有感兴趣的. 1.JavaScript图表库 ECharts ECharts开源来自百度商业前端数据可视化团队,基于html5 Canvas,是一个纯Ja ...

  3. android 高德卫星地图数据,白马地图 Bmap for Android v7.3.81 强大高德百度地图应用|张小北...

    Bmap for Android 是一款集成高德地图百度地图为一体的第三方地图应用APP,Bmap for Android 又叫白马地图,采用高德/百度地图数据引擎,可任意切换百度地图.高德地图数据源 ...

  4. Android 地图导航调用百度地图、高德地图、腾讯地图,腾讯T3团队整理

    前言 ============================================================= 为什么调用第三方呢?集成在App里面不行吗? 接入导航SDK,以百度为 ...

  5. 微信JS-SDK之地理位置的获取与在线导航,集成百度地图实现在线地图搜索

    原创声明:本文来源于本人另一博客[微信JS-SDK之地理位置的获取,集成百度地图实现在线地图搜索]原创作品,绝非他处摘取,转载请联系博主 本次讲解微信开发第三篇:获取用户地址位置信息,是非常常用的功能 ...

  6. Android中级篇之百度地图SDK v3.5.0-一步一步带你仿各大主流APP地图定位移动选址功能

    from: http://blog.csdn.net/y1scp/article/details/49095729 定位+移动选址 百学须先立志-学前须知: 我们经常在各大主流APP上要求被写上地址, ...

  7. ios点击大头针气泡不弹出_百度地图使用(二)自定义大头针和弹出气泡

    百度地图使用(二)自定义大头针和弹出气泡 (2014-08-19 10:37:09) 标签: 时尚 分类: IOS http://www.aichengxu.com/article/系统优化/1149 ...

  8. android百度地图单点定位_Android百度地图实现搜索和定位及自定义图标绘制并点击时弹出泡泡...

    一.问题描述 上一次我们使用百度地图实现基本的定位功能,接下来我们继续实现搜索和定位,并使用LocationOverlay绘制定位位置,同时展示如何使用自定义图标绘制并点击时弹出泡泡 如图所示: 二. ...

  9. android 仿百度地图,仿百度地图街景实现

    使用过百度地图的同学知道,它有个街景功能,可以看到许多地方的实景.这里就其街景内容的实现,进行下学习. 在百度地图SDK的官网上可以看到,百度对开发者提供了很多相关的内容,方便我们进行学习.关于SDK ...

最新文章

  1. 南方科技大学唐圆圆组招聘环境相关领域科研人才(年薪33~50万)
  2. Windows程序的基本结构(转)
  3. 【考研】2022温州大学计算机学硕招收调剂
  4. Adapter适配器设计模式
  5. [pytorch、学习] - 5.4 池化层
  6. Python~win32com~Excel
  7. 获取占用fd最大的前20个进程
  8. Dapper在.Net中的使用(二)
  9. 【520有奖征文】 老同学聚会,20年IT行业从业感悟
  10. java object jsonobject_JSONObject简介
  11. mimimiaaaaaa
  12. 转 《pywinauto进行win32应用程序的测试》
  13. C/C++获取文件大小
  14. 眼镜蛇效应:事与愿违的经济学教训
  15. Linux 安装Python3
  16. 黑苹果使用Hackintool注入声卡驱动
  17. 华为手机备忘录的字体怎么调大?
  18. 一个中科大差生的8年程序员工作总结
  19. 扫地机器人基本设计方案
  20. java迁移框架_Java敏捷数据库迁移框架——Flyway

热门文章

  1. 面向可维护性的设计模式
  2. python处理pdf文件
  3. [copy-webpack-plugin] unable to locate的错误解决
  4. 江苏2021高考成绩查询全省排名,2021年江苏高考成绩排名查询系统,江苏高考位次排名查询...
  5. 数学题 识别 批改 python_准确率99.9%!AI批改数学题,误判率仅为人工1/10,计算填空应用题都能批改...
  6. 布隆过滤器:一种低空间成本的判断元素是否存在的方式
  7. Feedforward ANC 主动降噪原理
  8. html中字的属性设置,html怎么设置字体属性
  9. 花开就是禅,多情即佛心
  10. ORACLE 11g 通过ASH结合AWR实战解决cpu高负载的详细过程