ios百度地图标注偏移 能让标注显示在中间吗

iOS-百度地图标注气泡实现 - 简书
下载简书移动应用
写了26344字,被537人关注,获得了427个喜欢
iOS-百度地图标注气泡实现
1.首先实现添加多个标注和自定义气泡
添加自定义标注
[_mapView addAnnotations:array];
arry 中放入标注(BMKPointAnnotation)的数组,此方法添加多个标注。
当添加多个标注时就触发以下代理方法
#pragma mark -- BMKMapdelegate
*根据anntation生成对应的View
*@param mapView 地图View
*@param annotation 指定的标注
*@return 生成的标注View
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id &BMKAnnotation&)annotation
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
newAnnotationView.animatesDrop = YES;
newAnnotationView.annotation =
//这里我根据自己需要,继承了BMKPointAnnotation,添加了标注的类型等需要的信息
MyBMKPointAnnotation *tt = (MyBMKPointAnnotation *)
//判断类别,需要添加不同类别,来赋予不同的标注图片
if (tt.profNumber == 100000) {
newAnnotationView.image = [UIImage imageNamed:@"ic_map_mode_category_merchants_normal.png"];
}else if (tt.profNumber == 100001){
//设定popView的高度,根据是否含有缩略图
double popViewH = 60;
if (annotation.subtitle == nil) {
popViewH = 38;
UIView *popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth-100, popViewH)];
popView.backgroundColor = [UIColor whiteColor];
[popView.layer setMasksToBounds:YES];
[popView.layer setCornerRadius:3.0];
popView.alpha = 0.9;
//自定义气泡的内容,添加子控件在popView上
UILabel *driverName = [[UILabel alloc]initWithFrame:CGRectMake(8, 4, 160, 30)];
driverName.text = annotation.
driverName.numberOfLines = 0;
driverName.backgroundColor = [UIColor clearColor];
driverName.font = [UIFont systemFontOfSize:15];
driverName.textColor = [UIColor blackColor];
driverName.textAlignment = NSTextAlignmentL
[popView addSubview:driverName];
UILabel *carName = [[UILabel alloc]initWithFrame:CGRectMake(8, 30, 180, 30)];
carName.text = annotation.
carName.backgroundColor = [UIColor clearColor];
carName.font = [UIFont systemFontOfSize:11];
carName.textColor = [UIColor lightGrayColor];
carName.textAlignment = NSTextAlignmentL
[popView addSubview:carName];
if (annotation.subtitle != nil) {
UIButton *searchBn = [[UIButton alloc]initWithFrame:CGRectMake(170, 0, 50, 60)];
[searchBn setTitle:@"查看路线" forState:UIControlStateNormal];
searchBn.backgroundColor = mainC
searchBn.titleLabel.numberOfLines = 0;
[searchBn addTarget:self action:@selector(searchLine)];
[popView addSubview:searchBn];
BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView];
pView.frame = CGRectMake(0, 0, ScreenWidth-100, popViewH);
((BMKPinAnnotationView*)newAnnotationView).paopaoView =
((BMKPinAnnotationView*)newAnnotationView).paopaoView = pV
return newAnnotationV
点击标注和气泡响应方法
* 当选中一个annotation views时,调用此接口
* @param mapView 地图View
* @param views 选中的annotation views
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view
_shopCoor = view.annotation.
选中气泡调用方法
@param mapView 地图
@param view
annotation
- (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view
MyBMKPointAnnotation *tt = (MyBMKPointAnnotation *)view.
if (tt.shopID) {
BusinessIfonUVC *BusinessIfonVC = [[BusinessIfonUVC alloc]init];
BusinessIfonVC.shopId = tt.shopID;
[self.navigationController pushViewController:BusinessIfonVC animated:YES];
2.实现路线搜索,路径规划,获取街道名称等功能
通过经纬度获取地址,逆地理编码
-(void)getStartAddress
//起点地址
CLGeocoder *Geocoder = [[CLGeocoder alloc]init];
CLGeocodeCompletionHandler handler = ^(NSArray *place,NSError *error){
for(CLPlacemark *placemark in place){
NSString *tmp = [[NSString alloc]init];
tmp = placemark.subT
if (tmp == nil) {
tmp = @"";
NSString *startAdr = [[NSString alloc]initWithFormat:@"%@%@",placemark.thoroughfare,tmp];
_startCityText.text = placemark.
if ([startAdr isEqualToString:@"(null)"]) {
_startAddrText.text = @"获取地址失败";
_startAddrText.text = startA
CLLocation *loc = [[CLLocation alloc]initWithLatitude:self.startCoor.latitude longitude:self.startCoor.longitude];
[Geocoder reverseGeocodeLocation:loc completionHandler:handler];
路径检索,该部分没有整理,将乘车和换乘信息放到了LineInfo,steps等模型中。
- (void)onGetTransitRouteResult:(BMKRouteSearch*)searcher result:(BMKTransitRouteResult*)result errorCode:(BMKSearchErrorCode)error
NSMutableArray *lineArr = [[NSMutableArray alloc]init];
NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
[_mapView removeAnnotations:array];
array = [NSArray arrayWithArray:_mapView.overlays];
[_mapView removeOverlays:array];
if (error == BMK_SEARCH_NO_ERROR) {
for(int j = 0; j & [result.routes count];j++)
NSMutableArray *busTitleArr = [[NSMutableArray alloc]init];
NSMutableArray *lineStepsArr = [[NSMutableArray alloc]init];
NSMutableArray *stepsArr = [[NSMutableArray alloc]init];
//生成数据模型
LineInfoModel *lineInfo = [[LineInfoModel alloc]init];
BMKTransitRouteLine* plan = (BMKTransitRouteLine*)[result.routes objectAtIndex:j];
//数据模型:获得路线长度
lineInfo.distance = plan.
//数据模型:获得路线消耗时间
lineInfo.dates = plan.duration.
lineInfo.hours = plan.duration.
lineInfo.minutes = plan.duration.
lineInfo.seconds = plan.duration.
// 获得轨迹点
lineInfo.planStepsArr = plan.
// 计算路线方案中的路段数目
int size = [plan.steps count];
int planPointCounts = 0;
for (int i = 0; i & i++) {
BMKTransitStep* transitStep = [plan.steps objectAtIndex:i];
//数据模型:获得乘坐公交数组
DLog(@"%@",transitStep.vehicleInfo.title);
if (transitStep.vehicleInfo.title) {
[busTitleArr addObject:transitStep.vehicleInfo.title];
//数据模型:获取换乘信息
if (transitStep.instruction) {
transitStep.instruction = [transitStep.instruction stringByReplacingOccurrencesOfString:@"&font color=\"#313233\"&" withString:@""];
[lineStepsArr addObject:transitStep.instruction];
DLog(@"%@ %@",transitStep.vehicleInfo.title,transitStep.instruction);
RouteAnnotation* item = [[RouteAnnotation alloc]init];
item.coordinate = plan.starting.
item.title = @"起点";
item.type = 0;
[_mapView addAnnotation:item]; // 添加起点标注
[stepsArr addObject:item];
}else if(i==size-1){
RouteAnnotation* item = [[RouteAnnotation alloc]init];
item.coordinate = plan.terminal.
item.title = @"终点";
item.type = 1;
[stepsArr addObject:item];
[_mapView addAnnotation:item]; // 添加起点标注
RouteAnnotation* item = [[RouteAnnotation alloc]init];
item.coordinate = transitStep.entrace.
item.title = transitStep.
transitStep.instruction = [transitStep.instruction stringByReplacingOccurrencesOfString:@"&font color=\"#313233\"&" withString:@""];
item.type = 3;
[stepsArr addObject:item];
[_mapView addAnnotation:item];
//轨迹点总数累计
planPointCounts += transitStep.pointsC
lineInfo.vehicleInfoArr = busTitleA
lineInfo.lineStepsArr = lineStepsA
lineInfo.stepsArr = stepsA
lineInfo.planPointCounts = planPointC
[lineArr addObject:lineInfo];
BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
int i = 0;
for (int j = 0; j & j++) {
BMKTransitStep* transitStep = [plan.steps objectAtIndex:j];
for(k=0;k&transitStep.pointsCk++) {
temppoints[i].x = transitStep.points[k].x;
temppoints[i].y = transitStep.points[k].y;
self.lineStatusArr = lineA
// 通过points构建BMKPolyline
BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
[_mapView addOverlay:polyLine]; // 添加路线overlay
[_tableView reloadData];
- (void)onGetDrivingRouteResult:(BMKRouteSearch*)searcher result:(BMKDrivingRouteResult*)result errorCode:(BMKSearchErrorCode)error
NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
[_mapView removeAnnotations:array];
array = [NSArray arrayWithArray:_mapView.overlays];
[_mapView removeOverlays:array];
if (error == BMK_SEARCH_NO_ERROR) {
BMKDrivingRouteLine* plan = (BMKDrivingRouteLine*)[result.routes objectAtIndex:0];
// 计算路线方案中的路段数目
int size = [plan.steps count];
int planPointCounts = 0;
for (int i = 0; i & i++) {
BMKDrivingStep* transitStep = [plan.steps objectAtIndex:i];
RouteAnnotation* item = [[RouteAnnotation alloc]init];
item.coordinate = plan.starting.
item.title = @"起点";
item.type = 0;
[_mapView addAnnotation:item]; // 添加起点标注
}else if(i==size-1){
RouteAnnotation* item = [[RouteAnnotation alloc]init];
item.coordinate = plan.terminal.
item.title = @"终点";
item.type = 1;
[_mapView addAnnotation:item]; // 添加起点标注
//添加annotation节点
RouteAnnotation* item = [[RouteAnnotation alloc]init];
item.coordinate = transitStep.entrace.
item.title = transitStep.entraceI
item.degree = transitStep.direction * 30;
item.type = 4;
[_mapView addAnnotation:item];
//轨迹点总数累计
planPointCounts += transitStep.pointsC
// 添加途经点
if (plan.wayPoints) {
for (BMKPlanNode* tempNode in plan.wayPoints) {
RouteAnnotation* item = [[RouteAnnotation alloc]init];
item = [[RouteAnnotation alloc]init];
item.coordinate = tempNode.
item.type = 5;
item.title = tempNode.
[_mapView addAnnotation:item];
BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
int i = 0;
for (int j = 0; j & j++) {
BMKDrivingStep* transitStep = [plan.steps objectAtIndex:j];
for(k=0;k&transitStep.pointsCk++) {
temppoints[i].x = transitStep.points[k].x;
temppoints[i].y = transitStep.points[k].y;
// 通过points构建BMKPolyline
BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
[_mapView addOverlay:polyLine]; // 添加路线overlay
- (void)onGetWalkingRouteResult:(BMKRouteSearch*)searcher result:(BMKWalkingRouteResult*)result errorCode:(BMKSearchErrorCode)error
NSArray* array = [NSArray arrayWithArray:_mapView.annotations];
[_mapView removeAnnotations:array];
array = [NSArray arrayWithArray:_mapView.overlays];
[_mapView removeOverlays:array];
if (error == BMK_SEARCH_NO_ERROR) {
BMKWalkingRouteLine* plan = (BMKWalkingRouteLine*)[result.routes objectAtIndex:0];
int size = [plan.steps count];
int planPointCounts = 0;
for (int i = 0; i & i++) {
BMKWalkingStep* transitStep = [plan.steps objectAtIndex:i];
RouteAnnotation* item = [[RouteAnnotation alloc]init];
item.coordinate = plan.starting.
item.title = @"起点";
item.type = 0;
[_mapView addAnnotation:item]; // 添加起点标注
}else if(i==size-1){
RouteAnnotation* item = [[RouteAnnotation alloc]init];
item.coordinate = plan.terminal.
item.title = @"终点";
item.type = 1;
[_mapView addAnnotation:item]; // 添加起点标注
//添加annotation节点
RouteAnnotation* item = [[RouteAnnotation alloc]init];
item.coordinate = transitStep.entrace.
item.title = transitStep.entraceI
item.degree = transitStep.direction * 30;
item.type = 4;
[_mapView addAnnotation:item];
//轨迹点总数累计
planPointCounts += transitStep.pointsC
BMKMapPoint * temppoints = new BMKMapPoint[planPointCounts];
int i = 0;
for (int j = 0; j & j++) {
BMKWalkingStep* transitStep = [plan.steps objectAtIndex:j];
for(k=0;k&transitStep.pointsCk++) {
temppoints[i].x = transitStep.points[k].x;
temppoints[i].y = transitStep.points[k].y;
// 通过points构建BMKPolyline
BMKPolyline* polyLine = [BMKPolyline polylineWithPoints:temppoints count:planPointCounts];
[_mapView addOverlay:polyLine]; // 添加路线overlay
我这里实现是跳转到另一个控制器中了,下面是他一些需要的数据
//路线长度
@property (nonatomic,assign)
//路线消耗时间
@property (nonatomic,assign)
@property (nonatomic,assign)
@property (nonatomic,assign)
@property (nonatomic,assign)
//交通工具数组
@property (nonatomic,strong) NSArray *vehicleInfoA
//换乘信息
@property (nonatomic,strong) NSArray *lineStepsA
@property (nonatomic,strong) NSArray *stepsA
//轨迹点个数
@property (nonatomic,assign) int planPointC
@property (nonatomic,strong) NSArray *planStepsA
接下来是画路经,关于乘车数据的展示,就是一个tableview上添加了手势,不做解释。
-(void)drawMap
BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
item = [_lineInfo.stepsArr firstObject];
[_mapView setCenterCoordinate:item.coordinate];
[_mapView addAnnotations:_lineInfo.stepsArr];
BMKMapPoint* temppoints = (BMKMapPoint *)malloc(sizeof(CLLocationCoordinate2D) * _lineInfo.planPointCounts);
int i = 0;
for (int j = 0; j & [_lineInfo.planStepsArr count]; j++) {
BMKTransitStep* transitStep = [_lineInfo.planStepsArr objectAtIndex:j];
for(k=0;k&transitStep.pointsCk++) {
temppoints[i].x = transitStep.points[k].x;
temppoints[i].y = transitStep.points[k].y;
BMKPolyline* polyLine =[BMKPolyline
polylineWithPoints:temppoints count:_lineInfo.planPointCounts];
if (nil != polyLine) {
[_mapView addOverlay:polyLine]; // 添加路线overlay
free(temppoints);
- (BMKOverlayView*)mapView:(BMKMapView *)map viewForOverlay:(id&BMKOverlay&)overlay
if ([overlay isKindOfClass:[BMKPolyline class]]) {
BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
polylineView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:1];
polylineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.7];
polylineView.lineWidth = 3.0;
return polylineV
// 判断标注类型,来处理
- (BMKAnnotationView*)getRouteAnnotationView:(BMKMapView *)mapview viewForAnnotation:(MyBMKPointAnnotation*)routeAnnotation
BMKAnnotationView* view =
switch (routeAnnotation.type) {
view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"start_node"];
if (view == nil) {
view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"start_node"];
view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_start.png"]];
view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
view.canShowCallout = TRUE;
view.annotation = routeA
view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"end_node"];
if (view == nil) {
view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"end_node"];
view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_end.png"]];
view.centerOffset = CGPointMake(0, -(view.frame.size.height * 0.5));
view.canShowCallout = TRUE;
view.annotation = routeA
view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"bus_node"];
if (view == nil) {
view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"bus_node"];
view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_bus.png"]];
view.canShowCallout = TRUE;
view.annotation = routeA
view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"rail_node"];
if (view == nil) {
view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"rail_node"];
view.image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_rail.png"]];
view.canShowCallout = TRUE;
view.annotation = routeA
view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"route_node"];
if (view == nil) {
view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"route_node"];
view.canShowCallout = TRUE;
[view setNeedsDisplay];
UIImage* image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_direction.png"]];
view.image = [image imageRotatedByDegrees:routeAnnotation.degree];
view.annotation = routeA
view = [mapview dequeueReusableAnnotationViewWithIdentifier:@"waypoint_node"];
if (view == nil) {
view = [[BMKAnnotationView alloc]initWithAnnotation:routeAnnotation reuseIdentifier:@"waypoint_node"];
view.canShowCallout = TRUE;
[view setNeedsDisplay];
UIImage* image = [UIImage imageWithContentsOfFile:[self getMyBundlePath1:@"images/icon_nav_waypoint.png"]];
view.image = [image imageRotatedByDegrees:routeAnnotation.degree];
view.annotation = routeA
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id &BMKAnnotation&)annotation
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
return [self getRouteAnnotationView:view viewForAnnotation:(MyBMKPointAnnotation *)annotation];
- (UIImage*)imageRotatedByDegrees:(CGFloat)degrees
CGFloat width = CGImageGetWidth(self.CGImage);
CGFloat height = CGImageGetHeight(self.CGImage);
CGSize rotatedS
rotatedSize.width =
rotatedSize.height =
UIGraphicsBeginImageContext(rotatedSize);
CGContextRef bitmap = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2);
CGContextRotateCTM(bitmap, degrees * M_PI / 180);
CGContextRotateCTM(bitmap, M_PI);
CGContextScaleCTM(bitmap, -1.0, 1.0);
CGContextDrawImage(bitmap, CGRectMake(-rotatedSize.width/2, -rotatedSize.height/2, rotatedSize.width, rotatedSize.height), self.CGImage);
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newI
MarkDown不会用,代码弄得很乱,但是经过几天的研究,可以很舒服的上代码了!
进一步交流 QQ群:
如果这篇文章有帮助到你,就请我喝杯茶吧~
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
被以下专题收入,发现更多相似内容:
如果你是程序员,或者有一颗喜欢写程序的心,喜欢分享技术干货、项目经验、程序员日常囧事等等,欢迎投稿《程序员》专题。
投稿须知:
...
· 128584人关注
沟通想法,分享知识,欢迎大家分享一些好的文章。
· 7618人关注
iOS移动开发相关
· 6040人关注
如果这篇文章有帮助到你,就请我喝杯茶吧~
选择支付方式:1044人阅读
理论杂谈(12)
移除一组标注
当前地图的已经添加的标注数组
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:35380次
排名:千里之外
原创:46篇
评论:16条
(1)(1)(1)(1)(1)(1)(1)(2)(1)(3)(2)(1)(3)(5)(13)(15)(3)iOS百度地图-BMK标注&覆盖物
在iOS开发中,地图算是一个比较重要的模块。我们常用的地图有高德地图,百度地图,谷歌地图,对于中国而言,苹果公司已经不再使用谷歌地图,官方使用的是高德地图。下面将讲述一下百度地图开发过程中的一些小的知识点。
对于如何配置百度地图的开发环境,在此不再讲述,具体可以参考:/map/index.php?title=iossdk/guide/buildproject
百度地图iOS的API地址:/map/index.php?title=iossdk/sdkiosdev-download
关于百度地图的基本使用,我们可以参考百度地图的开发文档,在此主要总结一下开发文档中一些重要的知识点和延伸点。(地图版本 SDK 2.9.0)
首先说明一下百度地图开发中可能遇到的问题:
如何添加标注(标注和自定义标注)
1 //添加标记 2 -(void)viewDidAppear:(BOOL)animated 3 { 4
for (int i = 0; i & 3; i++) { 6
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init]; 7
CLLocationCoordinate2D 8
coor.latitude = 39.915 + i*2; 9
coor.longitude = 116.404 + i*2;10
annotation.coordinate =11
annotation.title = @&这里是北京&;12
[myMapView addAnnotation:annotation];13
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];16
CLLocationCoordinate2D17
coor.latitude = 39.915;18
coor.longitude = 116.404;19
annotation.coordinate =20
annotation.title = @&这里是北京&;21
annotation.subtitle = @&1&;22
//[myMapView addAnnotation:annotation];23
BMKPointAnnotation* annotation1 = [[BMKPointAnnotation alloc]init];25
CLLocationCoordinate2D coor1;26
coor1.latitude = 38.915;27
coor1.longitude = 113.404 + 2;28
annotation1.coordinate = coor1;29
annotation1.title = @&这里也是北京&;30
annotation1.subtitle = @&2&;31
//[myMapView addAnnotation:annotation1];32
BMKPointAnnotation* annotation2 = [[BMKPointAnnotation alloc]init];34
CLLocationCoordinate2D coor2;35
coor2.latitude = 38.915;36
coor2.longitude = 119.404 + 2;37
annotation2.coordinate = coor2;38
annotation2.title = @&这里同样是北京&;39
annotation2.subtitle = @&3&;40
//[myMapView addAnnotation:annotation2];41
NSArray *arr = [NSArray arrayWithObjects:annotation,annotation1,annotation2, nil];43
[myMapView addAnnotations:arr];44 }45 46 -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation47 {48
if ([annotation isKindOfClass:[BMKPointAnnotation class]])49
BMKPinAnnotationView *newAnnotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@&AnnotationView&];51
newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@&AnnotationView&];52
newAnnotationView.pinColor = BMKPinAnnotationColorP53
newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示54
return newAnnotationV55
如何自定义大头针
自定义大头针就是改变大头针的样式,如你想使用自己的图片代替上面显示的样式,代码实现跟上面的代码基本一样。只是在上面代理实现的代码中加入下面的代码即可。1newAnnotationView.image = [UIImage imageNamed:@&2&];(图片显示的大小,可以通过newAnnotationView的frame设定)如何修改气泡样式在气泡上默认是可以显示两行信息,一个title,一个subtitle,在此仅修改气泡的样式,如边框大小,字体,等基本信息,没有改变总体布局。
1 -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation 2 { 3
if ([annotation isKindOfClass:[BMKPointAnnotation class]]) 4
BMKPinAnnotationView *newAnnotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@&AnnotationView&]; 6
newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@&AnnotationView&]; 7
newAnnotationView.pinColor = BMKPinAnnotationColorP 8
newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示 9
UIView *view = [[UIView alloc]init];11
view.frame = CGRectMake(0, 0, 100, 60);12
view.backgroundColor = [UIColor greenColor];13
[view.layer setMasksToBounds:YES];14
[view.layer setCornerRadius:3];15
view.alpha = 0.9;16
UILabel *label1 = [[UILabel alloc]init];18
label1.frame = CGRectMake(0, 0, 100, 30);19
label1.text = annotation.20
label1.numberOfLines = 0;21
label1.font = [UIFont systemFontOfSize:12];22
label1.textColor = [UIColor blackColor];23
[view addSubview:label1];24
UILabel *label2 = [[UILabel alloc]init];26
label2.frame = CGRectMake(0, 30, 100, 30);27
label2.text = annotation.28
label2.numberOfLines = 0;29
label2.font = [UIFont systemFontOfSize:10];30
label2.textColor = [UIColor lightGrayColor];31
[view addSubview:label2];32
BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:view];34
pView.frame = CGRectMake(0, 0, 100, 60);35
((BMKPinAnnotationView *)newAnnotationView).paopaoView = pV36
return newAnnotationV37
简单定义气泡样式
如果我们想要修改气泡的布局怎样处理呢?
因为系统默认的一个是坐标,一个标题,一个子标题,我们想要按照自己的方式布局弹出的气泡,我们需要做什么工作呢?
首先系统提供BMKPointAnnotation的方法不够我们使用,我们需要继承这个类,假如新类为myPoint,添加一些新的属性。比如这个类,我们需要添加三个属性。
分别是NSString *imgN NSString *placeN NSString *idN
接下来我们我们需要自定义气泡,气泡本身是一个UIView,我们可以在继承于UIView,创建一个子类myP在myPaopao里面要添加三个控件,显示上面定义的三个属性。
1 #import 2 3 @interface myPaopao : UIView4 @property(nonatomic,retain)UIImageView *imgV5 @property(nonatomic,retain) UILabel *placeN6 @property(nonatomic,retain) UILabel *idN7
myPaopao.h
1 #import &myPaopao.h& 2
3 @implementation myPaopao 4
5 -(instancetype)initWithFrame:(CGRect)frame 6 { 7
self = [super initWithFrame:frame]; 8
if (self) 9
self.frame = CGRectMake(0, 0, 150, 60);11
self.backgroundColor = [UIColor whiteColor];12
_imgView = [[UIImageView alloc]init];14
_imgView.frame = CGRectMake(0, 0, 60, 60);15
[self addSubview:_imgView];16
_placeName = [[UILabel alloc]init];18
_placeName.frame = CGRectMake(60, 0, 90, 30);19
_placeName.font = [UIFont systemFontOfSize:12];20
[self addSubview:_placeName];21
_idNum = [[UILabel alloc]init];23
_idNum.frame = CGRectMake(60, 30, 90, 30);24
[self addSubview:_idNum];25
}2728 }29 @end
myPaopao.m
1 //添加标记 2 -(void)viewDidAppear:(BOOL)animated 3 { 4
myPoint* annotation = [[myPoint alloc]init]; 5
CLLocationCoordinate2D 6
coor.latitude = 39.915; 7
coor.longitude = 116.404; 8
annotation.coordinate = 9
annotation.imgName = @&1.jpg&;10
annotation.placeName = @&这里是北京&;11
annotation.idNum = @&1&;12
[myMapView addAnnotation:annotation];13
myPoint* annotation1 = [[myPoint alloc]init];15
CLLocationCoordinate2D coor1;16
coor1.latitude = 38.915;17
coor1.longitude = 113.404 + 2;18
annotation1.coordinate = coor1;19
annotation1.imgName = @&2.jpg&;20
annotation1.placeName = @&这里也是北京&;21
annotation1.idNum = @&2&;22
[myMapView addAnnotation:annotation1];23 24 }25 -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation26 {27
if ([annotation isKindOfClass:[myPoint class]])28
myPoint *myAnnotation = (myPoint *)30
BMKPinAnnotationView *newAnnotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@&AnnotationView&];32
newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@&AnnotationView&];33
newAnnotationView.pinColor = BMKPinAnnotationColorP34
newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示35
myPaopao *paopapo = [[myPaopao alloc]init];36
paopapo.imgView.image = [UIImage imageNamed:myAnnotation.imgName];38
paopapo.placeName.text = myAnnotation.placeN39
paopapo.idNum.text = myAnnotation.idN40
BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:paopapo];41
((BMKPinAnnotationView *)newAnnotationView).paopaoView = pV42
return newAnnotationV43
如果有需要,我们还可以添加一个按钮,跳转到详情界面,添加按钮的方法,与上面的方法相同,在此
点聚合功能
点聚合功能是v2.9.0新增加的一个功能,如果在一个区域有大量的点,会产生覆盖现象,点聚合功能可以实现将很多点聚合到一个点上,通过缩放比例,可以显示更多点或聚合点。我们在下载SDK的时候,会带有一个Demo,从Demo中我们可以找到相应的实现代码。在开发文档中给出了我们核心代码:
折线(从一位置到另一位置的线段)
1 //添加标记 2 -(void)viewDidAppear:(BOOL)animated 3 { 4
CLLocationCoordinate2D coors[2] = {0}; 5
coors[0].latitude = 39.315; 6
coors[0].longitude = 116.304; 7
coors[1].latitude = 30.515; 8
coors[1].longitude = 116.504; 9
BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coors count:2];10
[myMapView addOverlay:polyline];11 }12 -(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id)overlay13 {14
NSLog(@&sdf&);15
if ([overlay isKindOfClass:[BMKPolyline class]])16
BMKPolylineView *polylineView = [[BMKPolylineView alloc]initWithPolyline:overlay];18
polylineView.strokeColor = [[UIColor greenColor]colorWithAlphaComponent:1];19
polylineView.lineWidth = 5.0;20
return polylineV21
分段纹理分段折线
1 //添加标记 2 -(void)viewDidAppear:(BOOL)animated 3 { 4
CLLocationCoordinate2D coords[5] = {0}; 5
coords[0].latitude = 39.965; 6
coords[0].longitude = 116.404; 7
coords[1].latitude = 39.925; 8
coords[1].longitude = 116.454; 9
coords[2].latitude = 39.955;10
coords[2].longitude = 116.494;11
coords[3].latitude = 39.905;12
coords[3].longitude = 116.654;13
coords[4].latitude = 39.965;14
coords[4].longitude = 116.704;15
//构建分段文理索引数组16
NSArray *textureIndex = [NSArray arrayWithObjects:17
[NSNumber numberWithInt:0],18
[NSNumber numberWithInt:1],19
[NSNumber numberWithInt:2],20
[NSNumber numberWithInt:1], nil];21
BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coords count:5 textureIndex:textureIndex];22
[myMapView addOverlay:polyline];23 }24 -(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id)overlay25 {26
if ([overlay isKindOfClass:[BMKPolyline class]])27
BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];29
polylineView.lineWidth = 5;30
polylineView.isFocus = YES;// 是否分段纹理绘制(突出显示),默认YES31
//加载分段纹理图片,必须否则不能进行分段纹理绘制32
[polylineView loadStrokeTextureImages:33
[NSArray arrayWithObjects:[UIImage imageNamed:@&1.jpg&],34
[UIImage imageNamed:@&2.jpg&],35
[UIImage imageNamed:@&3.jpg&],nil]];36
return polylineV37
分段颜色分段折线
1 -(void)viewDidAppear:(BOOL)animated 2 { 3
CLLocationCoordinate2D coords[5] = {0}; 4
coords[0].latitude = 39.965; 5
coords[0].longitude = 116.404; 6
coords[1].latitude = 39.925; 7
coords[1].longitude = 116.454; 8
coords[2].latitude = 39.955; 9
coords[2].longitude = 116.494;10
coords[3].latitude = 39.905;11
coords[3].longitude = 116.654;12
coords[4].latitude = 39.965;13
coords[4].longitude = 116.704;14
//构建分段文理索引数组15
NSArray *colorIndexs = [NSArray arrayWithObjects:16
[NSNumber numberWithInt:0],17
[NSNumber numberWithInt:1],18
[NSNumber numberWithInt:2],19
[NSNumber numberWithInt:1], nil];20
BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coords count:5 textureIndex:colorIndexs];21
[myMapView addOverlay:polyline];22 }23 -(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id)overlay24 {25
if ([overlay isKindOfClass:[BMKPolyline class]])26
BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];28
polylineView.lineWidth = 5;29
// 使用分段颜色绘制时,必须设置(内容必须为UIColor)30
polylineView.colors = [NSArray arrayWithObjects:[UIColor greenColor], [UIColor redColor], [UIColor yellowColor], nil];31
return polylineV32
弧线(起点,途经点,终点)
1 -(void)viewDidAppear:(BOOL)animated 2 { 3
CLLocationCoordinate2D coords[3] = {0}; 4
coords[0].latitude = 39.9374; 5
coords[0].longitude = 116.350; 6
coords[1].latitude = 39.9170; 7
coords[1].longitude = 116.360; 8
coords[2].latitude = 39.9479; 9
coords[2].longitude = 116.373;10
BMKArcline *arcline = [BMKArcline arclineWithCoordinates:coords];11
[myMapView addOverlay:arcline];12 }13 -(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id)overlay14 {15
if ([overlay isKindOfClass:[BMKArcline class]])16
NSLog(@&adf&);18
BMKArclineView* arclineView = [[BMKArclineView alloc] initWithOverlay:overlay];19
arclineView.strokeColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];20
arclineView.lineWidth = 5.0;21
return arclineV22
1 -(void)viewDidAppear:(BOOL)animated 2 { 3
CLLocationCoordinate2D coords[3] = {0}; 4
coords[0].latitude = 39; 5
coords[0].longitude = 116; 6
coords[1].latitude = 38; 7
coords[1].longitude = 115; 8
coords[2].latitude = 38; 9
coords[2].longitude = 117;10
BMKPolygon *ploygon = [BMKPolygon polygonWithCoordinates:coords count:3];11
[myMapView addOverlay:ploygon];12 }13 -(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id)overlay14 {15
if ([overlay isKindOfClass:[BMKPolygon class]])16
BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay];18
polygonView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];19
polygonView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];20
polygonView.lineWidth = 5.0;21
return polygonV23
1 -(void)viewDidAppear:(BOOL)animated 2 { 3
CLLocationCoordinate2D 4
coor.latitude = 39.915; 5
coor.longitude = 116.404; 6
BMKCircle* circle = [BMKCircle circleWithCenterCoordinate:coor radius:5000]; 7
[myMapView addOverlay:circle]; 8 } 9 -(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id)overlay10 {11
if ([overlay isKindOfClass:[BMKCircle class]])12
BMKCircleView* circleView = [[BMKCircleView alloc] initWithOverlay:overlay];14
circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5];15
circleView.strokeColor = [[UIColor orangeColor] colorWithAlphaComponent:0.5];16
circleView.lineWidth = 10.0;17
return circleV19
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'

我要回帖

更多关于 ios 百度地图标注 的文章

 

随机推荐