android 百度地图大头针,百度地图的集成 ---自定义大头针和弹窗
前言:在上一篇中介紹了百度地圖sdk的加入,以及定位功能的實(shí)現(xiàn),在本篇將要介紹如何在地圖上繪制線,效果如圖所示
//
// 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 ()
//定義用于顯示氣泡內(nèi)容的控件
@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];
// 添加標(biāo)題,即商戶名
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];
// 添加副標(biāo)題,即商戶地址
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;// 此處記得不用的時(shí)候需要置nil,否則影響內(nèi)存的釋放
_mapView.delegate = nil; // 不用時(shí),置nil
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"定位";
self.view.backgroundColor = [UIColor whiteColor];
// 設(shè)置地圖定位
[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;
// 初始化編碼服務(wù)
_geoCodeSearch = [[BMKGeoCodeSearch alloc] init];
_geoCodeSearch.delegate = self;
// 啟動(dòng)LocationService
[_locService startUserLocationService];
_mapView.showsUserLocation = YES;//顯示定位圖層
_mapView.userTrackingMode = BMKUserTrackingModeFollow;//設(shè)置定位的狀態(tài)為定位跟隨模式
}
#pragma mark - BMKLocationServiceDelegate 實(shí)現(xiàn)相關(guān)delegate 處理位置信息更新
/**
*在地圖View將要啟動(dòng)定位時(shí),會(huì)調(diào)用此函數(shù)
*@param mapView 地圖View
*/
- (void)willStartLocatingUser
{
NSLog(@"start locate");
}
/**
*用戶方向更新后,會(huì)調(diào)用此函數(shù)
*@param userLocation 新的用戶位置
*/
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
NSLog(@"heading is %@",userLocation.heading);
}
/**
*用戶位置更新后,會(huì)調(diào)用此函數(shù)
*@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];//定位完成停止位置更新
//添加當(dāng)前位置的標(biāo)注
CLLocationCoordinate2D coord;
coord.latitude = userLocation.location.coordinate.latitude;
coord.longitude = userLocation.location.coordinate.longitude;
BMKPointAnnotation *_pointAnnotation = [[BMKPointAnnotation alloc] init];
_pointAnnotation.coordinate = coord;
_pointAnnotation.title = @"我的位置";// 要顯示的標(biāo)題;注意:如果不設(shè)置title,無法點(diǎn)擊annotation,也無法使用回調(diào)函數(shù)
_pointAnnotation.subtitle = @"hello";//副標(biāo)題
//反地理編碼出地理位置
CLLocationCoordinate2D pt=(CLLocationCoordinate2D){0,0};
pt=(CLLocationCoordinate2D){coord.latitude,coord.longitude};
BMKReverseGeoCodeOption *reverseGeoCodeOption = [[BMKReverseGeoCodeOption alloc] init];
reverseGeoCodeOption.reverseGeoPoint = pt;
//發(fā)送反編碼請(qǐng)求.并返回是否成功
BOOL flag = [_geoCodeSearch reverseGeoCode:reverseGeoCodeOption];
if (flag) {
NSLog(@"反geo檢索發(fā)送成功");
} else {
NSLog(@"反geo檢索發(fā)送失敗");
}
dispatch_async(dispatch_get_main_queue(), ^{
[_mapView setCenterCoordinate:coord animated:true];
[_mapView addAnnotation:_pointAnnotation];
[_mapView selectAnnotation:_pointAnnotation animated:NO]; //默認(rèn)選中大頭針
});
}
//設(shè)置標(biāo)注樣式
-(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; //設(shè)置氣泡可以彈出,默認(rèn)為NO
annotationView.draggable = YES; //設(shè)置標(biāo)注可以拖動(dòng),默認(rèn)為NO
annotationView.image = [UIImage imageNamed:@"頭像"];
return annotationView;
}
return nil;
}
/**
*在地圖View停止定位后,會(huì)調(diào)用此函數(shù)
*
*/
- (void)didStopLocatingUser
{
NSLog(@"stop locate");
}
/**
*定位失敗后,會(huì)調(diào)用此函數(shù)
*
*@param error 錯(cuò)誤號(hào),參考CLError.h中定義的錯(cuò)誤號(hào)
*/
- (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
到此就結(jié)束了在自定義大頭針,后續(xù)會(huì)更新關(guān)于地圖的別的功能,盡請(qǐng)期待.....
demo地址
有沒有幫到你呢?😁
(歡迎大家對(duì)不合適的地方進(jìn)行指正,看完覺得有幫到你給點(diǎn)個(gè)贊👍吧)
總結(jié)
以上是生活随笔為你收集整理的android 百度地图大头针,百度地图的集成 ---自定义大头针和弹窗的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA实现对阿里云DNS的解析管理
- 下一篇: 关于Visual Studio 2017