vue使用高德地图动态设置marker,polyline,点击显示。

按照高德官网,引入sdk。

效果如下

点击地图上的车辆icon显示它的轨迹,右上角的选择框效果如下

选中车牌将它的位置定位到地图中心,并设置地图缩放等级。

initMap(e, p) {//e:经纬度,p:zoom
      let that = this;
      var map = new AMap.Map("map", {
        center: [120.67, 30.07], // [纬度,经度]
        resizeEnable: true,
        scrollWheel: true,
        zoom: 12,
        mapStyle: "amap://styles/macaron", //设置地图的显示样式
      });
      //重新设置地图中心点
      if (e) {
        map.setZoomAndCenter(p, e);
      }
      AMap.plugin(["AMap.ToolBar", "AMap.Scale"], function () {
        // map.addControl(new AMap.ToolBar());
        map.addControl(new AMap.Scale());
      });
      let locationList = that.list2;
      // 添加marker
      locationList.map((item) => {
        let state = item.cp;
        var marker = new AMap.Marker({
          position: [item.lat, item.lng], //位置
          offset: new AMap.Pixel(-10, -10),
          map: map,
          icon: require("../assets/img/car.png"), // 添加 Icon 图标 URL
        });
        // map.add(marker);

        // 创建折线实例
        var polyline = new AMap.Polyline({
          path: item.path,
          isOutline: true,
          outlineColor: "#ffeeff",
          borderWeight: 3,
          strokeColor: "#3366FF",
          strokeOpacity: 1,
          strokeWeight: 6,
          // 折线样式还支持 'dashed'
          strokeStyle: "solid",
          // strokeStyle是dashed时有效
          strokeDasharray: [10, 5],
          lineJoin: "round",
          lineCap: "round",
          zIndex: 50,
        });

        marker.on("click", function (e) {
          map.remove(map.getAllOverlays("polyline"));//删除已经存在的轨迹
          map.add(polyline);
          that.cp = state;
        });
      });
      // map.setFitView(polyline, marker);
    },
   mounted() {
    this.map = this.initMap();
  },

下拉框选择代码

<div class="magWrap">
            <div class="btn" v-show="checked">{{ cp }}</div>
        <div class="select">
          <el-select
            v-model="value"
            placeholder="请选择车牌"
            @change="handleChange(value)"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            >
            </el-option>
          </el-select>
        </div>
        <div class="cbox">
          <el-checkbox v-model="checked">显示车牌</el-checkbox>
        </div>
      </div>

       options: [
        {
          value: "all",
          label: "全部",
        },
        {
          value: [120.67, 30.07],
          label: "浙A15742",
        },
        {
          value: [120.8612070444, 30.0436018444],
          label: "浙B15742",
        },
        {
          value: [120.5659987111, 29.9430935111],
          label: "浙A17542",
        },
      ],

     handleChange(e) {
      let center = [e[0], e[1]];
      let fs = "";
      if (e == "all") {
        fs = 12;
        center = [120.67, 30.07];
      } else {
        fs = 16;
      }
      console.log(center, "test");
      this.initMap(center, fs);
    },

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Vue 3和高德地图的轨迹回放,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了Vue 3和高德地图的相关依赖。你可以使用npm或yarn来安装这些依赖。例如,在你的Vue项目中,可以运行以下命令来安装高德地图的依赖: ``` npm install vue-amap ``` 2. 在你的Vue组件中,引入Vue AMap库并注册该组件: ```javascript import VueAMap from 'vue-amap'; export default { ... mounted() { Vue.use(VueAMap); VueAMap.initAMapApiLoader({ key: 'your_amap_key', plugin: ['AMap.Polyline'] }); }, ... } ``` 在上述代码中,你需要将`your_amap_key`替换成你自己的高德地图API密钥。 3. 在模板中添加地图容器和控件: ```html <template> <div> <amap :zoom="13" :center="[lng, lat]"> <amap-polyline :path="path" :visible="true" :style="{ strokeColor: 'red', strokeWeight: 6 }"></amap-polyline> </amap> </div> </template> ``` 在上述代码中,`amap`是地图容器组件,`amap-polyline`是轨迹回放的折线组件。你可以根据需要调整地图的缩放级别和中心点位置,以及折线的样式。 4. 在组件的`data`属性中定义轨迹回放的经纬度数据: ```javascript data() { return { lng: 116.397428, lat: 39.90923, path: [ [116.405289, 39.904987], [116.406089, 39.904987], [116.406289, 39.905087], // 更多经纬度数据... ] }; } ``` 你需要根据实际情况提供正确的经纬度数据。 5. 最后,你可以根据需求实现轨迹回放的逻辑。例如,你可以使用定时器来逐步显示折线上的点,实现轨迹的动态回放效果。 这样,你就可以在Vue 3中使用高德地图实现轨迹回放了。记得根据你的实际需求进行相应的调整和扩展。希望这能帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值