更新时间:2022-10-27
弧线
用户可以根据三个有序点唯一确定一条弧线,满足您的业务需求。
1添加弧线覆盖物数据(BMKArcline)
Objective-C
Swift
//添加弧线覆盖物 //传入的坐标顺序为起点、途经点、终点 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; BMKArcline *arcline = [BMKArcline arclineWithCoordinates:coords]; [_mapView addOverlay:arcline];
var coords = [CLLocationCoordinate2D]() coords.append(CLLocationCoordinate2D(latitude: 40.065, longitude: 116.124)) coords.append(CLLocationCoordinate2D(latitude: 40.125, longitude: 116.304)) coords.append(CLLocationCoordinate2D(latitude: 40.065, longitude: 116.404)) /** 根据指定经纬度生成一段圆弧 @param coords 指定的经纬度坐标点数组(需传入3个点) @return 新生成的BMKArcline实例 */ let arcline: BMKArcline = BMKArcline(coordinates: &coords) /** 向地图View添加Overlay,需要实现BMKMapViewDelegate的-mapView:viewForOverlay:方法 来生成标注对应的View @param overlay 要添加的overlay */ mapView.add(arcline)
2实现代理方法生成对应的View(BMKArclineView)
Objective-C
Swift
#pragma mark - BMKMapViewDelegate /** 根据overlay生成对应的BMKOverlayView @param mapView 地图View @param overlay 指定的overlay @return 生成的覆盖物View */ - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay { 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; }
//MARK:BMKMapViewDelegate /** 根据overlay生成对应的BMKOverlayView @param mapView 地图View @param overlay 指定的overlay @return 生成的覆盖物View */ func mapView(_ mapView: BMKMapView!, viewFor overlay: BMKOverlay!) -> BMKOverlayView! { if overlay.isKind(of: BMKArcline.self) { //初始化一个overlay并返回相应的BMKArclineView的实例 let arclineView = BMKArclineView(arcline: arcline) //设置arclineView的画笔颜色 arclineView?.strokeColor = UIColor.blue //设置arclineView为虚线样式 //lineDash属性已废弃,since5.0.0请使用lineDashType配置虚线样式 //arclineView?.lineDash = true arclineView?.lineDashType = kBMKLineDashTypeSquare //设置arclineView的线宽度 arclineView?.lineWidth = 6.0 return arclineView } return nil }
3运行程序
效果如图:
多边形
1添加多边形覆盖物数据(BMKPolygon)
Objective-C
Swift
// 添加多边形覆盖物 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; BMKPolygon *polygon = [BMKPolygon polygonWithCoordinates:coords count:5]; [_mapView addOverlay:polygon];
var coords = [CLLocationCoordinate2D]() coords.append(CLLocationCoordinate2D(latitude: 39.965, longitude: 116.304)) coords.append(CLLocationCoordinate2D(latitude: 39.865, longitude: 116.304)) coords.append(CLLocationCoordinate2D(latitude: 39.865, longitude: 116.404)) coords.append(CLLocationCoordinate2D(latitude: 39.905, longitude: 116.354)) coords.append(CLLocationCoordinate2D(latitude: 39.965, longitude: 116.404)) /** 根据多个经纬点生成多边形 @param coords 经纬度坐标点数组 @param count 点的个数 @return 新生成的BMKPolygon实例 */ let polygon: BMKPolygon = BMKPolygon(coordinates: &coords, count: 5) /** 向地图View添加Overlay,需要实现BMKMapViewDelegate的-mapView:viewForOverlay:方法 来生成标注对应的View @param overlay 要添加的overlay */ mapView.add(polygon)
2实现代理方法生成对应的View(BMKPolygonView)
Objective-C
Swift
#pragma mark - BMKMapViewDelegate /** 根据overlay生成对应的BMKOverlayView @param mapView 地图View @param overlay 指定的overlay @return 生成的覆盖物View */ - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{ 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 = YES; return polygonView; } return nil; }
//MARK:BMKMapViewDelegate /** 根据overlay生成对应的BMKOverlayView @param mapView 地图View @param overlay 指定的overlay @return 生成的覆盖物View */ func mapView(_ mapView: BMKMapView!, viewFor overlay: BMKOverlay!) -> BMKOverlayView! { if overlay.isKind(of: BMKPolygon.self) { //初始化一个overlay并返回相应的BMKPolygonView的实例 let polygonView = BMKPolygonView(polygon: polygon) //设置polygonView的画笔(边框)颜色 polygonView?.strokeColor = UIColor.init(red: 0, green: 0, blue: 0.5, alpha: 1) //设置polygonView的填充色 polygonView?.fillColor = UIColor.init(red: 0, green: 1, blue: 1, alpha: 0.2) //设置polygonView的线宽度 polygonView?.lineWidth = 2.0 //设置polygonView为虚线样式 //lineDash属性已废弃,since5.0.0请使用lineDashType配置虚线样式 //polygonView?.lineDash = true polygonView?.lineDashType = kBMKLineDashTypeSquare return polygonView } return nil }
3运行程序
效果如图
圆形
1添加圆形覆盖物数据(BMKCircle)
Objective-C
Swift
// 添加圆形覆盖物 CLLocationCoordinate2D coor; coor.latitude = 39.915; coor.longitude = 116.404; BMKCircle *circle = [BMKCircle circleWithCenterCoordinate:coor radius:5000]; [_mapView addOverlay:circle];
let coor = CLLocationCoordinate2D(latitude: 39.915, longitude: 116.404) /** 根据中心点和半径生成圆 @param coord 中心点的经纬度坐标 @param radius 半径,单位:米 @return 新生成的BMKCircle实例 */ let circle: BMKCircle = BMKCircle(center: coor, radius: 5000) /** 向地图View添加Overlay,需要实现BMKMapViewDelegate的-mapView:viewForOverlay:方法 来生成标注对应的View @param overlay 要添加的overlay */ mapView.add(circle)
2实现代理方法生成对应的View(BMKCircleView)
Objective-C
Swift
#pragma mark - BMKMapViewDelegate /** 根据overlay生成对应的BMKOverlayView @param mapView 地图View @param overlay 指定的overlay @return 生成的覆盖物View */ - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{ if ([overlay isKindOfClass:[BMKCircle class]]){ BMKCircleView* circleView = [[BMKCircleView alloc] initWithOverlay:overlay]; circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5]; circleView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5]; circleView.lineWidth = 10.0; return circleView; } return nil; }
/** 根据overlay生成对应的BMKOverlayView @param mapView 地图View @param overlay 指定的overlay @return 生成的覆盖物View */ func mapView(_ mapView: BMKMapView!, viewFor overlay: BMKOverlay!) -> BMKOverlayView! { if overlay.isKind(of: BMKCircle.self) { //初始化一个overlay并返回相应的BMKCircleView的实例 let circleView: BMKCircleView = BMKCircleView(circle: circle) //设置circleView的填充色 circleView.fillColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.5) //设置circleView的画笔(边框)颜色 circleView.strokeColor = UIColor(red: 0, green: 0, blue: 1, alpha: 0.5) //设置circleView的轮廓宽度 circleView.lineWidth = 5.0 return circleView } return nil }
3运行程序
效果如图:
渐变圆
1添加渐变圆覆盖物数据(BMKCircle)
Objective-C
Swift
// 添加渐变圆覆盖物 CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(39.915, 116.404); BMKCircle *gradientCircle = [BMKCircle circleWithCenterCoordinate:coor radius:5000]; [_mapView addOverlay:gradientCircle];
let coor = CLLocationCoordinate2D(latitude: 39.915, longitude: 116.404) let gradientCircle: BMKCircle = BMKCircle(center: coor, radius: 5000) mapView.add(gradientCircle)
2实现代理方法生成对应View(BMKGradientCircleView)
Objective-C
Swift
- (__kindof BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay { if ([overlay isKindOfClass:[BMKCircle class]]) { BMKGradientCircleView *gradientCircleView = [[BMKGradientCircleView alloc] initWithOverlay:overlay]; /** * 渐变规则如下: * (0 ~ radiusWeight * radius) 该部分颜色从 centerColor 渐变至 colorWeight * (sideColor - centerColor); * (radiusWeight * radius ~ radius)该部分间颜色从 centerColor + colorWeight * (sideColor - centerColor) 渐变至 sideColor; */ gradientCircleView.radiusWeight = 0.6; gradientCircleView.colorWeight = 0.1; gradientCircleView.centerColor = [UIColor colorWithRed:93.f / 255.f green:232.f / 255.f blue:204.f / 255.f alpha:0.0f]; gradientCircleView.sideColor = [UIColor colorWithRed:93.f / 255.f green:232.f / 255.f blue:204.f / 255.f alpha:1.f]; // 边框 gradientCircleView.lineWidth = 2.f; gradientCircleView.strokeColor = [UIColor colorWithRed:93.f / 255.f green:232.f / 255.f blue:204.f / 255.f alpha:1.f]; return gradientCircleView; } return nil; }
func mapView(_ mapView: BMKMapView, viewFor overlay: BMKOverlay) -> BMKOverlayView? { if overlay.isKind(of: BMKCircle.self) { let gradientCircleView: BMKGradientCircleView = BMKGradientCircleView(circle: gradientCircle)! // 渐变规则如下: //(0 ~ radiusWeight * radius) 该部分颜色从 centerColor 渐变至 colorWeight * (sideColor - centerColor); //(radiusWeight * radius ~ radius)该部分间颜色从 centerColor + colorWeight * (sideColor - centerColor) 渐变至 sideColor; gradientCircleView.centerColor = UIColor(red: 93.0 / 255.0, green: 232.0 / 255.0, blue: 204.0 / 255.0, alpha: 0.0) gradientCircleView.sideColor = UIColor(red: 93.0 / 255.0, green: 232.0 / 255.0, blue: 204.0 / 255.0, alpha: 1.0) // 边框 gradientCircleView.lineWidth = 2.0 gradientCircleView.strokeColor = UIColor(red: 93.0 / 255.0, green: 232.0 / 255.0, blue: 204.0 / 255.0, alpha: 1.0) return gradientCircleView } return nil }
3运行程序
效果如图:
自定义图片图层(图片覆盖物)
图片图层(GroundOverlay)又称为图片覆盖物,此功能支持在地图的指定位置上添加一张大小合适的图片。图片可随地图的平移、缩放、旋转等操作做相应的变换。 图片图层是一种特殊的Overlay, 它位于底图和底图标注层之间(即图片图层不会遮挡地图标注信息), 此外,图片图层的添加顺序不会影响其他图层(例如:POI搜索图层、我的位置图层等)的叠加关系。
1添加图片图层数据(BMKGroundOverlay)
图片图层对象初始化的方法有两种:(1)根据指定经纬度坐标生成 (2)根据指定区域生成。
Objective-C
Swift
//添加图片图层覆盖物(第一种:根据指定经纬度坐标生成) CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(39.910, 116.420); /** *根据指定经纬度坐标生成一个groundOverlay *@param position 指定的经纬度坐标 *@param zoomLevel 不损失精度绘制原始图片的地图等级 *@param anchor 绘制图片的锚点 *@param icon 绘制使用的图片 *@return 新生成的groundOverlay对象 */ _ground = [BMKGroundOverlay groundOverlayWithPosition:coord zoomLevel:12 anchor:CGPointMake(0, 0) icon:[UIImage imageNamed:@"groundIcon.png"]]; //图片纹理透明度,最终透明度 = 纹理透明度 * alpha,取值范围为[0.0f, 1.0f],默认为1.0f _ground.alpha = 1; [_mapView addOverlay:ground]; //添加图片图层覆盖物(第二种:根据指定区域生成) CLLocationCoordinate2D coords[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 imageNamed:@"groundIcon.png"]]; [_mapView addOverlay:ground2];
var coords = [CLLocationCoordinate2D]() coords.append(CLLocationCoordinate2D(latitude: 39.910, longitude: 116.370)) coords.append(CLLocationCoordinate2D(latitude: 39.950, longitude: 116.430)) //表示一个经纬度区域:东北角点经纬度坐标和西南角点经纬度坐标 let bound = BMKCoordinateBounds(northEast: coords[1], southWest: coords[0]) /** 根据指定区域生成一个图层 @param bounds 指定的经纬度区域 @param icon 绘制使用的图片 @return 新生成的BMKGroundOverlay实例 */ let ground: BMKGroundOverlay = BMKGroundOverlay(bounds: bound, icon: UIImage(named: "groundIcon")) //图片纹理透明度,最终透明度 = 纹理透明度 * alpha,取值范围为[0.0f, 1.0f],默认为1.0f ground.alpha = 0.8 /** 向地图View添加Overlay,需要实现BMKMapViewDelegate的-mapView:viewForOverlay:方法 来生成标注对应的View @param overlay 要添加的overlay */ mapView.add(ground)
2实现代理方法返回对应的View(BMKGroundOverlayView)
Objective-C
Swift
#pragma mark - BMKMapViewDelegate /** 根据overlay生成对应的BMKOverlayView @param mapView 地图View @param overlay 指定的overlay @return 生成的覆盖物View */ - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id<BMKOverlay>)overlay { if ([overlay isKindOfClass:[BMKGroundOverlay class]]) { //初始化一个overlay并返回相应的BMKGroundOverlayView的实例 BMKGroundOverlayView *groundView = [[BMKGroundOverlayView alloc] initWithGroundOverlay:overlay]; return groundView; } return nil; }
/** 根据overlay生成对应的BMKOverlayView @param mapView 地图View @param overlay 指定的overlay @return 生成的覆盖物View */ func mapView(_ mapView: BMKMapView!, viewFor overlay: BMKOverlay!) -> BMKOverlayView! { if overlay.isKind(of: BMKGroundOverlay.self) { let groundView = BMKGroundOverlayView(groundOverlay: ground) return groundView } return nil }
3运行程序
效果如图:
删除Overlay
Objective-C
Swift
//删除单个overlay [_mapView removeOverlay:ground];
mapView?.removeOverlay(ground)
批量操作Overlay
批量添加Overlay
Objective-C
Swift
/** *向地图窗口添加一组Overlay,需要实现BMKMapViewDelegate的-mapView:viewForOverlay:函数来生成标注对应的View *@param overlays 要添加的overlay数组 */ [_mapView addOverlays:overlays];
/** *向地图窗口添加一组Overlay,需要实现BMKMapViewDelegate的-mapView:viewForOverlay:函数来生成标注对应的View *@param overlays 要添加的overlay数组 */ mapView?.addOverlays(overlays)
批量删除Overlay
Objective-C
Swift
/** *移除一组Overlay *@param overlays 要移除的overlay数组 */ [_mapView removeOverlays:overlays];
/** *移除一组Overlay *@param overlays 要移除的overlay数组 */ mapView?.removeOverlays(overlays)