【vue+amap】高德地图绘制多边形区域

在这里插入图片描述

参考文档:
高德地图参考手册
高德地图示例代码

1、高德地图控制台创建应用,获取权限ak

高德地图控制台
在这里插入图片描述

Ps.本项目里按钮等基础控件使用的是element-ui版本控件

2、项目内全局引入

index.html内引入高德地图代码:

<script type="text/javascript">
    window._AMapSecurityConfig = {
      securityJsCode: "你的安全密钥"
    };
  </script>
  <script
    type="text/javascript"
    src="https://webapi.amap.com/maps?v=1.4.6&key=你的key"
  ></script>
  <script
    type="text/javascript"
    src="http://webapi.amap.com/maps?v=1.4.6&key=你的key&plugin=AMap.PolyEditor&plugin=AMap.MouseTool"
  ></script>

在这里插入图片描述

3、完整项目代码

template:

<template>
  <div class="map-wrap">
    <div>
      <h1>{{ msg }}</h1>
      <div class="flex">
        <div class="button-wrap">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-edit"
            @click="handleDraw"
            >绘制</el-button
          >
          <el-button size="small" icon="el-icon-add" @click="handelFinishDraw"
            >完成</el-button
          >
          <el-button
            size="small"
            icon="el-icon-refresh-left"
            @click="handleClearDraw"
            >重置</el-button
          >
        </div>
        <div class="picker-color" v-if="isediting">
          <div class="text">选择颜色</div>
          <span
            @click="handleChangeColor(item)"
            v-for="item in colors"
            :key="item.code"
            :class="[
              'color' + item.code,
              drawColor == item.value ? 'active' : '',
            ]"
          >
            <i v-if="drawColor == item.value" class="el-icon-check"></i>
            <i v-else>&nbsp;</i>
          </span>
        </div>
      </div>
    </div>
    <div style="width: 1200px; height: 500px; padding-left: calc(50% - 600px)">
      <div id="container"></div>
    </div>
  </div>
</template>

js:


<script>
export default {
  name: "Map",
  data() {
    return {
      msg: "地图绘制展示页",
      map: null,
      poly: null,
      drawColor: "#2A8DFF",
      colors: [
        { code: 1, value: "#FF6B36" },
        { code: 2, value: "#FFAD29" },
        { code: 3, value: "#FFDA21" },
        { code: 4, value: "#29E98F" },
        { code: 5, value: "#1EEDE6" },
        { code: 6, value: "#2A8DFF" },
        { code: 7, value: "#CC16EF" },
        { code: 8, value: "#F53ABD" },
      ],
      paths: [
        [111.683736, 40.818554],
        [111.684444, 40.816971],
        [111.689036, 40.818172],
        [111.688264, 40.819788],
      ],
      mouseOverEvent: true,
      isediting: false,
      tool: null,
    };
  },

  created() {
    this.$nextTick(() => {
      this.createMap();
    });
  },
  methods: {
    createMap() {
      // 地图创建
      var map = new AMap.Map("container", {
        zoom: 11, //级别
        center: [111.688264, 40.818205], //兴泰御都国际
        viewMode: "3D", //使用3D视图
      });
      // 添加一个标记点
      var marker = new AMap.LabelMarker({
        icon: "http://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
        position: [111.687931, 40.818392],
        offset: new AMap.Pixel(-10, -30),
        text: {
          content: "东方国信",
          direction: "right",
          style: {
            fontSize: 15,
            fillColor: "#fff",
            strokeColor: "rgba(255,255,0,0.5)",
            strokeWidth: 2,
            padding: [3, 10],
            backgroundColor: "blue",
            borderColor: "#ccc",
            borderWidth: 3,
          },
        },
      });
      var labelsLayer = new AMap.LabelsLayer({
        collision: true,
      });
      labelsLayer.add(marker);// 将 LabelMarker 实例添加到 LabelsLayer 上
      map.add(labelsLayer);// 将 LabelsLayer 添加到地图上
      // 创建默认区域
      var polygon = new AMap.Polygon({
        path: this.paths,
        strokeColor: "#fff",
        strokeWeight: 6,
        strokeOpacity: 0.2,
        fillOpacity: 0.4,
        fillColor: this.drawColor,
        zIndex: 50,
      });
      map.add(polygon);
      map.setFitView([polygon]); // 缩放地图到合适的视野级别
      this.map = map;
      // 如果后期想修改默认区域:修改this.poly即可
      // var polyEditor = new AMap.PolyEditor(map, polygon);
      // this.poly = polyEditor;
    },
    /* 操作按钮 */
    // 编辑
    handleDraw() {
      // this.poly.open();
      this.isediting = true;
      var mouseTool = new AMap.MouseTool(this.map);
      this.tool = mouseTool;
      mouseTool.polygon({
        strokeColor: "#FFF",
        strokeOpacity: 1,
        strokeWeight: 6,
        strokeOpacity: 0.2,
        fillColor: this.drawColor,
        fillOpacity: 0.4,
        strokeStyle: "solid",
      });
      mouseTool.on("draw", function (event) {
      	console.log("覆盖物对象绘制完成:", event.obj);// event.obj 为绘制出来的覆盖物对象
      });
    },
    handelFinishDraw() {
      this.isediting = false;
      this.tool.close();
    },
    //重置
    handleClearDraw() {
      this.isediting = false;
      this.tool.close(true);
    },
    //切换颜色
    handleChangeColor(item) {
      this.drawColor = item.value;
      this.tool.polygon({
        strokeColor: "#FFF",
        strokeOpacity: 1,
        strokeWeight: 6,
        strokeOpacity: 0.2,
        fillColor: this.drawColor,
        fillOpacity: 0.4,
        strokeStyle: "solid",
      });
    },
  },
};
</script>

css:

<style scoped>
h1 {
  font-weight: normal;
}
#container {
  width: 100%;
  height: 100%;
}
.map-wrap {
  position: relative;
  width: 100%;
  height: 100%;
}
.map-wrap .flex {
  display: flex;
  flex-shrink: 0;
  white-space: nowrap;
  justify-content: space-between;
  align-items: center;
  height: 50px;
  line-height: 50px;
  width: 1200px;
  padding-left: calc(50% - 600px);
}
.allmap {
  width: 100%;
  height: calc(100% - 50px);
  position: absolute;
}
ul {
  list-style: none;
}
.picker-color {
  text-align: right;
  padding-right: 30px;
}
.text {
  display: inline-block;
  padding: 0 10px;
  float: left;
}
span {
  display: inline-block;
  width: 24px;
  height: 24px;
  line-height: 20px;
  border-radius: 4px;
  border-width: 2px;
  border-style: solid;
  margin-left: 8px;
  overflow: hidden;
  text-align: center;
  margin-top: 10px;
  float: left;
}
span i {
  font-weight: 600;
}
.color1 {
  border-color: #ff6b36;
  background: rgba(255, 107, 54, 0.3);
  color: #ff6b36;
}
.color2 {
  border-color: #ffad29;
  background: rgba(255, 173, 41, 0.3);
  color: #ffad29;
}
.color3 {
  border-color: #ffda21;
  background: rgba(255, 218, 33, 0.3);
  color: #ffda21;
}
.color4 {
  border-color: #29e98f;
  background: rgba(41, 233, 143, 0.3);
  color: #29e98f;
}
.color5 {
  border-color: #1eede6;
  background: rgba(30, 237, 230, 0.3);
  color: #1eede6;
}
.color6 {
  border-color: #2a8dff;
  background: rgba(42, 141, 255, 0.3);
  color: #2a8dff;
}
.color7 {
  border-color: #cc16ef;
  background: rgba(204, 22, 239, 0.3);
  color: #cc16ef;
}
.color8 {
  border-color: #f53abd;
  background: rgba(245, 58, 189, 0.3);
  color: #f53abd;
}
</style>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3和TypeScript都是现代化前端开发技术,相结合可以大大提高开发效率和代码质量。高德地图是目前应用广泛的地图API之一,在Vue3中使用TypeScript实现高德地图展示,可以借助高德地图JavaScript API实现。 首先需要在项目中安装高德地图JavaScript API:npm install @amap/amap-jsapi-loader 接着在组件中引入AMapJSAPILoader,代码如下: ```typescript import { defineComponent, onMounted } from 'vue'; import { AMapJSAPILoader } from '@amap/amap-jsapi-loader'; export default defineComponent({ setup() { const initMap = () => { AMapJSAPILoader.load({ key: '你的高德地图key', version: '2.0', plugins: [], }).then((AMap: any) => { // 在这里可以使用高德地图API进行相关操作 }); }; onMounted(() => { initMap(); }); return {}; }, }); ``` 其中,key为高德地图的开发者key,version为API的版本号,plugins为需要引入的插件。 使用AMapJSAPILoader.load方法加载API之后,就可以通过AMap对象操作地图了,例如: ```typescript AMap.plugin('AMap.Geolocation', function() { var geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默认:true timeout: 10000, // 超过10秒后停止定位,默认:无穷大 maximumAge: 0, // 定位结果缓存0毫秒,默认:0 convert: true, // 自动偏移坐标,偏移后的坐标为高德坐标,默认:true showMarker: true, // 定位成功后在定位到的位置显示点标记,默认:true showCircle: true, // 定位成功后用圆圈表示定位精度范围,默认:true panToLocation: true, // 定位成功后将定位到的位置作为地图中心点,默认:true zoomToAccuracy: true, // 定位成功后调整地图视野范围使定位位置及精度范围可见,默认:false }); geolocation.getCurrentPosition(); AMap.event.addListener(geolocation, 'complete', onComplete); AMap.event.addListener(geolocation, 'error', onError); function onComplete(data: any) { // data中包含定位结果 } function onError(error: any) { // 定位出错 } }); ``` 以上代码使用高德地图的定位插件获取当前位置,通过添加事件监听器,可以获取获取定位结果。通过类似的方式,可以实现地图的缩放、平移、标注、路径规划等功能。 总之,在Vue3中使用TypeScript实现高德地图的展示,可以借助高德地图JavaScript API,通过类似上述的方法进行相关操作,实现地图的各种功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值