Appearance
覆盖物
本篇覆盖丰图地图 Android SDK 提供的地图覆盖物类型及其用法。所有覆盖物均在地图就绪后通过 map.addXxx() 添加,返回对应的覆盖物对象,后续可对其属性进行动态更新。
示例中的
SHENZHEN、BEIJING、SHANGHAI、GUANGZHOU、WUHAN为官方 DemoBaseMapActivity提供的坐标常量。
Marker 点标记
在地图指定坐标放置图标点标记,支持标题/副标题(InfoWindow)、自定义图标、拖拽、多帧动画图标等。是使用频率最高的覆盖物。
java
// 基本添加:Options 链式构建 + addMarker 返回 Marker
Marker marker = map.addMarker(new MarkerOptions()
.position(SHENZHEN)
.title("深圳顺丰总部")
.snippet("这是 Marker 的 snippet 描述")
.icon(BitmapDescriptorFactory.defaultMarker()));
// 可拖拽 Marker
Marker draggable = map.addMarker(new MarkerOptions()
.position(new LatLng(SHENZHEN.latitude - 0.01, SHENZHEN.longitude + 0.01))
.title("可拖拽Marker")
.draggable(true)
.icon(BitmapDescriptorFactory.defaultMarker()));
// 自定义 Bitmap 图标
BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(createCircleBitmap(dp(32), Color.RED));
Marker custom = map.addMarker(new MarkerOptions()
.position(pos)
.title("自定义图标Marker")
.icon(icon));
// 自定义 View 图标(View 先 measure/layout 再转 BitmapDescriptor)
View view = createCustomMarkerView("SF", "顺丰点", Color.parseColor("#FF6B00"));
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Marker viewMarker = map.addMarker(new MarkerOptions()
.position(pos)
.icon(BitmapDescriptorFactory.fromView(view)));
// 多帧图标(多帧切换动画)
ArrayList<BitmapDescriptor> icons = new ArrayList<>();
icons.add(BitmapDescriptorFactory.fromBitmap(bmp1));
icons.add(BitmapDescriptorFactory.fromBitmap(bmp2));
Marker multiFrame = map.addMarker(new MarkerOptions()
.position(pos)
.icons(icons)
.period(10)); // 切换周期
// 拖拽监听
map.setOnMarkerDragListener(new MapController.OnMarkerDragListener() {
@Override public void onMarkerDragStart(Marker marker) { }
@Override public void onMarkerDrag(Marker marker) { }
@Override public void onMarkerDragEnd(Marker marker) { }
});
// 动态更新属性
marker.setAnchor(0.5f, 1f); // 锚点(归一化 0~1)
marker.setRotateAngle(45f); // 旋转角度
marker.setZIndex(100f); // 层级
marker.setObject(myData); // 绑定业务对象
marker.setPeriod(20); // 多帧切换周期
marker.setVisible(false); // 可见性
marker.setInfoWindowEnable(false);// 是否点击弹出 InfoWindow
marker.setIcons(newIcons); // 切换多帧图标
marker.setDraggable(true); // 可拖拽
marker.setFlat(true); // 平贴地图(随地图旋转/倾斜)
marker.setPosition(newPos); // 移动位置
// 销毁单个 Marker
marker.destroy();
// 清除所有覆盖物
map.clear();要点:
icon单帧图标用BitmapDescriptorFactory.defaultMarker()或fromBitmap/fromView;多帧动画用icons+period。setAnchor(u, v)参数为归一化值(0~1),(0.5,1) 表示底部中心锚点。setFlat(true)使标记平贴地面,随地图旋转/倾斜;默认为竖立 billboard。setObject(obj)可绑定任意业务对象,点击回调中通过marker.getObject()取回。destroy()释放单个 Marker 资源;map.clear()清除地图上全部覆盖物。
Polyline 多段线
在地图上绘制折线,支持虚线、渐变色、大地曲线、纹理贴图、多线段等。
java
// 基本折线
Polyline polyline = map.addPolyline(new PolylineOptions()
.add(SHENZHEN)
.add(new LatLng(SHENZHEN.latitude + 0.01, SHENZHEN.longitude + 0.01))
.add(new LatLng(SHENZHEN.latitude + 0.01, SHENZHEN.longitude + 0.02))
.width(10)
.color(Color.RED));
// 虚线
map.addPolyline(new PolylineOptions()
.add(p1).add(p2).add(p3)
.width(8)
.color(Color.GREEN)
.setDottedLine(true));
// 渐变色线(逐段不同颜色)
PolylineOptions opts = new PolylineOptions().width(8);
List<Integer> colors = new ArrayList<>();
for (int i = 0; i <= 5; i++) {
opts.add(new LatLng(lat, lng));
colors.add(Color.HSVToColor(new float[]{i * 60f, 1f, 1f}));
}
opts.colorValues(colors).useGradient(true);
Polyline gradient = map.addPolyline(opts);
// 大地曲线(球面插值)
map.addPolyline(new PolylineOptions()
.add(SHENZHEN).add(new LatLng(31.2304, 121.4737))
.width(6)
.color(Color.RED)
.geodesic(true));
// 纹理线
Bitmap tex = makeStripeTexture(32, Color.RED, Color.WHITE);
map.addPolyline(new PolylineOptions()
.add(p1).add(p2).add(p3)
.width(16)
.color(Color.GRAY)
.setCustomTexture(BitmapDescriptorFactory.fromBitmap(tex)));
// 多线段(单条 Polyline 内多条独立折线)
List<List<LatLng>> multi = new ArrayList<>();
multi.add(line1); multi.add(line2);
Polyline multiLine = map.addPolyline(new PolylineOptions().width(8).color(Color.MAGENTA));
multiLine.setMultiLine(multi);
// 动态更新
polyline.setColor(Color.BLUE);
polyline.setWidth(12f);
polyline.addPoint(newLatLng); // 追加单点
polyline.setPoints(newPointList); // 重置全部点
polyline.setDottedLine(true);
polyline.setGeodesic(true);
polyline.setTransparency(0.5f); // 0~1,0 全透明
polyline.setUseTexture(true);
polyline.setZIndex(100f);
polyline.setVisible(false);
// 查找线上距离某点最近的点
LatLng nearest = polyline.getNearestLatLng(SHENZHEN);要点:
- 渐变色需
colorValues(list)+useGradient(true)配合使用;color()此时为底色。 - 纹理需
setCustomTexture(bitmapDescriptor),并配合setUseTexture(true)开启。 setTransparency(float)范围 0(不透明)~ 1(全透明)。geodesic(true)在长距离场景下按球面插值生成弧线。setMultiLine(list)可在单个 Polyline 对象中承载多条独立折线。
Polygon 多边形
绘制填充多边形,支持带洞、自定义填充色/描边色/线宽。
java
// 基本矩形
Polygon polygon = map.addPolygon(new PolygonOptions()
.add(new LatLng(lat - 0.005, lng - 0.005))
.add(new LatLng(lat - 0.005, lng + 0.005))
.add(new LatLng(lat + 0.005, lng + 0.005))
.add(new LatLng(lat + 0.005, lng - 0.005))
.strokeColor(Color.GREEN)
.fillColor(Color.argb(80, 0, 255, 0))
.strokeWidth(4));
// 带洞多边形
List<LatLng> hole = new ArrayList<>();
hole.add(new LatLng(lat - 0.002, lng - 0.003));
hole.add(new LatLng(lat - 0.002, lng + 0.003));
hole.add(new LatLng(lat + 0.002, lng + 0.003));
hole.add(new LatLng(lat + 0.002, lng - 0.003));
map.addPolygon(new PolygonOptions()
.add(/* 外环顶点 */)
.addHole(hole)
.strokeColor(Color.RED)
.fillColor(Color.argb(60, 255, 0, 0))
.strokeWidth(4));
// 动态更新
polygon.setFillColor(Color.argb(80, 0, 0, 255));
polygon.setStrokeColor(Color.BLUE);
polygon.setStrokeWidth(6f);
polygon.setPoints(newPoints); // 更新顶点
polygon.setZIndex(100f);
polygon.setVisible(false);
// 点是否在多边形内
boolean inside = polygon.contains(testLatLng);要点:
fillColor建议带 alpha(如Color.argb(80, ...))以便看到底层地图。addHole(List<LatLng>)可添加内洞,支持多个洞。contains(LatLng)可做点/面空间关系判断。
Circle 圆形
以指定中心点和半径(米)绘制圆,支持填充色、描边色、线宽。
java
// 基本圆形
Circle circle = map.addCircle(new CircleOptions()
.center(SHENZHEN)
.radius(1000) // 单位:米
.strokeColor(Color.RED)
.fillColor(Color.argb(60, 255, 0, 0))
.strokeWidth(4));
// 动态更新
circle.setFillColor(Color.argb(80, 0, 0, 255));
circle.setStrokeColor(Color.BLUE);
circle.setStrokeWidth(8f);
circle.setRadius(2000); // 半径(米)
circle.setCenter(newCenter);
circle.setZIndex(100f);
circle.setVisible(false);
// 点是否在圆内
boolean inside = circle.contains(testLatLng);
// 移除单个
circle.remove();要点:
radius单位为米,非像素。contains(LatLng)按球面距离判断点是否在圆内。- 可通过
circle.remove()单独移除,或map.clear()一并清除。
Arc 弧线
通过起点、终点、控制点三点确定一条弧线,支持颜色和线宽。
java
// 添加弧线
Arc arc = map.addArc(new ArcOptions()
.start(SHENZHEN)
.end(new LatLng(SHENZHEN.latitude + 0.02, SHENZHEN.longitude + 0.02))
.point(new LatLng(SHENZHEN.latitude + 0.03, SHENZHEN.longitude))
.strokeColor(Color.RED)
.strokeWidth(6));
// 动态更新
arc.setStrokeColor(Color.BLUE);
arc.setStrokeWidth(12f);
arc.setZIndex(100f);
arc.setVisible(false);要点:
start/end/point三点确定弧线,point为弧线经过的控制点。- Arc 仅支持描边属性(
strokeColor/strokeWidth),无填充。
Text 文字
在地图指定位置放置文字标注,支持字号、颜色、旋转、对齐、背景色。
java
// 基本文字
Text text = map.addText(new TextOptions()
.position(SHENZHEN)
.text("深圳顺丰总部")
.fontSize(28)
.textColor(Color.BLACK));
// 旋转文字
map.addText(new TextOptions()
.position(pos)
.text("旋转45度")
.fontSize(24)
.textColor(Color.MAGENTA)
.rotation(45));
// 带背景色文字
map.addText(new TextOptions()
.position(SHENZHEN)
.text("丰图地图文字对齐测试")
.fontSize(20)
.textColor(Color.parseColor("#CC0000"))
.backgroundColor(Color.parseColor("#330000FF")));
// 动态更新
text.setFontColor(Color.RED);
text.setFontSize(40);
text.setText("新文字");
text.setRotateAngle(90f);
text.setPosition(newPos);
text.setZIndex(100f);
text.setBackgroundColor(Color.YELLOW);
text.setAlign(TextOptions.ALIGN_CENTER);
text.setVisible(false);要点:
setAlign(int)支持ALIGN_CENTER/ALIGN_LEFT/ALIGN_RIGHT/ALIGN_TOP/ALIGN_BOTTOM/ALIGN_TOP_LEFT等九种对齐方式,决定文字相对定位点的偏移。backgroundColor设为 0 表示无背景。rotation单位为度。
NavigateArrow 导航箭头
沿路径绘制带箭头的导航指示线,支持顶面/侧面颜色、线宽。
java
// 基本导航箭头
NavigateArrow arrow = map.addNavigateArrow(new NavigateArrowOptions()
.add(SHENZHEN)
.add(new LatLng(SHENZHEN.latitude + 0.005, SHENZHEN.longitude + 0.003))
.add(new LatLng(SHENZHEN.latitude + 0.010, SHENZHEN.longitude + 0.006))
.width(16)
.sideColor(Color.BLUE)
.topColor(Color.BLUE));
// 多点路径
NavigateArrowOptions opts = new NavigateArrowOptions()
.width(10)
.sideColor(Color.GREEN)
.topColor(Color.GREEN);
for (int i = 0; i <= 8; i++) {
opts.add(new LatLng(lat, lng));
}
map.addNavigateArrow(opts);
// 动态更新
arrow.setTopColor(Color.RED);
arrow.setSideColor(Color.RED); // 设为 0 则回退使用 topColor
arrow.setWidth(24f);
arrow.setZIndex(100f);
arrow.setPoints(newPoints);
arrow.setVisible(false);要点:
topColor为线面颜色,sideColor为箭头三角颜色;sideColor设为 0 时回退使用topColor。- 路径点通过
add(LatLng)逐点追加,或setPoints(List)整体替换。
