有关基本地图的实现参考 【iOS百度地图系列_0】百度地图的配置及实现基本地图
//切换为卫星图 [_mapView setMapType:BMKMapTypeSatellite];
由卫星图切换为普通矢量图的核心代码如下:
//切换为普通地图 [_mapView setMapType:BMKMapTypeStandard];
开启实时路况的核心代码如下:
//打开实时路况图层 [_mapView setTrafficEnabled:YES];
关闭实时路况的核心代码如下:
//关闭实时路况图层 [_mapView setTrafficEnabled:NO];
//打开百度城市热力图图层(百度自有数据) [_mapView setBaiduHeatMapEnabled:YES]; //关闭百度城市热力图图层(百度自有数据) [_mapView setBaiduHeatMapEnabled:NO];
- (void) viewDidAppear:(BOOL)animated { // 添加一个PointAnnotation BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init]; CLLocationCoordinate2D coor; coor.latitude = 39.915; coor.longitude = 116.404; annotation.coordinate = coor; annotation.title = @"这里是北京"; [_mapView addAnnotation:annotation]; } // Override - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation { if ([annotation isKindOfClass:[BMKPointAnnotation class]]) { BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"]; newAnnotationView.pinColor = BMKPinAnnotationColorPurple; newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示 return newAnnotationView; } return nil; }
- (void)viewDidLoad { [super viewDidLoad]; // 添加折线覆盖物 CLLocationCoordinate2D coors[2] = {0}; coors[0].latitude = 39.315; coors[0].longitude = 116.304; coors[1].latitude = 39.515; coors[1].longitude = 116.504; BMKPolyline* polyline = [BMKPolyline polylineWithCoordinates:coors count:2]; [_mapView addOverlay:polyline]; } // Override - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{ if ([overlay isKindOfClass:[BMKPolyline class]]){ BMKPolylineView* polylineView = [[[BMKPolylineView alloc] initWithOverlay:overlay] autorelease]; polylineView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1]; polylineView.lineWidth = 5.0; return polylineView; } return nil; }
- (void)viewDidLoad { [super viewDidLoad]; //添加弧线覆盖物 //传入的坐标顺序为起点、途经点、终点 CLLocationCoordinate2D coords[3] = {0}; coords[0].latitude = 39.9374; coords[0].longitude = 116.350; coords[1].latitude = 39.9170; coords[1].longitude = 116.360; coords[2].latitude = 39.9479; coords[2].longitude = 116.373; BMKArcline *arcline = [BMKArcline arclineWithCoordinates:coords]; [_mapView addOverlay:arcline]; } //根据overlay生成对应的View - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<bmkoverlay>)overlay { if ([overlay isKindOfClass:[BMKArcline class]]) { BMKArclineView* arclineView = [[[BMKArclineView alloc] initWithOverlay:overlay] autorelease]; arclineView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5]; arclineView.lineWidth = 5.0; return arclineView; } return nil; }
- (void)viewDidLoad { [super viewDidLoad]; // 添加多边形覆盖物 CLLocationCoordinate2D coords[3] = {0}; coords[0].latitude = 39; coords[0].longitude = 116; coords[1].latitude = 38; coords[1].longitude = 115; coords[2].latitude = 38; coords[2].longitude = 117; BMKPolygon* polygon = [BMKPolygon polygonWithCoordinates:coords count:3]; [_mapView addOverlay:polygon]; } // Override - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{ if ([overlay isKindOfClass:[BMKPolygon class]]){ BMKPolygonView* polygonView = [[[BMKPolygonView alloc] initWithOverlay:overlay] autorelease]; polygonView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1]; polygonView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2]; polygonView.lineWidth = 5.0; return polygonView; } return nil; }
- (void)viewDidLoad { [super viewDidLoad]; // 添加圆形覆盖物 CLLocationCoordinate2D coor; coor.latitude = 39.915; coor.longitude = 116.404; BMKCircle* circle = [BMKCircle circleWithCenterCoordinate:coor radius:5000]; [_mapView addOverlay:circle]; } // Override - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{ if ([overlay isKindOfClass:[BMKCircle class]]){ BMKCircleView* circleView = [[[BMKCircleView alloc] initWithOverlay:overlay] autorelease]; circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5]; circleView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5]; circleView.lineWidth = 10.0; return circleView; } return nil; }
- (void)viewDidLoad { [super viewDidLoad]; //添加图片图层覆盖物(第一种:根据指定经纬度坐标生成) CLLocationCoordinate2D coors; coors.latitude = 39.800; coors.longitude = 116.404; BMKGroundOverlay* ground = [BMKGroundOverlay groundOverlayWithPosition:coors zoomLevel:11 anchor:CGPointMake(0.0f,0.0f) icon:[UIImage imageWithName:@"test.png"]]; [_mapView addOverlay:ground]; //添加图片图层覆盖物(第二种:根据指定区域生成) CLLocationCoordinate2Dcoords[2] = {0}; coords[0].latitude = 39.815; coords[0].longitude = 116.404; coords[1].latitude = 39.915; coords[1].longitude = 116.504; BMKCoordinateBounds bound; bound.southWest = coords[0]; bound.northEast = coords[1]; BMKGroundOverlay* ground2 = [BMKGroundOverlay groundOverlayWithBounds: bound icon:[UIImage imageWithName:@"test.png"]]; [_mapView addOverlay:ground2]; }
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay{ if ([overlay isKindOfClass:[BMKGroundOverlay class]]){ BMKGroundOverlayView* groundView = [[[BMKGroundOverlayView alloc] initWithOverlay:overlay] autorelease]; return groundView; } return nil; }
热力图是用不同颜色的区块叠加在地图上描述人群分布、密度和变化趋势的一个产品,百度地图SDK将绘制热力图的能力为广大开发者开放,帮助开发者利用自有数据,构建属于自己的热力图,提供丰富的展示效果。
注意:此处的“热力图功能”不同于“百度城市热力图”。百度城市热力图通过简单的接口调用,开发者可展示百度数据的热力图层。而此处的热力图功能,需要开发者传入自己的位置数据,然后SDK会根据热力图绘制规则为开发者做本地的热力图渲染绘制。
利用热力图功能构建自有数据热力图的方式如下:
//添加热力图 -(void)addHeatMap{ //创建热力图数据类 BMKHeatMap* heatMap = [[BMKHeatMapalloc]init]; //创建渐变色类 UIColor* color1 = [UIColorblueColor]; UIColor* color2 = [UIColoryellowColor]; UIColor* color3 = [UIColorredColor]; NSArray*colorInitialArray = [[NSArrayalloc]initWithObjects:color1,color2,color3, nil]; BMKGradient* gradient = [[BMKGradientalloc]initWithColors:colorInitialArraystartPoints:@[@"0.08f", @"0.4f", @"1f"]]; [colorInitialArrayrelease]; //如果用户自定义了渐变色则按自定义的渐变色进行绘制否则按默认渐变色进行绘制 heatMap.mGradient = gradient; //创建热力图数据数组 NSMutableArray* data = [NSMutableArrayarray]; int num = 1000; for(int i = 0; i<num; i++) { //创建BMKHeatMapNode BMKHeatMapNode* heapmapnode_test = [[BMKHeatMapNodealloc]init]; //此处示例为随机生成的坐标点序列,开发者使用自有数据即可 CLLocationCoordinate2D coor; float random = (arc4random()%1000)*0.001; float random2 = (arc4random()%1000)*0.003; float random3 = (arc4random()%1000)*0.015; float random4 = (arc4random()%1000)*0.016; if(i%2==0){ coor.latitude = 39.915+random; coor.longitude = 116.403+random2; }else{ coor.latitude = 39.915-random3; coor.longitude = 116.403-random4; } heapmapnode_test.pt = coor; //随机生成点强度 heapmapnode_test.intensity = arc4random()*900; //添加BMKHeatMapNode到数组 [data addObject:heapmapnode_test]; [heapmapnode_test release]; } //将点数据赋值到热力图数据类 heatMap.mData = data; //调用mapView中的方法根据热力图数据添加热力图 [_mapView addHeatMap:heatMap]; } //删除热力图 -(void)removeHeatMap{ [_mapView removeHeatMap]; }
从2.0.0开始,地图渲染采用OpenGL方式实现,因此覆盖物基类BMKOverlayView新增glRender接口,以及绘制基本线renderLinesWithPoints、面renderRegionWithPoints的接口来实现对覆盖物的OpenGL渲染。绘制自定义overlay时,继承BMKOverlayView的子类需实现glRender接口,在glRender中通过调用renderLinesWithPoints、renderRegionWithPoints来组合自己想要实现的图形。
CustomOverlayView继承BMKOverlayPathView,在CustomOverlayView中实现glRender。核心代码如下所示:
- (void)glRender { //自定义overlay绘制 CustomOverlay *customOverlay = [self customOverlay]; if (customOverlay.pointCount >= 3) { [self renderRegionWithPoints:customOverlay.points pointCount:customOverlay.pointCount fillColor:self.fillColor usingTriangleFan:YES];//绘制多边形 }else { [self renderLinesWithPoints:customOverlay.points pointCount:customOverlay.pointCount strokeColor:self.strokeColor lineWidth:self.lineWidth looped:NO];//绘制线 } }
如果不实现glRender,则需实现drawMapRect默认使用系统GDI绘制,GDI绘制方式在overlayView尺寸较大时可能有效率问题,因此建议使用glRender来实现自定义overlay绘制。
针对已添加的自定义覆盖物,您可以通过一下方式进行删除操作:
if (overlay != nil) { [_mapView removeOverlay:overlay]; }
自v2.6.0起,iOS地图SDK为广大开发者开放了OpenGL绘制功能,开发者可利用OpenGL的绘制来实现更多复杂的覆盖物绘制。
v2.6.0新增BMKMapViewDelegate中新增-mapView:onDrawMapFrame:,地图渲染每一帧画面过程中,以及每次需要重绘地图时(例如添加覆盖物)都会调用此接口。开发者可以在这个接口中进行opengl的绘制。不需要用户自己创建context和buffer,步骤如下(具体代码请参考BaiduMap_IOSSDK_SampleOpenGL绘制功能 部分):
1、转换坐标(坐标系原点为地图中心点);
2、根据地图的状态,设置旋转和缩放比例;
3、绘制;
注意事项:
app在前后台切换时,需要使用下面的代码停止地图的渲染和openGL的绘制:
- (void)applicationWillResignActive:(UIApplication *)application
{ [BMKMapViewwillBackGround];//当应用即将后台时调用,停止一切调用opengl相关的操作 } - (void)applicationDidBecomeActive:(UIApplication *)application
{ [BMKMapViewdidForeGround];//当应用恢复前台状态时调用,回复地图的渲染和opengl相关的操作 }