使用百度地图SDK开发一个功能,好多坑。 但是,还是要硬着头皮都解决了这些问题。 首先,百度地图SDK地址:http://lbsyun.baidu.com/ 初步总结: 由于百度地图SDK做的非常好,很多例子都有详细的介绍,所以得好好看demo的。 ============ 特殊需求:删除特定的地图覆盖物 地图上有BMKPolyline,有BMKPolygonView还有BMKGroundOverlayView 所以每次只想删除一种类型的时候,就需要处理了 比如删除 BMKPolyline 类型 for (id zoneP in [_mapView overlays]) { if ([zoneP isKindOfClass:[BMKPolyline class]]) { [_mapView removeOverlay:zoneP]; } } 当然删除所有的覆盖物的时候,使用这个就够了 。。。 [_mapView removeOverlays:_mapView.overlays]; ==============百度提供的demo============= ******************* #import <UIKit/UIKit.h> #import <BaiduMapAPI_Map/BMKMapComponent.h> @interface AnnotationDemoViewController : UIViewController <BMKMapViewDelegate>{ IBOutlet BMKMapView* _mapView; IBOutlet UISegmentedControl* segment; } @end *********************** #define MYBUNDLE_NAME @ "mapapi.bundle" #define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME] #define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH] #import "AnnotationDemoViewController.h" #import "MyAnimatedAnnotationView.h" @interface AnnotationDemoViewController () { BMKCircle* circle; BMKPolygon* polygon; BMKPolygon* polygon2; BMKPolyline* polyline; BMKPolyline* colorfulPolyline; BMKArcline* arcline; BMKGroundOverlay* ground2; BMKPointAnnotation* pointAnnotation; BMKPointAnnotation* animatedAnnotation; } @end @implementation AnnotationDemoViewController // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. - (void)viewDidLoad { [super viewDidLoad]; //适配ios7 if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) { // self.edgesForExtendedLayout=UIRectEdgeNone; self.navigationController.navigationBar.translucent = NO; } //设置地图缩放级别 [_mapView setZoomLevel:11]; //初始化segment segment.selectedSegmentIndex=0; //添加内置覆盖物 [self addOverlayView]; } -(void)viewWillAppear:(BOOL)animated { [_mapView viewWillAppear]; _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放 } -(void)viewWillDisappear:(BOOL)animated { [_mapView viewWillDisappear]; _mapView.delegate = nil; // 不用时,置nil } - (void)dealloc { if (_mapView) { _mapView = nil; } } //segment进行切换 - (IBAction)segmentChanged:(UISegmentedControl *)sender { UISegmentedControl *tempSeg = (UISegmentedControl *)sender; //内置覆盖物 if(tempSeg.selectedSegmentIndex == 0) { [_mapView removeOverlays:_mapView.overlays]; [_mapView removeAnnotations:_mapView.annotations]; //添加内置覆盖物 [self addOverlayView]; return; } //添加标注 else if(tempSeg.selectedSegmentIndex == 1) { [_mapView removeOverlays:_mapView.overlays]; [self addPointAnnotation]; [self addAnimatedAnnotation]; return; } //添加图片图层 else if(tempSeg.selectedSegmentIndex == 2) { [_mapView removeOverlays:_mapView.overlays]; [_mapView removeAnnotations:_mapView.annotations]; [self addGroundOverlay]; } } - (void)addGroundOverlay { //添加图片图层覆盖物 if (ground2 == nil) { CLLocationCoordinate2D coords[2] = {0}; coords[0].latitude = 39.910; coords[0].longitude = 116.370; coords[1].latitude = 39.950; coords[1].longitude = 116.430; BMKCoordinateBounds bound; bound.southWest = coords[0]; bound.northEast = coords[1]; ground2 = [BMKGroundOverlay groundOverlayWithBounds:bound icon:[UIImage imageNamed:@"test.png"]]; ground2.alpha = 0.8; } [_mapView addOverlay:ground2]; // _mapView.zoomLevel = 14; // _mapView.centerCoordinate = CLLocationCoordinate2DMake(39.9255, 116.3995); } //添加内置覆盖物 - (void)addOverlayView { // 添加圆形覆盖物 if (circle == nil) { CLLocationCoordinate2D coor; coor.latitude = 39.915; coor.longitude = 116.404; circle = [BMKCircle circleWithCenterCoordinate:coor radius:5000]; } [_mapView addOverlay:circle]; // 添加多边形覆盖物 if (polygon == nil) { CLLocationCoordinate2D coords[4] = {0}; coords[0].latitude = 39.915; coords[0].longitude = 116.404; coords[1].latitude = 39.815; coords[1].longitude = 116.404; coords[2].latitude = 39.815; coords[2].longitude = 116.504; coords[3].latitude = 39.915; coords[3].longitude = 116.504; polygon = [BMKPolygon polygonWithCoordinates:coords count:4]; } [_mapView addOverlay:polygon]; // 添加多边形覆盖物 if (polygon2 == nil) { CLLocationCoordinate2D coords[5] = {0}; coords[0].latitude = 39.965; coords[0].longitude = 116.604; coords[1].latitude = 39.865; coords[1].longitude = 116.604; coords[2].latitude = 39.865; coords[2].longitude = 116.704; coords[3].latitude = 39.905; coords[3].longitude = 116.654; coords[4].latitude = 39.965; coords[4].longitude = 116.704; polygon2 = [BMKPolygon polygonWithCoordinates:coords count:5]; } [_mapView addOverlay:polygon2]; //添加折线覆盖物 if (polyline == nil) { CLLocationCoordinate2D coors[2] = {0}; coors[1].latitude = 39.895; coors[1].longitude = 116.354; coors[0].latitude = 39.815; coors[0].longitude = 116.304; polyline = [BMKPolyline polylineWithCoordinates:coors count:2]; } [_mapView addOverlay:polyline]; //添加折线(分段颜色绘制)覆盖物 if (colorfulPolyline == nil) { CLLocationCoordinate2D coords[5] = {0}; coords[0].latitude = 39.965; coords[0].longitude = 116.404; coords[1].latitude = 39.925; coords[1].longitude = 116.454; coords[2].latitude = 39.955; coords[2].longitude = 116.494; coords[3].latitude = 39.905; coords[3].longitude = 116.554; coords[4].latitude = 39.965; coords[4].longitude = 116.604; //构建分段颜色索引数组 NSArray *colorIndexs = [NSArray arrayWithObjects: [NSNumber numberWithInt:2], [NSNumber numberWithInt:0], [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil]; //构建BMKPolyline,使用分段颜色索引,其对应的BMKPolylineView必须设置colors属性 colorfulPolyline = [BMKPolyline polylineWithCoordinates:coords count:5 textureIndex:colorIndexs]; } [_mapView addOverlay:colorfulPolyline]; // 添加圆弧覆盖物 if (arcline == nil) { CLLocationCoordinate2D coords[3] = {0}; coords[0].latitude = 40.065; coords[0].longitude = 116.124; coords[1].latitude = 40.125; coords[1].longitude = 116.304; coords[2].latitude = 40.065; coords[2].longitude = 116.404; arcline = [BMKArcline arclineWithCoordinates:coords]; } [_mapView addOverlay:arcline]; } //添加标注 - (void)addPointAnnotation { if (pointAnnotation == nil) { pointAnnotation = [[BMKPointAnnotation alloc]init]; CLLocationCoordinate2D coor; coor.latitude = 39.915; coor.longitude = 116.404; pointAnnotation.coordinate = coor; pointAnnotation.title = @"test"; pointAnnotation.subtitle = @"此Annotation可拖拽!"; } [_mapView addAnnotation:pointAnnotation]; } // 添加动画Annotation - (void)addAnimatedAnnotation { if (animatedAnnotation == nil) { animatedAnnotation = [[BMKPointAnnotation alloc]init]; CLLocationCoordinate2D coor; coor.latitude = 40.115; coor.longitude = 116.404; animatedAnnotation.coordinate = coor; animatedAnnotation.title = @"我是动画Annotation"; } [_mapView addAnnotation:animatedAnnotation]; } #pragma mark - #pragma mark implement BMKMapViewDelegate //根据overlay生成对应的View - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay { if ([overlay isKindOfClass:[BMKCircle class]]) { BMKCircleView* circleView = [[BMKCircleView alloc] initWithOverlay:overlay]; circleView.fillColor = [[UIColor alloc] initWithRed:1 green:0 blue:0 alpha:0.5]; circleView.strokeColor = [[UIColor alloc] initWithRed:0 green:0 blue:1 alpha:0.5]; circleView.lineWidth = 5.0; return circleView; } if ([overlay isKindOfClass:[BMKPolyline class]]) { BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay]; if (overlay == colorfulPolyline) { polylineView.lineWidth = 5; /// 使用分段颜色绘制时,必须设置(内容必须为UIColor) polylineView.colors = [NSArray arrayWithObjects: [[UIColor alloc] initWithRed:0 green:1 blue:0 alpha:1], [[UIColor alloc] initWithRed:1 green:0 blue:0 alpha:1], [[UIColor alloc] initWithRed:1 green:1 blue:0 alpha:0.5], nil]; } else { polylineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:1]; polylineView.lineWidth = 20.0; [polylineView loadStrokeTextureImage:[UIImage imageNamed:@"texture_arrow.png"]]; } return polylineView; } if ([overlay isKindOfClass:[BMKPolygon class]]) { BMKPolygonView* polygonView = [[BMKPolygonView alloc] initWithOverlay:overlay]; polygonView.strokeColor = [[UIColor alloc] initWithRed:0.0 green:0 blue:0.5 alpha:1]; polygonView.fillColor = [[UIColor alloc] initWithRed:0 green:1 blue:1 alpha:0.2]; polygonView.lineWidth =2.0; polygonView.lineDash = (overlay == polygon2); return polygonView; } if ([overlay isKindOfClass:[BMKGroundOverlay class]]) { BMKGroundOverlayView* groundView = [[BMKGroundOverlayView alloc] initWithOverlay:overlay]; return groundView; } if ([overlay isKindOfClass:[BMKArcline class]]) { BMKArclineView *arclineView = [[BMKArclineView alloc] initWithArcline:overlay]; arclineView.strokeColor = [UIColor blueColor]; arclineView.lineDash = YES; arclineView.lineWidth = 6.0; return arclineView; } return nil; } // 根据anntation生成对应的View - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation { //普通annotation if (annotation == pointAnnotation) { NSString *AnnotationViewID = @"renameMark"; BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID]; if (annotationView == nil) { annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID]; // 设置颜色 annotationView.pinColor = BMKPinAnnotationColorPurple; // 从天上掉下效果 annotationView.animatesDrop = YES; // 设置可拖拽 annotationView.draggable = YES; } return annotationView; } //动画annotation NSString *AnnotationViewID = @"AnimatedAnnotation"; MyAnimatedAnnotationView *annotationView = nil; if (annotationView == nil) { annotationView = [[MyAnimatedAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID]; } NSMutableArray *images = [NSMutableArray array]; for (int i = 1; i < 4; i++) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"poi_%d.png", i]]; [images addObject:image]; } annotationView.annotationImages = images; return annotationView; } // 当点击annotation view弹出的泡泡时,调用此接口 - (void)mapView:(BMKMapView *)mapView annotationViewForBubble:(BMKAnnotationView *)view; { NSLog(@"paopaoclick"); } @end |
|