vue+天地圖繪制區(qū)域邊界線

[項(xiàng)目地址:https://gitee.com/fan-caixia/tianditumap)

微信截圖_20240619163825.png



代碼結(jié)構(gòu):
  • 服務(wù)端代碼

public/index.html

<script
type="text/javascript"
src="https://api.tianditu.gov.cn/api?v=4.0&tk=天地圖密鑰"
</script>

代碼片段:
<!--
以山西臨汾為例,繪制邊界線
 -->

<template>
  <div>
    <div id="mapDiv"></div>
  </div>
</template>

<script>
import * as PointsData from "../../utils/pointslinfen.js";

export default {
  props: {},
  data() {
    return {
      bounds: [],
    };
  },
  mounted() {
    this.tdtMap = new window.T.Map("mapDiv", {
      attributionControl: false,
      inertia: false,
    });
    setTimeout(() => {
      this.onLoad();
    }, 500);
  },

  created() {},
  methods: {
    MarkerClick(e) {
      console.log(e.lnglat.getLng() + "," + e.lnglat.getLat(), "坐標(biāo)信息");
    },

    onLoad() {
      // 先清除坐標(biāo)
      let newMarker = this.tdtMap.getOverlays(); // 獲取到了地圖上的所有點(diǎn)

      for (let i = 0; i < newMarker.length; i++) {
        this.tdtMap.removeOverLay(newMarker[i]);
      }

      var zoom = 8;

      // 根據(jù)經(jīng)緯度設(shè)置中心點(diǎn)
      this.tdtMap.centerAndZoom(
        new window.T.LngLat("111.646", "36.0067"),
        zoom
      );

      this.tdtMap.setStyle("indigo"); // map.setStyle是用來設(shè)置顏色的
      // this.tdtMap.setStyle('black');
      //向地圖上添加自定義標(biāo)注
      var marker = new window.T.Marker(
        new window.T.LngLat("111.646", "36.0067")
      );
      marker.addEventListener("click", this.MarkerClick);

      this.tdtMap.addOverLay(marker);

      this.setlinfenArea(); // 繪制邊界線
    },

    setlinfenArea() {
      // 獲取行政區(qū)劃邊界信息
      let res = PointsData.default.PointsData;

      var countries = res.features;
      var sc = countries[0];

      var bounds = sc.geometry.coordinates[0][0];

      this.drawLine(bounds);
    },

    drawLine(lines) {
      // 繪制邊界線
      let style = {
        color: "#81a5b9",
        weight: 3,
        opacity: 1,
        lineStyle: "solid", // 實(shí)線;dashed虛線
        fillColor: "transprent",
        fillOpacity: 0, // 透明度
      };
      let points = [];
      lines.forEach((line) => {
        // lines是邊界經(jīng)緯度組成的數(shù)組
        var point = new window.T.LngLat(line[0], line[1]);
        points.push(point);
      });
      // console.log(points)
      var poly = new window.T.Polygon(points, style);
      this.tdtMap.addOverLay(poly);
    },
  },
};
</script>

<style lang="less" scoped>
#mapDiv {
  width: 100%;
  height: 500px;
}
</style>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容