- 上一篇:百度地图集成--定位
前言:在上一篇中介绍了百度地图
sdk
的加入,以及定位功能的实现,在本篇将要介绍如何在地图上绘制线,效果如图所示
//
// CustomPaopaotView.h
// DaDa
//
// Created by apple on 2018/3/30.
// Copyright © 2018年 PinBaiHui. All rights reserved.
//添加自定义气泡
#import <UIKit/UIKit.h>
@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 <BaiduMapAPI_Map/BMKAnnotationView.h>
#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<BMKAnnotation>)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 ()<BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate,BMKRouteSearchDelegate>{
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<BMKAnnotation>)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地址
有没有帮到你呢?😁
(欢迎大家对不合适的地方进行指正,看完觉得有帮到你给点个赞👍吧)
网友评论