码迷,mamicode.com
首页 > 其他好文 > 详细

百度地图之标注一组地理坐标<2>

时间:2014-11-13 12:57:52      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:百度地图   添加一组标注   地图   

一、需求

开发移动地图相关的应用有时会有这样的需求:在地图上显示自己的定位,然后想查看周边使用这个应用的有哪些人。当然完成这个功能需要后台数据的支持,你要把自己的位置信息发给后台,后台在根据你的位置查询数据库返回你周围的用户的信息,这些信息包括经纬度坐标、描述等。这里只描述客户端如何实现,至于后台返回的这些数据就在本地创建家数据了,下面就用百度地图实现这个功能。


二、实现效果展示

bubuko.com,布布扣


三、代码(定位功能上一篇文章已经描述,下面只实现显示一组坐标)

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、当点击“显示周边”按钮的时创建大头针

-(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>

标签:百度地图   添加一组标注   地图   

原文地址:http://blog.csdn.net/always_on_the_way/article/details/41075573

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!