百度地图之标注一组地理坐标<2>
一、需求
开发移动地图相关的应用有时会有这种需求:在地图上显示自己的定位,然后想查看周边使用这个应用的有哪些人。当然完毕这个功能须要后台数据的支持。你要把自己的位置信息发给后台,后台在依据你的位置查询数据库返回你周围的用户的信息,这些信息包含经纬度坐标、描写叙述等。这里仅仅描写叙述client怎样实现。至于后台返回的这些数据就在本地创建家数据了,以下就用百度地图实现这个功能。
二、实现效果展示
三、代码(定位功能上一篇文章已经描写叙述。以下仅仅实现显示一组坐标)
1、创建变量接受协议
@interface BaiduMapViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>
{
BMKMapView * _mapView; //地图
BMKLocationService * _locationService; //定位 NSMutableArray * _points;//地理坐标的集合
NSMutableArray * _titles;//标注
}
@property (nonatomic,strong) CLLocationManager *locationManager; //iOS8以后定位授权机制的改变,须要手动授权
@end
2、创建视图+初始化对应数据
- (void)viewDidLoad {
[super viewDidLoad];
_mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:_mapView];
[_mapView setZoomLevel:14];
//定位
_locationService = [[BMKLocationService alloc]init];
//显示周围
UIButton * showAround = [UIButton buttonWithType:UIButtonTypeCustom];
[showAround setTitle:@"显示周围" forState:UIControlStateNormal];
[showAround setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
showAround.frame = CGRectMake(200, 0, 80, 30);
[showAround addTarget:self action:@selector(showAround) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:showAround];
//创建地理坐标和标注title
CGPoint item1 = CGPointMake(39.915101, 116.403981);
CGPoint item2 = CGPointMake(39.945210, 116.403981);
CGPoint item3 = CGPointMake(39.935301, 116.403991);
CGPoint item4 = CGPointMake(39.925421, 116.403971);
_points = [[NSMutableArray alloc] initWithObjects:NSStringFromCGPoint(item1), NSStringFromCGPoint(item2),NSStringFromCGPoint(item3),NSStringFromCGPoint(item4),nil];
_titles = [[NSMutableArray alloc]initWithObjects:@"天安门",@"神刹海",@"景山公园",@"故宫", nil];
}
3、管理地图的生命周期:自2.0.0起。BMKMapView新增viewWillAppear、viewWillDisappear方法来控制BMKMapView的生命周期。而且在一个时刻仅仅能有一个BMKMapView接受回调消息。因此在使用BMKMapView的viewController中须要在viewWillAppear、viewWillDisappear方法中调用BMKMapView的相应的方法,并处理delegate
#pragma mark - viewWillAppear
-(void)viewWillAppear:(BOOL)animated{
[_mapView viewWillAppear];
_mapView.delegate = self;
_locationService.delegate = self;
} #pragma mark - viewDidAppear
-(void)viewWillDisappear:(BOOL)animated{
[_mapView viewWillDisappear];
_mapView.delegate = nil;
_locationService.delegate = nil; }
4、当点击“显示周边”button的时创建大头针
-(void)showAround{
if (_points.count) {
NSMutableArray * annotations = [[NSMutableArray alloc]init];
for (int i = 0; i < _points.count; i++) {
CGPoint point = CGPointFromString(_points[i]);
CLLocationCoordinate2D pt = (CLLocationCoordinate2D){point.x,point.y};
//创建大头针
BMKPointAnnotation * item = [[BMKPointAnnotation alloc]init];
//设置大头针的坐标
item.coordinate = pt;
//设置大头针的标注
item.title = _titles[i];
[annotations addObject:item];
if(i == 0)
{
//将第一个点的坐标移到屏幕中央
_mapView.centerCoordinate = pt;
}
}
//加入大头针到地图上
[_mapView addAnnotations:annotations];
}
}
#pragma mark 当调用[_mapView addAnnotations:annotations]时回出发地图的代理方法,创建大头针
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation{
NSString * ID = @"annotationViewID";
BMKPinAnnotationView * view = (BMKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:ID];
if (view == nil) {
view = [[BMKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:ID];
view.pinColor = BMKPinAnnotationColorPurple;
view.animatesDrop = YES;
}
view.centerOffset = CGPointMake(0, -(view.frame.size.height*.5));
view.annotation = annotation; //设置代理
view.canShowCallout = TRUE;
return view;
}
关于地图的其它功能待续……
百度地图之标注一组地理坐标<2>的更多相关文章
- php+js实现百度地图多点标注的方法
本文实例讲述了php+js实现百度地图多点标注的方法.分享给大家供大家参考,具体如下: 1.php创建json数据 ? 1 2 $products = $this->product_db-> ...
- iOS 百度地图坐标标注
注:由于iOS9改用更安全的https,为了能够在iOS9中正常使用地图SDK,请在"Info.plist"中进行如下配置,否则影响SDK的使用. <key>NSApp ...
- vue 百度地图多标注展示和点击标注进行的提示
index.html中加入script <script type="text/javascript" src="http://api.map.baidu.com/a ...
- 百度地图api 标注的图标不显示问题
图中郑州PPT设计制作中心前面应该有一个小的标,但是死活就是不显示. 经过百度搜索和测试,终于解决.应该是页面定义的CSS和百度的冲突了,解决办法如下: 在当前页面中,加入 <style typ ...
- 【百度地图API】如何快速创建带有标注的地图?——快速创建地图工具+如何标注商家
原文:[百度地图API]如何快速创建带有标注的地图?--快速创建地图工具+如何标注商家 摘要: 如果你不会程序,如果你不想写代码. 如果你想拥有一张自己的地图,如果你想在该地图上标注出你商店的位置. ...
- 百度地图标注及结合ECharts图谱数据可视化
本示例中根据企业位置经纬度,在页面右侧百度地图中标注企业名称.同时页面左侧ECharts图谱饼状图用于统计企业行业与注册资本.当右侧百度地图缩放拖拽,左侧ECharts图谱根据右侧地图上出现的企业动态 ...
- 【百度地图API】JS版本的常见问题
1.请问如何将我的店铺标注在百度地图上?我是否可以做区域代理?在百度地图上标注是否免费? 答复: 这里只负责API的技术咨询,不解决任何地图标注问题.在百度地图上标注自己公司,即气泡标注业务.该业务已 ...
- 深入浅出百度地图API开发系列(1):前言
百度地图API目前在地图API领域越来越受到众多开发者的关注,许多应用都使用到了百度地图API服务,包括博主me,我自己使用做的是Javascript API,根据经验,我想整理出一份系列教程,如果能 ...
- 【百度地图API】交你如何用百度地图搜索自己的数据!不需数据库!
原文:[百度地图API]交你如何用百度地图搜索自己的数据!不需数据库! 摘要: 我有一定的房产数据,还有银行数据.我想在百度地图上标注出来,并且能搜索到我这些数据. 可是百度的数据库上并没有我的数据. ...
随机推荐
- SpringCloudLearning
http://blog.didispace.com/Spring-Boot%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/ https://github.com/forezp ...
- Spring Cloud(2.0)能力大致列表
微服务九大特性 出自Martin Fowler的<Microservices> 服务组件化 按业务组织团队 做"产品"的态度 智能端点与哑管道 去中心化治理 去中心化管 ...
- Django Rest Framework 教程及API向导
Django Rest Framework 教程及API向导. 一.请求(Request)REST_FRAMEWORK 中的 Request 扩展了标准的HttpRequest,为 REST_FRAM ...
- [密钥权限过大错误]ssh “permissions are too open” error
I had a problem with my mac where I couldn't save any kind of file on the disk anymore. I had to reb ...
- hibernate基础工具findBySQL学习
public List<Map<String,Object>> findBySQL(String sql,Map<String,Object> param,int ...
- iOS学习笔记34-加速计和陀螺仪
一.CoreMotion框架介绍 我们知道有一些iOS的应用,会有一些特殊的要求,比如: 电子罗盘指南针之类的应用:让我们知道方向. 运动类型软件:让我们知道我们跑步多少公里. 社交软件中的摇一摇功能 ...
- 【bzoj3751】[NOIP2014]解方程 数论
题目描述 已知多项式方程: a0+a1*x+a2*x^2+...+an*x^n=0 求这个方程在[1,m]内的整数解(n和m均为正整数). 输入 第一行包含2个整数n.m,每两个整数之间用一个空格隔开 ...
- BZOJ 4554 [Tjoi2016&Heoi2016]游戏 ——二分图
出原题,直接二分图匹配即可. #include <cstdio> #include <vector> #include <cstring> using namesp ...
- [图论训练]1143: [CTSC2008]祭祀river 二分图匹配
Description 在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在 水面上,奉龙王为神.每逢重大庆典, Y族都会在水面上举办盛大的祭祀活动.我们可以把Y族居住地水系看成一个由岔口和河道组 ...
- 【BJOI2014/bzoj4530】大融合
题意 有 $n$ 个点,初始没有连边,要求支持两个动态操作: 1. 加一条边(保证之前两点不连通) 2. 查询过一条边的简单路径数量(就是两边连通块的大小的乘积) $n,Q\le 100000$ 题解 ...