iOS-BMK标注&覆盖物
在iOS开发中,地图算是一个比较重要的模块。我们常用的地图有高德地图,百度地图,谷歌地图,对于中国而言,苹果公司已经不再使用谷歌地图,官方使用的是高德地图。下面将讲述一下百度地图开发过程中的一些小的知识点。
对于如何配置百度地图的开发环境,在此不再讲述,具体可以参考:http://developer.baidu.com/map/index.php?title=iossdk/guide/buildproject
百度地图iOS的API下载地址:http://developer.baidu.com/map/index.php?title=iossdk/sdkiosdev-download
关于百度地图的基本使用,我们可以参考百度地图的开发文档,在此主要总结一下开发文档中一些重要的知识点和延伸点。(地图版本IOS SDK 2.9.0)
首先说明一下百度地图开发中可能遇到的问题:
如何添加标注(系统标注和自定义标注)
//添加标记
-(void)viewDidAppear:(BOOL)animated
{
/*
for (int i = 0; i < 3; i++) {
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor;
coor.latitude = 39.915 + i*2;
coor.longitude = 116.404 + i*2;
annotation.coordinate = coor;
annotation.title = @"这里是北京";
[myMapView addAnnotation:annotation];
}
*/
BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor;
coor.latitude = 39.915;
coor.longitude = 116.404;
annotation.coordinate = coor;
annotation.title = @"这里是北京";
annotation.subtitle = @"";
//[myMapView addAnnotation:annotation]; BMKPointAnnotation* annotation1 = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor1;
coor1.latitude = 38.915;
coor1.longitude = 113.404 + ;
annotation1.coordinate = coor1;
annotation1.title = @"这里也是北京";
annotation1.subtitle = @"";
//[myMapView addAnnotation:annotation1]; BMKPointAnnotation* annotation2 = [[BMKPointAnnotation alloc]init];
CLLocationCoordinate2D coor2;
coor2.latitude = 38.915;
coor2.longitude = 119.404 + ;
annotation2.coordinate = coor2;
annotation2.title = @"这里同样是北京";
annotation2.subtitle = @"";
//[myMapView addAnnotation:annotation2]; NSArray *arr = [NSArray arrayWithObjects:annotation,annotation1,annotation2, nil];
[myMapView addAnnotations:arr];
} -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]])
{
BMKPinAnnotationView *newAnnotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationView"];
newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AnnotationView"];
newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
return newAnnotationView;
}
return nil;
}
系统标记


-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[BMKPointAnnotation class]])
{
BMKPinAnnotationView *newAnnotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationView"];
newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AnnotationView"];
newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示 UIView *view = [[UIView alloc]init];
view.frame = CGRectMake(, , , );
view.backgroundColor = [UIColor greenColor];
[view.layer setMasksToBounds:YES];
[view.layer setCornerRadius:];
view.alpha = 0.9; UILabel *label1 = [[UILabel alloc]init];
label1.frame = CGRectMake(, , , );
label1.text = annotation.title;
label1.numberOfLines = ;
label1.font = [UIFont systemFontOfSize:];
label1.textColor = [UIColor blackColor];
[view addSubview:label1]; UILabel *label2 = [[UILabel alloc]init];
label2.frame = CGRectMake(, , , );
label2.text = annotation.subtitle;
label2.numberOfLines = ;
label2.font = [UIFont systemFontOfSize:];
label2.textColor = [UIColor lightGrayColor];
[view addSubview:label2]; BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:view];
pView.frame = CGRectMake(, , , );
((BMKPinAnnotationView *)newAnnotationView).paopaoView = pView;
return newAnnotationView;
}
return nil;
}
简单定义气泡样式
如果我们想要修改气泡的布局怎样处理呢?
因为系统默认的一个是坐标,一个标题,一个子标题,我们想要按照自己的方式布局弹出的气泡,我们需要做什么工作呢?
首先系统提供BMKPointAnnotation的方法不够我们使用,我们需要继承这个类,假如新类为myPoint,添加一些新的属性。比如这个类,我们需要添加三个属性。
分别是NSString *imgName ; NSString *placeName; NSString *idNum;
接下来我们我们需要自定义气泡,气泡本身是一个UIView,我们可以在继承于UIView,创建一个子类myPaopao;在myPaopao里面要添加三个控件,显示上面定义的三个属性。
#import <UIKit/UIKit.h> @interface myPaopao : UIView
@property(nonatomic,retain)UIImageView *imgView;
@property(nonatomic,retain) UILabel *placeName;
@property(nonatomic,retain) UILabel *idNum; @end
myPaopao.h
#import "myPaopao.h" @implementation myPaopao -(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
self.frame = CGRectMake(, , , );
self.backgroundColor = [UIColor whiteColor]; _imgView = [[UIImageView alloc]init];
_imgView.frame = CGRectMake(, , , );
[self addSubview:_imgView]; _placeName = [[UILabel alloc]init];
_placeName.frame = CGRectMake(, , , );
_placeName.font = [UIFont systemFontOfSize:];
[self addSubview:_placeName]; _idNum = [[UILabel alloc]init];
_idNum.frame = CGRectMake(, , , );
[self addSubview:_idNum]; }
return self;
}
@end
myPaopao.m
//添加标记
-(void)viewDidAppear:(BOOL)animated
{
myPoint* annotation = [[myPoint alloc]init];
CLLocationCoordinate2D coor;
coor.latitude = 39.915;
coor.longitude = 116.404;
annotation.coordinate = coor;
annotation.imgName = @"1.jpg";
annotation.placeName = @"这里是北京";
annotation.idNum = @"";
[myMapView addAnnotation:annotation]; myPoint* annotation1 = [[myPoint alloc]init];
CLLocationCoordinate2D coor1;
coor1.latitude = 38.915;
coor1.longitude = 113.404 + ;
annotation1.coordinate = coor1;
annotation1.imgName = @"2.jpg";
annotation1.placeName = @"这里也是北京";
annotation1.idNum = @"";
[myMapView addAnnotation:annotation1]; }
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
if ([annotation isKindOfClass:[myPoint class]])
{
myPoint *myAnnotation = (myPoint *)annotation; BMKPinAnnotationView *newAnnotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationView"];
newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AnnotationView"];
newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
myPaopao *paopapo = [[myPaopao alloc]init]; paopapo.imgView.image = [UIImage imageNamed:myAnnotation.imgName];
paopapo.placeName.text = myAnnotation.placeName;
paopapo.idNum.text = myAnnotation.idNum;
BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:paopapo];
((BMKPinAnnotationView *)newAnnotationView).paopaoView = pView;
return newAnnotationView;
}
return nil;
}
实现代码
如果有需要,我们还可以添加一个按钮,跳转到详情界面,添加按钮的方法,与上面的方法相同,在此
点聚合功能
点聚合功能是v2.9.0新增加的一个功能,如果在一个区域有大量的点,会产生覆盖现象,点聚合功能可以实现将很多点聚合到一个点上,通过缩放比例,可以显示更多点或聚合点。我们在下载SDK的时候,会带有一个Demo,从Demo中我们可以找到相应的实现代码。在开发文档中给出了我们核心代码:

//添加标记
-(void)viewDidAppear:(BOOL)animated
{
CLLocationCoordinate2D coors[] = {};
coors[].latitude = 39.315;
coors[].longitude = 116.304;
coors[].latitude = 30.515;
coors[].longitude = 116.504;
BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coors count:];
[myMapView addOverlay:polyline];
}
-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay
{
NSLog(@"sdf");
if ([overlay isKindOfClass:[BMKPolyline class]])
{
BMKPolylineView *polylineView = [[BMKPolylineView alloc]initWithPolyline:overlay];
polylineView.strokeColor = [[UIColor greenColor]colorWithAlphaComponent:];
polylineView.lineWidth = 5.0;
return polylineView;
}
return nil;
}
折线
分段纹理分段折线

//添加标记
-(void)viewDidAppear:(BOOL)animated
{
CLLocationCoordinate2D coords[] = {};
coords[].latitude = 39.965;
coords[].longitude = 116.404;
coords[].latitude = 39.925;
coords[].longitude = 116.454;
coords[].latitude = 39.955;
coords[].longitude = 116.494;
coords[].latitude = 39.905;
coords[].longitude = 116.654;
coords[].latitude = 39.965;
coords[].longitude = 116.704;
//构建分段文理索引数组
NSArray *textureIndex = [NSArray arrayWithObjects:
[NSNumber numberWithInt:],
[NSNumber numberWithInt:],
[NSNumber numberWithInt:],
[NSNumber numberWithInt:], nil];
BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coords count: textureIndex:textureIndex];
[myMapView addOverlay:polyline];
}
-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay
{
if ([overlay isKindOfClass:[BMKPolyline class]])
{
BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
polylineView.lineWidth = ;
polylineView.isFocus = YES;// 是否分段纹理绘制(突出显示),默认YES
//加载分段纹理图片,必须否则不能进行分段纹理绘制
[polylineView loadStrokeTextureImages:
[NSArray arrayWithObjects:[UIImage imageNamed:@"1.jpg"],
[UIImage imageNamed:@"2.jpg"],
[UIImage imageNamed:@"3.jpg"],nil]];
return polylineView;
}
return nil;
}
纹理折线
分段颜色分段折线
代码段:
-(void)viewDidAppear:(BOOL)animated
{
CLLocationCoordinate2D coords[] = {};
coords[].latitude = 39.965;
coords[].longitude = 116.404;
coords[].latitude = 39.925;
coords[].longitude = 116.454;
coords[].latitude = 39.955;
coords[].longitude = 116.494;
coords[].latitude = 39.905;
coords[].longitude = 116.654;
coords[].latitude = 39.965;
coords[].longitude = 116.704;
//构建分段文理索引数组
NSArray *colorIndexs = [NSArray arrayWithObjects:
[NSNumber numberWithInt:],
[NSNumber numberWithInt:],
[NSNumber numberWithInt:],
[NSNumber numberWithInt:], nil];
BMKPolyline *polyline = [BMKPolyline polylineWithCoordinates:coords count: textureIndex:colorIndexs];
[myMapView addOverlay:polyline];
}
-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay
{
if ([overlay isKindOfClass:[BMKPolyline class]])
{
BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];
polylineView.lineWidth = ;
// 使用分段颜色绘制时,必须设置(内容必须为UIColor)
polylineView.colors = [NSArray arrayWithObjects:[UIColor greenColor], [UIColor redColor], [UIColor yellowColor], nil];
return polylineView;
}
return nil;
}
颜色分段
弧线(起点,途经点,终点)
代码段:
-(void)viewDidAppear:(BOOL)animated
{
CLLocationCoordinate2D coords[] = {};
coords[].latitude = 39.9374;
coords[].longitude = 116.350;
coords[].latitude = 39.9170;
coords[].longitude = 116.360;
coords[].latitude = 39.9479;
coords[].longitude = 116.373;
BMKArcline *arcline = [BMKArcline arclineWithCoordinates:coords];
[myMapView addOverlay:arcline];
}
-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay
{
if ([overlay isKindOfClass:[BMKArcline class]])
{
NSLog(@"adf");
BMKArclineView* arclineView = [[BMKArclineView alloc] initWithOverlay:overlay];
arclineView.strokeColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
arclineView.lineWidth = 5.0;
return arclineView;
}
return nil;
}
弧线
多边形
代码段:
-(void)viewDidAppear:(BOOL)animated
{
CLLocationCoordinate2D coords[] = {};
coords[].latitude = ;
coords[].longitude = ;
coords[].latitude = ;
coords[].longitude = ;
coords[].latitude = ;
coords[].longitude = ;
BMKPolygon *ploygon = [BMKPolygon polygonWithCoordinates:coords count:];
[myMapView addOverlay:ploygon];
}
-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay
{
if ([overlay isKindOfClass:[BMKPolygon class]])
{
BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay];
polygonView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:];
polygonView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
polygonView.lineWidth = 5.0; return polygonView;
}
return nil;
}
多边形
圆
代码段:
-(void)viewDidAppear:(BOOL)animated
{
CLLocationCoordinate2D coor;
coor.latitude = 39.915;
coor.longitude = 116.404;
BMKCircle* circle = [BMKCircle circleWithCenterCoordinate:coor radius:];
[myMapView addOverlay:circle];
}
-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay
{
if ([overlay isKindOfClass:[BMKCircle class]])
{
BMKCircleView* circleView = [[BMKCircleView alloc] initWithOverlay:overlay];
circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5];
circleView.strokeColor = [[UIColor orangeColor] colorWithAlphaComponent:0.5];
circleView.lineWidth = 10.0; return circleView;
}
return nil;
}
圆
图片图层
第一种方法根据坐标,缩放尺度确定
代码段:
-(void)viewDidAppear:(BOOL)animated
{
CLLocationCoordinate2D coors;
coors.latitude = 39.800;
coors.longitude = 116.404;
BMKGroundOverlay* ground = [BMKGroundOverlay groundOverlayWithPosition:coors
zoomLevel: anchor:CGPointMake(0.0f,0.0f)
icon:[UIImage imageNamed:@"1.jpg"]];
[myMapView addOverlay:ground];
}
-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay
{
if ([overlay isKindOfClass:[BMKGroundOverlay class]]){
BMKGroundOverlayView* groundView = [[BMKGroundOverlayView alloc] initWithOverlay:overlay];
return groundView;
}
return nil;
}
图片图层一
第二种方法根据指定区域生成
代码段:
-(void)viewDidAppear:(BOOL)animated
{
CLLocationCoordinate2D coords[] = {};
coords[].latitude = 39.815;
coords[].longitude = 116.404;
coords[].latitude = 39.915;
coords[].longitude = 116.504;
BMKCoordinateBounds bound;
bound.southWest = coords[];
bound.northEast = coords[];
BMKGroundOverlay* ground = [BMKGroundOverlay groundOverlayWithBounds: bound
icon:[UIImage imageNamed:@"2.jpg"]];
[myMapView addOverlay:ground];
}
-(BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay
{
if ([overlay isKindOfClass:[BMKGroundOverlay class]]){
BMKGroundOverlayView* groundView = [[BMKGroundOverlayView alloc] initWithOverlay:overlay];
return groundView;
}
return nil;
}
图片图层二
iOS-BMK标注&覆盖物的更多相关文章
- Android应用之——百度地图最新SDK3.0应用,实现最经常使用的标注覆盖物以及弹出窗覆盖物
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/yanglfree/article/details/33333413 一.概述 最新版的百度地图SDK ...
- iOS设计标注处理方法
如果设计只给3x的设计图 在做2x适配时有几种处理方法: 按逻辑像素,大小不变,比如3x手机上一张图的逻辑像素设为24x24point,那么2x手机上这张图的大小也设为24x24point,一般适用于 ...
- 学习笔记:APP切图那点事儿–详细介绍android和ios平台
学习笔记:APP切图那点事儿–详细介绍android和ios平台 转载自:http://www.woofeng.cn/articles/168.html 版权归原作者所有 作者:亚茹有李 原文地址 ...
- Android百度地图开发02之添加覆盖物 + 地理编码和反地理编码
下面来看一下地图上覆盖物的添加,以及地理编码和反地理编码. 添加覆盖物 在地图上添加覆盖物,一般需要以下几个步骤: 1. 定义坐标点,有可能是一个,有可能是多个(比如:多边形覆盖物). 2. 构造Ov ...
- 【Android】3.9 覆盖物功能
分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 百度地图SDK所提供的地图等级为3-19级(3.7.1版本中有些部分已经提供到了21级),所包含的信 ...
- [书目20160624]Android应用开发从入门到精通
卢海东 著 第1章 揭开神秘面纱——Android系统简介 1 1.1 认识Android系统 2 1.1.1 Android成长历程 2 1.1.2 发行版本 3 1.1.3 得到大 ...
- 基础地图Android SDK
开发者可利用SDK提供的接口,使用百度为您提供的基础地图数据.目前百度地图SDK所提供的地图等级为3-21级,所包含的信息有建筑物.道路.河流.学校.公园等内容. V3.7.0起,地图支持缩放至21级 ...
- <Android 应用 之路> 百度地图API使用(3)
前言 上一篇讲解并实践了百度地图基本显示,地图类型,实时交通图,热力图,地图控制和手势控制,今天看下在地图上添加view和覆盖物. 地图Logo不允许遮挡,可通过mBaiduMap.setPaddin ...
- iOS原生地图开发指南续——大头针与自定义标注
iOS原生地图开发指南续——大头针与自定义标注 出自:http://www.sxt.cn/info-6042-u-7372.html 在上一篇博客中http://my.oschina.net/u/23 ...
随机推荐
- 《毛毛虫组》【Alpha】Scrum meeting 5
第二天 日期:2019/6/18 1.1 今日完成任务情况以及遇到的问题. 今日完成任务情况: 出入库货物年统计模块设计及系统的测试运行: (1)对数据库表--tb_InStore和tb_OutSto ...
- 如何通过修改文件添加用户到sudoers上
su - root chmod u+w /etc/sudoers (该文件没有写权限, 修改)vim /etc/sudoers 按下 I 键进行编写 # User privilege speci ...
- Java的WatchService文件夹监听遇到的一些问题
打开word文档时会新增一个~$开头的同名文件,关闭时该文件自动删除 修改excel文件时,会新增一个文件名像E56B4610,CBC15610等这样的文件,同时也会产生tmp格式的文件 PPT文件修 ...
- JQ之$.ajax()方法以及ajax跨域请求
AJAX(Asynchronous javascript AND xml :异步javascript和xml):是一种创建交互式网页应用的网页开发技术.AJAX可以在不重新加载整个页面的情况下与服务器 ...
- GoogleTest 之路3-Mocking Framework
当你写一个原型或者测试的时候,依赖整个object 是不可行和明智的.一个 mock object和 real object 有同样的接口(所以它可以像同一个使用),但是让你在运行时进行指定它应该如何 ...
- 微软与百度合作:win10搜索引擎默认百度
全球最大的中文搜索引擎百度公司与微软公司共同宣布双方展开战略合作.百度并将成为中国市场上Windows 10 Microsoft Edge浏览器的默认主页和搜索引擎.也就是说,将来人们在win10的M ...
- oop中 限制文件类型和大小
<?php /** * Created by IntelliJ IDEA. * User: jiabinwang * Date: 7/5/18 * Time: 8:46 PM */ namesp ...
- python 取余运算
python中取余运算逻辑如下: 如果a 与d 是整数,d 非零,那么余数 r 满足这样的关系: a = qd + r , q 为整数,且0 ≤ |r| < |d|. 经过测试可发现,pytho ...
- hdu-2553 N皇后问题(搜索题)
在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求出有多少种合法的放置方法. Inpu ...
- Mr. Panda and Crystal HDU - 6007 最短路+完全背包
题目:题目链接 思路:不难看出,合成每个宝石需要消耗一定的魔力值,每个宝石有一定的收益,所以只要我们知道每个宝石合成的最小花费,该题就可以转化为一个背包容量为初始魔力值的完全背包问题,每个宝石的最小花 ...