日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/8 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 百度地图大头针,百度地图的集成 ---自定义大头针和弹窗 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言:在上一篇中介紹了百度地圖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 百度地图大头针,百度地图的集成 ---自定义大头针和弹窗的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。