管理中心
您当前正在浏览新版开发者文档中心,目录分类和层级有所调整。点击左侧当前文档分类名称前的“☰”图标,可切换文档分类。 了解新版目录
问题处理与运维开发与测试问题应用服务开发地图服务(Map Kit)如何在地图上绘制虚线

如何在地图上绘制虚线

问题现象

参考官网的文档,可以实现在地图上添加折线,如何实现虚线效果?

效果预览

背景知识

展开

名称

值

说明

DASH

0

表示折线、多边形或圆的边框中的短划线。

DOT

1

表示折线、多边形或圆的边框的点。

GAP

2

表示折线、多边形或圆中边框的间隙。

解决方案

patterns可配置折线的样式,将patterns设置成patterns: [{ type: 0, length: 10 }, { type: 2, length: 10 }]表示短划线与间隙1:1相间,即呈现虚线效果。完整示例参考如下:

收起
自动换行
深色代码主题
复制
  1. import { MapComponent, mapCommon, map } from '@kit.MapKit';
  2. import { AsyncCallback } from '@kit.BasicServicesKit';
  3. import { display } from '@kit.ArkUI';
  4. @Entry
  5. @Component
  6. struct PolylinePage {
  7. private mapOptions?: mapCommon.MapOptions;
  8. private mapController?: map.MapComponentController;
  9. private callback?: AsyncCallback<map.MapComponentController>;
  10. @State mapHeight: number = 0;
  11. aboutToAppear(): void {
  12. try {
  13. let displayClass = display.getDefaultDisplaySync();
  14. this.mapHeight = this.getUIContext().px2vp(displayClass.height);
  15. } catch (error) {
  16. console.error(`getDefaultDisplaySync error. Code is ${error.code}, message is ${error.message}.`);
  17. }
  18. // 地图初始化参数
  19. this.mapOptions = {
  20. position: {
  21. target: {
  22. latitude: 31.985,
  23. longitude: 118.78
  24. },
  25. zoom: 15
  26. }
  27. };
  28. this.callback = async (err, mapController) => {
  29. if (!err) {
  30. this.mapController = mapController;
  31. // polyline初始化参数
  32. let polylineOption: mapCommon.MapPolylineOptions = {
  33. points: [
  34. { longitude: 118.78, latitude: 31.975 },
  35. { longitude: 118.78, latitude: 31.982 },
  36. { longitude: 118.79, latitude: 31.985 }
  37. ],
  38. clickable: true,
  39. startCap: mapCommon.CapStyle.ROUND,
  40. endCap: mapCommon.CapStyle.ROUND,
  41. jointType: mapCommon.JointType.BEVEL,
  42. visible: true,
  43. width: 10,
  44. zIndex: 1,
  45. gradient: false,
  46. patterns: [{ type: 0, length: 10 }, { type: 2, length: 10 }]
  47. };
  48. // 创建polyline
  49. try {
  50. await this.mapController.addPolyline(polylineOption);
  51. } catch (error) {
  52. console.error(`创建polyline失败, code is:${error.code}, message is ${error.message}`);
  53. }
  54. } else {
  55. console.error(`地图初始化失败, code is:${err.code}, message is ${err.message}`);
  56. }
  57. };
  58. }
  59. build() {
  60. Stack() {
  61. MapComponent({ mapOptions: this.mapOptions, mapCallback: this.callback })
  62. .width('100%')
  63. .height(this.mapHeight)
  64. .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM]);
  65. }.height('100%');
  66. }
  67. }