高德地图(百度地图,Google地图)中自定义Annotation&CallOutView
来源:互联网 发布:崩坏三淘宝 编辑:程序博客网 时间:2024/05/10 11:35
本篇介绍如何在高德地图中自定义Annotation的CallOutView
本文最后会附上App的Github地址。
之前在网上看到一篇百度地图的自定义CallOutView的方法,但是应用在高德上,一直出现这样那样的问题,下面介绍一种方法,非常的简单,因为官方Demo中已经将自定义方法告诉大家了。
废话不多说,直接上一张自定义后的截图
下面介绍用高德地图如果来做,百度地图和Google地图方法类似
1.将官方Demo中得文件拷贝到自己的项目中
CustomeAnnotationView和CustomCalloutView是对应地图中的地标和弹出框
这里简答介绍一下原理,这两个view其实是绑定在一个coordinate(坐标)上的,只是在点击时,才显示calloutView。
2.深入了解这两个view
下面这个是CustomeAnnotationView中得部分代码,也是自定义CalloutView的部分,中间注释部分就是需要自定义的代码段
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{ if (self.selected == selected) { return; } if (selected) { if (self.calloutView == nil) { /* Construct custom callout. */ self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)]; self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x, -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y); } //add view to calloutView将需要添加的label,button等view add到calloutView上。 [self addSubview:self.calloutView];} else { [self.calloutView removeFromSuperview]; } [super setSelected:selected animated:animated];}
该段代码是在调用地图时使用的,是MAMapView的delegate方法
//customer annotation- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{ if ([annotation isKindOfClass:[MAPointAnnotation class]]) { static NSString *customReuseIndetifier = @"customReuseIndetifier"; CustomAnnotationView *annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier]; if (annotationView == nil) { annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:customReuseIndetifier]; // must set to NO, so we can show the custom callout view. annotationView.canShowCallout = NO; //注意这个地方一定要设置成NO,不然就是Callout出系统的calloutview。 annotationView.draggable = YES; annotationView.calloutOffset = CGPointMake(0, -5); } annotationView.portrait = [UIImage imageNamed:@"write_p.png"]; //这个是annotation的image,可以在这里进行设置 } return annotationView; } return nil;}
下面这部分代码基本不需要做改变,它的主要功能是设置calloutView显示位置等。
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view{ /* Adjust the map center in order to show the callout view completely. */ if ([view isKindOfClass:[CustomAnnotationView class]]) { CustomAnnotationView *cusView = (CustomAnnotationView *)view; CGRect frame = [cusView convertRect:cusView.calloutView.frame toView:self.mapView]; frame = UIEdgeInsetsInsetRect(frame, UIEdgeInsetsMake(kCalloutViewMargin, kCalloutViewMargin, kCalloutViewMargin, kCalloutViewMargin)); if (!CGRectContainsRect(self.mapView.frame, frame)) { /* Calculate the offset to make the callout view show up. */ CGSize offset = [self offsetToContainRect:frame inRect:self.mapView.frame]; CGPoint theCenter = self.mapView.center; theCenter = CGPointMake(theCenter.x - offset.width, theCenter.y - offset.height); CLLocationCoordinate2D coordinate = [self.mapView convertPoint:theCenter toCoordinateFromView:self.mapView]; [self.mapView setCenterCoordinate:coordinate animated:YES]; } }}
我在该项目中,只是对背景色进行的修改。
3.小结
本教程非常简单,稍微仔细看看官方的Demo就会知道该如何去做。
该项目中,我还根据每个annotation的不同状态显示不同的image,包括CalloutView里的button处理事件等。
点击这里获取App的源码
如果有任何问题欢迎再下面留言,或者扫描二维码
0 0
- 高德地图(百度地图,Google地图)中自定义Annotation&CallOutView
- ios 一步一步学会自定义地图吹出框(CalloutView)-->(百度地图,高德地图,google地图)
- ios 一步一步学会自定义地图吹出框(CalloutView)-->(百度地图,高德地图,google地图)
- ios 一步一步学会自定义地图吹出框(CalloutView)-->(百度地图,高德地图,google地图)
- ios 一步一步学会自定义地图吹出框(CalloutView)-->(百度地图,高德地图,google地图)
- ios 自定义地图吹出框(CalloutView)-->(百度地图,高德地图,google地图)
- ios 一步一步学会自定义地图吹出框(CalloutView)-->(百度地图,高德地图,google地图)
- ios 百度地图自定义地图弹出框(CalloutView)
- ios 高德地图 默认弹出吹出框(calloutView)
- 高德地图自定义annotation图标
- IOS 地图导航(其中包括苹果地图、百度地图、高德地图、谷歌地图)
- 高德、百度地图自定义底图
- 几个地图(高德、百度、Apple、Google)URL API
- 几个地图(高德、百度、Apple、Google)URL API
- 几个地图(高德、百度、Apple、Google)URL API
- iOS开发——项目中的地图跳转(苹果地图,百度地图,高德地图)
- Android app调用第三方地图(百度地图,高德地图,谷歌地图)导航
- 百度地图定位,高德地图定位
- 我的课堂学习成果
- IOS证书/私钥/代码签名/描述文件
- C++&C编程相关小细节
- java线程及操作实例,线程池简单例子
- 编程:利用循环求和-求1000以内所有偶数的和(用while语句构成循环)
- 高德地图(百度地图,Google地图)中自定义Annotation&CallOutView
- 富可视M310刷机包 MIUIV5 红米开发版 闪光 美化 稳定
- magento根据运送城市设置支付方式
- c#怎么用insert语句把文件的修改日期插到sql数据库的表中
- [USACO 2014 Mar Silver]mooomoo
- 最大公约数(欧几里得算法)
- MySQL得到数据库的大小
- 密码
- 使用nisebosh方式部署cloudfoundry多节点二