百度地图之标注一组地理坐标<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;
}


关于地图的其它功能待续……
posted @ 2017-05-12 13:43  yfceshi  阅读(430)  评论(0编辑  收藏  举报