首页 > 编程知识 正文

地图上怎么标注位置,高德地图怎么显示公司

时间:2023-05-03 12:03:19 阅读:44924 作者:4361

关于在项目中遇到的黄金地图的书写问题,本人差点没有问10万个理由10万次。 首先,说明一下自己面临的问题吧。 地图加载完成后,将自动显示自定义的尺寸点和气泡框。 然后,单击气泡框时,可以直接响应气泡框上的Button事件。 和一些人一样,开始在网上找相关的帖子,结果被告知基本上不重要。 想想都觉得心里堵得慌,没办法。 我自己写了演示测试,然后就像自己开始怀疑人生一样写了。 适可而止吧。 直接上了代码。 希望下次有人遇到就避开这个洞。 一、手动配置黄金地图SDK。 这里是自己直接手动配置SDK,当然有一些故障。 没办法,网络不好,辅酶a波德又下不了半天。 本节介绍了几个手动配置步骤,但实际上,您将更直接地前往黄金开发人员中心查看文档。 有相当精细的结构。 黄金SDK下载地址: (http://lbs.AMAP.com/API/IOs-SDK/download/) ) )。 下载结束后,直接拉进项目中自己需要的包里,当然这里本人只拉了三个需要的包。 是AMAP 3d地图、AMAP基础设施和AMAP位置。 包好了。 当然还没结束。 如果继续手动添加依存库,并且不添加依存库就无法成功编译,则会显示无数红色警告。 然后找到工程。 添加此从属库后,再次编辑。 嗯,没关系。 你可以走一步。 二、配置黄金api密钥黄金的api,首先需要账号。 so,如果没有帐户,请先注册一个,然后在控制台上创建“我的APP”以设置APP信息。 提交后会自动生成apiKey,然后复制并保存此key。 在项目的项目中使用。 在这里说明。 绑定id必须与项目的绑定标识符匹配。 因为一个key只能对应一个ID 三.在要配置AppDelegate的AppDelegate.m文件中,首先导入第三方库amapfoundationkit/amapfoundationkit.h,然后通过以下方法将类称为AMAP服务 -(bool )应用: (ui应用)应用didfinishlaunchingwithoptions : (ns dictionary ) launchOptions { [ amapservicessharedservices ].API key=@ ]xxxxx

既然创建了标注,前面说需要加载自定义气泡,那也就需要去重写构造。调用下面的这个方法:

CustomAnnotationView是关于气泡的自定义样式,因为气泡本来是创建在标注的视图层上,所以在创建的时候需要去继承:MAAnnotationView这个类

在CustomAnnotationView.m文件里加载自定义气泡样式的CustomCalloutView,并且将数据传递给气泡CustomCalloutView上需要显示的label字符串。

创建自定义气泡View

在 .h 文件中创建需要传值的字符串、图片和按钮

在.m文件里实现自定义View的样式

#import "CustomCalloutView.h"#define kArrorHeight 10#define kTitleWidth 200#define kTitleHeight 20 #define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]@interface CustomCalloutView()@property (nonatomic, strong) UILabel *subtitleLabel;@property (nonatomic, strong) UILabel *titleLabel;@property (nonatomic, strong) UIImageView *pointImge;@end@implementation CustomCalloutView//创建气泡背景样式- (void)drawRect:(CGRect)rect{[self drawInContext:UIGraphicsGetCurrentContext()];self.layer.shadowColor = [[UIColor blackColor] CGColor];self.layer.shadowOpacity = 0.2;self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);}- (void)drawInContext:(CGContextRef)context{CGContextSetLineWidth(context, 2.0);CGContextSetFillColorWithColor(context, [UIColor whiteColor].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);}- (id)initWithFrame:(CGRect)frame{self = [super initWithFrame:frame];if (self){ self.backgroundColor = [UIColor clearColor]; [self initSubViews];}return self;}//自定义样式- (void)initSubViews{// 添加图片,即商户图self.pointImge = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 16, 18)];// self.pointImge.backgroundColor = COLOR(102, 102, 102, 1.0);[self addSubview:self.pointImge];// 添加标题,即商户名self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(35, 8, kTitleWidth, kTitleHeight)];self.titleLabel.font = [UIFont boldSystemFontOfSize:12];self.titleLabel.textColor = COLOR(102, 102, 102, 1.0);// self.titleLabel.text = @"titletitletitletitle";[self addSubview:self.titleLabel];// 添加副标题,即商户地址self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(35 ,5, kTitleWidth, kTitleHeight+30)];self.subtitleLabel.numberOfLines = 0;//表示label可以多行显示self.subtitleLabel.font = [UIFont boldSystemFontOfSize:12];self.subtitleLabel.textColor = COLOR(51, 51, 51, 1.0);// self.subtitleLabel.text = @"subtitleLabelsubtitleLabelsubtitleLabel";[self addSubview:self.subtitleLabel];//添加按钮self.button = [UIButton buttonWithType:UIButtonTypeCustom];self.button.frame = self.bounds;[self.button addTarget:self action:@selector(respondsToOnBtn) forControlEvents:UIControlEventTouchUpInside];[self addSubview:self.button];}- (void)setTitle:(NSString *)title{ self.titleLabel.text = title;}- (void)setSubtitle:(NSString *)subtitle{self.subtitleLabel.text = subtitle;}- (void)respondsToOnBtn{ NSLog(@"11111111111");}

恩,写到这里代码基本上就算全部完成了。运行工程跑起来,也能看到默认的标注点了,but,气泡却没有自动弹出来,也响应不了按钮的点击事件。why? 然后就开始搜索技术贴,果然,有一些收获,有人说是因为本来气泡就写在了标注的视图上,层级关系不对,所以响应不到按钮,相对应的解决方法:在CustomAnnotationView.m文件中添加

- (UIView )hitTest:(CGPoint)point withEvent:(UIEvent )event

解释一下这个方法的原理:iOS系统检测到手指触摸(Touch)操作时会将其放入当前活动Application的事件队列,UIApplication会从事件队列中取出触摸事件并传递给key window(当前接收用户事件的窗口)处理,window对象首先会使用hitTest:withEvent:方法寻找此次Touch操作初始点所在的视图(View),即需要将触摸事件传递给其处理的视图,称之为hit-test view。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {UIView *view = [super hitTest:point withEvent:event];if (view == nil) { CGPoint tempoint = [self.calloutView.button convertPoint:point fromView:self]; if (CGRectContainsPoint(self.calloutView.button.bounds, tempoint)) { view = self.calloutView.button; }}return view;}

这里设置了,按道理说,应该能够响应气泡层的按钮事件了。可是,地图上应该先弹出气泡,才能响应按钮事件。到这里,我又卡住了。然后直接在MAAnnotation代理中查看了相关代理方法。发现了有一个selected 的属性,瞬间欣喜如狂,在这下面加上了这个选中的属性。再次运行Demo.有一种感觉自己很聪明的傲娇。结果,地图上确实能够显示标注了,可是却点不动,而且点击气泡以外的View位置,居然气泡不消失了。what?这又是怎么回事…..

然后,在经历了多次测试后发现,气泡要默认弹出显示在地图上,是需要加载定位这个属性所调用的方法。在初始化加载地图的后面补上这段代码。

self.locationManager = [[AMapLocationManager alloc]init];//获取定位创建个人坐标self.locationManager.delegate = self;self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;//定位精确度在10米以内 //单次定位[self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) { if (location) { _pointAnnotaion = [[MAPointAnnotation alloc]init]; _pointAnnotaion.title = @"蓝鲸鱼待久大酒店蓝鲸鱼待久大酒店蓝鲸鱼待久大酒店"; CLLocationCoordinate2D coordinate = {30.6594620000,104.0657350000}; _mapView.centerCoordinate = coordinate; _pointAnnotaion.coordinate = coordinate; _mapView.zoomLevel = 17; [self.mapView addAnnotation:self.pointAnnotaion]; //默认选中标注数据对应的view (同时在地图 MAMapViewDelegate方法中,去掉selected这个选中) [self.mapView selectAnnotation:self.pointAnnotaion animated:YES]; }}];

然后就可以完全实现自己想要的效果了。

注:以上均为个人的实现方法和建议,如果有更好的方法,欢迎回复讨论。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。