一、需求
开发移动地图相关的应用有时会有这样的需求:在地图上显示自己的定位,然后想查看周边使用这个应用的有哪些人。当然完成这个功能需要后台数据的支持,你要把自己的位置信息发给后台,后台在根据你的位置查询数据库返回你周围的用户的信息,这些信息包括经纬度坐标、描述等。这里只描述客户端如何实现,至于后台返回的这些数据就在本地创建家数据了,下面就用百度地图实现这个功能。
二、实现效果展示
三、代码(定位功能上一篇文章已经描述,下面只实现显示一组坐标)
1、创建变量接受协议
@interface BaiduMapViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate> { BMKMapView * _mapView; //地图 BMKLocationService * _locationService; //定位 NSMutableArray * _points;//地理坐标的集合 NSMutableArray * _titles;//标注 } @property (nonatomic,strong) CLLocationManager *locationManager; //iOS8以后定位授权机制的改变,需要手动授权 @end
- (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]; }
#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; }
原文地址:http://blog.csdn.net/always_on_the_way/article/details/41075573