高德地图 Web JS API UI组件示例学习笔记(5)——轨迹展示

简单路径

展示PathSimplifier的基本接口的使用方法。
在这里插入图片描述

AMapUI.load(['ui/misc/PathSimplifier', 'lib/$'], function (PathSimplifier, $) {
      if (!PathSimplifier.supportCanvas) {
        alert('当前环境不支持 Canvas!');
        return;
      }

      // 创建简单路径
      var pathSimplifierIns = new PathSimplifier({
        zIndex: 100,
        //autoSetFitView:false,
        map: map, //所属的地图实例
        // 获取路径
        getPath: function (pathData, pathIndex) {
          return pathData.path;
        },
        // 鼠标悬浮在点上,显示的标题
        getHoverTitle: function (pathData, pathIndex, pointIndex) {
          if (pointIndex >= 0) {
            //point 
            return pathData.name + ',点:' + pointIndex + '/' + pathData.path.length;
          }

          return pathData.name + ',点数量' + pathData.path.length;
        },
        renderOptions: {
          renderAllPointsIfNumberBelow: 100 //绘制路线节点,如不需要可设置为-1
        }
      });

      window.pathSimplifierIns = pathSimplifierIns;

      //设置数据
      pathSimplifierIns.setData([{
        name: '路线0',
        path: [
          [116.405289, 39.904987],
          [113.964458, 40.54664],
          [111.47836, 41.135964],
          [108.949297, 41.670904],
          [106.380111, 42.149509],
          [103.774185, 42.56996],
          [101.135432, 42.930601],
          [98.46826, 43.229964],
          [95.777529, 43.466798],
          [93.068486, 43.64009],
          [90.34669, 43.749086],
          [87.61792, 43.793308]
        ]
      }]);

      //对第一条线路(即索引 0)创建一个巡航器
      var navg1 = pathSimplifierIns.createPathNavigator(0, {
        loop: true, //循环播放
        speed: 1000000 //巡航速度,单位千米/小时
      });

      navg1.start();
    });

仅展示驶过线

在这里插入图片描述
通过pathLinePassedStyle属性控制PathSimplifier巡航器已经驶过部分的线路样式。

AMapUI.load(['ui/misc/PathSimplifier', 'lib/$'], function (PathSimplifier, $) {
      if (!PathSimplifier.supportCanvas) {
        alert('当前环境不支持 Canvas!');
        return;
      }

      var emptyLineStyle = {
        lineWidth: 0,
        fillStyle: null,
        strokeStyle: null,
        borderStyle: null
      };

      var pathSimplifierIns = new PathSimplifier({
        zIndex: 100,
        //autoSetFitView:false,
        map: map, //所属的地图实例
        getPath: function (pathData, pathIndex) {
          return pathData.path;
        },
        getHoverTitle: function (pathData, pathIndex, pointIndex) {
          return null;
        },
        renderOptions: {
          //将点、线相关的style全部置emptyLineStyle
          pathLineStyle: emptyLineStyle,
          pathLineSelectedStyle: emptyLineStyle,
          pathLineHoverStyle: emptyLineStyle,
          keyPointStyle: emptyLineStyle,
          startPointStyle: emptyLineStyle,
          endPointStyle: emptyLineStyle,
          keyPointHoverStyle: emptyLineStyle,
          keyPointOnSelectedPathLineStyle: emptyLineStyle
        }
      });

      window.pathSimplifierIns = pathSimplifierIns;

      pathSimplifierIns.setData([{
        name: '测试',
        path: [
          [116.405289, 39.904987],
          [113.964458, 40.54664],
          [111.47836, 41.135964],
          [108.949297, 41.670904],
          [106.380111, 42.149509],
          [103.774185, 42.56996],
          [101.135432, 42.930601],
          [98.46826, 43.229964],
          [95.777529, 43.466798],
          [93.068486, 43.64009],
          [90.34669, 43.749086],
          [87.61792, 43.793308]
        ]
      }]);

      function onload() {
        pathSimplifierIns.renderLater();
      }

      function onerror(e) {
        alert('图片加载失败!');
      }

      var navg1 = pathSimplifierIns.createPathNavigator(0, {
        loop: true,
        speed: 1000000,
        pathNavigatorStyle: {
          width: 24,
          height: 24,
          //使用图片
          content: PathSimplifier.Render.Canvas.getImageContent('./imgs/plane.png', onload, onerror),
          strokeStyle: null,
          fillStyle: null,
          //经过路径的样式
          pathLinePassedStyle: {
            lineWidth: 6,
            strokeStyle: 'black',
            dirArrowStyle: {
              stepSpace: 15,
              strokeStyle: 'red'
            }
          }
        }
      });
      navg1.start();
    });

简单数据附加

给PathSimplifier路径的节点附加数据,并展示。

AMapUI.load(['ui/misc/PathSimplifier', 'lib/$'], function (PathSimplifier, $) {
      if (!PathSimplifier.supportCanvas) {
        alert('当前环境不支持 Canvas!');
        return;
      }

      var pathSimplifierIns = new PathSimplifier({
        zIndex: 100,
        //autoSetFitView:false,
        map: map, //所属的地图实例
        getPath: function (pathData, pathIndex) {
          var points = pathData.points,
            lnglatList = [];
          for (var i = 0, len = points.length; i < len; i++) {
            lnglatList.push(points[i].lnglat);
          }
          return lnglatList;
        },
        getHoverTitle: function (pathData, pathIndex, pointIndex) {
          if (pointIndex >= 0) {
            //point 
            return pathData.name + ',' + pathData.points[pointIndex].name;
          }
          return pathData.name + ',点数量' + pathData.points.length;
        },
        renderOptions: {
          renderAllPointsIfNumberBelow: 100 //绘制路线节点,如不需要可设置为-1
        }
      });

      window.pathSimplifierIns = pathSimplifierIns;

      //设置数据
      pathSimplifierIns.setData([{
        name: '路线0',
        points: [{
          name: "点a",
          lnglat: [116.405289, 39.904987]
        }, {
          name: "点b",
          lnglat: [113.964458, 40.54664]
        }, {
          name: "点c",
          lnglat: [111.47836, 41.135964]
        }, {
          name: "点d",
          lnglat: [108.949297, 41.670904]
        }, {
          name: "点e",
          lnglat: [106.380111, 42.149509]
        }, {
          name: "点f",
          lnglat: [103.774185, 42.56996]
        }, {
          name: "点g",
          lnglat: [101.135432, 42.930601]
        }, {
          name: "点h",
          lnglat: [98.46826, 43.229964]
        }, {
          name: "点i",
          lnglat: [95.777529, 43.466798]
        }, {
          name: "点j",
          lnglat: [93.068486, 43.64009]
        }, {
          name: "点k",
          lnglat: [90.34669, 43.749086]
        }, {
          name: "点l",
          lnglat: [87.61792, 43.793308]
        }]
      }]);

      //选中路线0
      pathSimplifierIns.setSelectedPathIndex(0);

      pathSimplifierIns.on('pointClick', function (e, info) {
        console.log('Click: ' + info.pathData.points[info.pointIndex].name);
      });
    });
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 想要在Vue中使用高德JS API UI组件示例轨迹,需要按照以下步骤进行操作: 1. 在Vue项目中安装高德地图的JavaScript APIJS API UI的依赖,可以使用npm或yarn命令进行安装。 2. 在Vue项目中引入高德地图的JavaScript APIJS API UI的依赖。可以在Vue的入口文件(如main.js)中使用import语句引入。 3. 在需要使用JS API UI组件的Vue组件中,使用组件的方式导入所需的组件。例如,如果需要使用轨迹组件,可以使用import语句导入AMapUI的Track组件。 4. 在Vue组件的模板中,使用导入的组件进行渲染。例如,在需要显示轨迹的地方使用<amap-track></amap-track>进行渲染。 5. 在Vue组件的JavaScript代码中,根据需要配置组件的属性和方法。可以通过提供的API文档查找所需的属性和方法,根据文档中的说明进行配置。 6. 在Vue组件的JavaScript代码中,使用高德地图的JavaScript APIJS API UI的相关方法进行地图的初始化和操作,例如将地图显示在指定的容器中,加载轨迹数据等。 7. 运行Vue项目,即可在界面上看到使用高德JS API UI组件示例轨迹的效果。 需要注意的是,为了使用高德地图APIUI组件,可能需要在高德地图开放平台注册并获取密钥。在开发过程中,可以在相关配置中使用密钥,以实现相关功能。 以上就是在Vue中使用高德JS API UI组件示例轨迹的基本步骤。具体的实现方式和配置可以根据自己的需求进行调整和扩展。 ### 回答2: 要使用高德 JS API UI 组件示例轨迹,首先需要安装相应的依赖包。可以在 Vue 项目的根目录下运行以下命令来安装: ``` npm install @amap/amap-jsapi-loader ``` 安装完成后,在需要使用轨迹示例组件中引入 AMapJSApiLoader: ```javascript import AMapJSApiLoader from '@amap/amap-jsapi-loader'; export default { data() { return { map: null }; }, mounted() { this.initMap(); }, methods: { async initMap() { // 加载高德地图 JS API const loader = new AMapJSApiLoader({ key: 'your-amap-api-key', version: '2.0', plugins: ['AMap.ToolBar'] }); // 加载完成后初始化地图 const AMap = await loader.load(); this.map = new AMap.Map('mapContainer', { // 地图选项配置 }); // 在地图上绘制轨迹 this.drawTrack(); }, drawTrack() { // 根据自己的需求,使用高德地图的服务和组件来绘制轨迹 // 示例:使用 AMap.Polyline 组件绘制折线 const polyline = new AMap.Polyline({ // 折线选项配置 }); polyline.setMap(this.map); } } } ``` 在上述代码中,我们首先在组件的 mounted 钩子函数中调用 initMap 方法来初始化地图。在 initMap 方法中,我们加载了高德地图JS API,并在加载完成后初始化了地图对象。然后我们调用 drawTrack 方法来绘制轨迹,在该方法中可以使用高德地图提供的服务和组件来绘制所需的轨迹效果。 需要注意的是,示例中的 `your-amap-api-key` 需要替换为自己申请的高德地图 API Key。另外,根据具体需求,可能需要根据高德地图API 文档来配置轨迹的样式、数据等参数。 ### 回答3: 要使用高德 JS API UI 组件示例轨迹,首先需要确保已经引入了高德地图 API 的 JavaScript库以及相关的插件。 在 Vue 中,可以在页面中使用 script 标签引入高德地图 API 的 JavaScript库,并在 mounted 钩子函数中初始化地图,并根据需要引入用于绘制轨迹的插件。 具体步骤如下: 1. 首先在 index.html 文件的 head 标签中引入高德地图 API 的 JavaScript库,例如: ```html <script src="https://webapi.amap.com/maps?v=1.4.15&key=your_amap_api_key"></script> ``` 2. 在组件文件中,引入需要用到的高德地图插件,例如轨迹插件: ```javascript import 'AMap.MarkerClusterer'; import 'AMap.Marker'; // 其他需要的插件 ``` 3. 在 mounted 钩子函数中,初始化地图并使用相关的插件进行轨迹绘制,例如: ```javascript mounted() { // 初始化地图 const map = new AMap.Map('mapContainer', { center: [lng, lat], zoom: 10 }); // 绘制轨迹 const polyline = new AMap.Polyline({ path: [ // 轨迹点数组 [lng1, lat1], [lng2, lat2], [lng3, lat3], ... ], strokeColor: "#3366FF", // 线颜色 strokeOpacity: 1, // 线透明度 strokeWeight: 3, // 线宽度 map: map // 轨迹绘制在地图上 }); // 其他相关操作 } ``` 以上就是使用 Vue 框架中实现高德 JS API UI 组件示例轨迹的一般步骤。根据具体需求,还可以进一步自定义地图样式、轨迹样式等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值