百度地圖取點(diǎn)糖埋,以及不規(guī)則區(qū)域取點(diǎn),行政區(qū)域搜索取點(diǎn)窃这,自動(dòng)計(jì)算中心點(diǎn)瞳别,經(jīng)緯度小工具。

#寫在前面

因?yàn)殚_(kāi)發(fā)需要杭攻,弄了一個(gè)小的獲取百度地圖經(jīng)緯度的取點(diǎn)小工具祟敛。方便自己取點(diǎn),不規(guī)則區(qū)域取點(diǎn)兆解,行政區(qū)域搜索取點(diǎn)馆铁。自動(dòng)獲取經(jīng)緯度。(咳咳锅睛,最近感覺(jué)GitHub有點(diǎn)慢埠巨?历谍??辣垒?所以轉(zhuǎn)移到了gitee)
項(xiàng)目地址(gitee):https://gitee.com/lovezengcheng/baidumap-getpoint | 【點(diǎn)擊跟蹤項(xiàng)目地址】

取點(diǎn)工具.jpg

1望侈,引入vue-baidu-map,element勋桶,scss脱衙,這個(gè)就不說(shuō)了把。

2例驹,頁(yè)面代碼

<template>
  <div class="map-all">
    <baidu-map
      class="bm-view"
      :center="center"
      :zoom="zoom"
      :dragging="!isEdit"
      :double-click-zoom="!isEdit"
      @ready="handler"
      :min-zoom="5"
      :max-zoom="18"
      :mapClick="false"
      @click="mapClick"
    >
      <!-- 縮放 -->
      <bm-navigation
        anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
        :offset="{ width: 10, height: 40 }"
      ></bm-navigation>
      <bm-city-list
        anchor="BMAP_ANCHOR_TOP_RIGHT"
        :offset="{ width: 20, height: 20 }"
      ></bm-city-list>
      <bm-polygon
        :path="polygonPath"
        stroke-color="blue"
        :stroke-opacity="0.5"
        :stroke-weight="2"
        :editing="isEdit"
        @lineupdate="updatePolygonPath"
      />
    </baidu-map>
    <div class="dot-list">
      <div class="dot-list-btn">
        <el-button type="primary" size="medium" @click="clearBtn"
          ><i class="el-icon-document-copy" />&nbsp;清空</el-button
        >
        <el-button
          size="medium"
          :type="isEdit ? 'success' : 'primary'"
          @click="startEdit"
          ><i :class="isEdit ? 'el-icon-close' : 'el-icon-check'" />&nbsp;{{
            isEdit ? "關(guān)閉繪制" : "開(kāi)始繪制"
          }}</el-button
        >
      </div>
      <h3 v-if="mapPinot">當(dāng)前點(diǎn)擊點(diǎn)的經(jīng)緯度</h3>
      <div class="dot-list-top" v-if="mapPinot">{{ mapPinot }}</div>
      <h3>框選區(qū)域的點(diǎn)的集合</h3>
      <div class="search-city">
        <el-input
          placeholder="請(qǐng)輸入行政區(qū)域搜索"
          size="medium"
          v-model="searchInput"
          class="input-with-select"
        >
          <el-button
            type="primary"
            slot="append"
            icon="el-icon-search"
            @click="searchCity(searchInput)"
          ></el-button>
        </el-input>
      </div>
      <div class="dot-list-text">
        <p>[</p>
        <span v-for="(item, index) in polygonPath" :key="index"
          >{{ item }}<em v-if="index !== polygonPath.length - 1">,</em></span
        >
        <p>]</p>
      </div>
      <div class="dot-list-center" v-if="polygonPath.length > 1">
        <el-button
          type="danger"
          size="medium"
          @click="getPolygonCenter(polygonPath)"
          >獲取中心點(diǎn)</el-button
        >
        <div class="dot-list-txt" v-if="polygonCenter">{{ polygonCenter }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "baidumap",
  data() {
    return {
      center: "深圳",
      zoom: 19,
      map: null,
      BMap: null,
      textarea: "",
      isEdit: false,
      polygonPath: [],
      mapPinot: null,
      polygonCenter: null,
      searchInput: "",
    };
  },
  methods: {
    handler({ BMap, map }) {
      this.map = map;
      this.BMap = BMap;
      map.enableScrollWheelZoom(true);
      map.setDefaultCursor("pointer");
    },
    updatePolygonPath(e) {
      this.polygonPath = e.target.getPath();
    },
    // 是否開(kāi)啟取點(diǎn)畫框
    startEdit() {
      this.isEdit = !this.isEdit;
    },
    // 點(diǎn)擊地圖 取點(diǎn)
    mapClick(type) {
      if (this.isEdit) {
        this.polygonPath.push(type.point);
      }
      this.mapPinot = type.point;
    },
    // 清除
    clearBtn() {
      this.polygonPath = [];
      this.mapPinot = null;
      this.polygonCenter = null;
      this.searchInput = "";
    },
    // 獲取中心點(diǎn)
    getPolygonCenter(polygonPath) {
      if (polygonPath.length > 1) {
        let lngTotal = 0;
        let latTotal = 0;
        let len = polygonPath.length;
        polygonPath.forEach((element) => {
          lngTotal += element.lng;
          latTotal += element.lat;
        });
        const lngCenter = (lngTotal / len).toFixed(8);
        const latCenter = (latTotal / len).toFixed(8);
        this.polygonCenter = { lng: lngCenter, lat: latCenter };
      } else {
        this.$message.error("請(qǐng)先選擇區(qū)域岂丘,獲取范圍點(diǎn)!");
      }
    },
    // 搜索
    searchCity(input) {
      this.polygonPath = [];
      let bdary = new BMap.Boundary();
      bdary.get(input, (rs) => {
        rs.boundaries.forEach((item) => {
          let arrTemp = item.split(";");
          arrTemp.forEach((element) => {
            let arrPint = element.split(",");
            this.polygonPath.push({
              lng: Number(arrPint[0]),
              lat: Number(arrPint[1]),
            });
          });
        });
      });
      // 獲取所有點(diǎn)的最佳視圖
      setTimeout(() => {
        this.getPolygonCenter(this.polygonPath);
        this.map.setViewport(this.polygonPath);
      }, 500);
    },
  },
};
</script>

<style lang="scss" scoped="scoped">
.map-all ::v-deep .city_content_top {
  padding: 15px 10px 25px 10px;
  .sel_city_input {
    height: 24px !important;
    line-height: 24px !important;
  }
}
.map-all {
  position: relative;
  .bm-view {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 1;
    width: 100%;
    height: 100vh;
    cursor: pointer;
  }
  .dot-list {
    position: absolute;
    top: 0;
    left: 0;
    padding: 10px;
    z-index: 100;
    width: 300px;
    height: 100vh;
    background: rgba(0, 0, 0, 0.5);
    h3 {
      padding: 5px;
      color: rgba(255, 255, 255, 0.8);
      font-size: 14px;
    }
    .dot-list-text {
      background: #fff;
      min-height: 300px;
      padding: 10px;
      max-height: calc(100vh - 360px);
      overflow: auto;
      font-size: 14px;
      color: #555;
      border-radius: 4px;
      line-height: 1.4em;
      span {
        display: block;
      }
      p {
        padding: 5px;
      }
    }
    .dot-list-top {
      background: #fff;
      padding: 10px;
      font-size: 14px;
      color: #555;
      border-radius: 4px;
    }
  }

  .dot-list-btn {
    padding: 10px;
    text-align: center;
    display: flex;
    button {
      width: 100%;
    }
  }
  .dot-list-center {
    padding-top: 10px;
    button {
      width: 100%;
    }
  }
  .dot-list-txt {
    margin-top: 10px;
    padding: 10px;
    border-radius: 4px;
    background: #fff;
    font-size: 14px;
    color: #555;
    line-height: 1.4em;
  }
  .search-city {
    margin-bottom: 10px;
  }
}
</style>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市眠饮,隨后出現(xiàn)的幾起案子奥帘,更是在濱河造成了極大的恐慌,老刑警劉巖仪召,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件寨蹋,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡扔茅,警方通過(guò)查閱死者的電腦和手機(jī)已旧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)召娜,“玉大人运褪,你說(shuō)我怎么就攤上這事【寥常” “怎么了秸讹?”我有些...
    開(kāi)封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)雅倒。 經(jīng)常有香客問(wèn)我璃诀,道長(zhǎng),這世上最難降的妖魔是什么蔑匣? 我笑而不...
    開(kāi)封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任劣欢,我火速辦了婚禮,結(jié)果婚禮上裁良,老公的妹妹穿的比我還像新娘凿将。我一直安慰自己,他們只是感情好价脾,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布牧抵。 她就那樣靜靜地躺著,像睡著了一般彼棍。 火紅的嫁衣襯著肌膚如雪灭忠。 梳的紋絲不亂的頭發(fā)上膳算,一...
    開(kāi)封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音弛作,去河邊找鬼涕蜂。 笑死,一個(gè)胖子當(dāng)著我的面吹牛映琳,可吹牛的內(nèi)容都是我干的机隙。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼萨西,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼有鹿!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起谎脯,我...
    開(kāi)封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤葱跋,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后源梭,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體娱俺,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年废麻,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了荠卷。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡烛愧,死狀恐怖油宜,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情怜姿,我是刑警寧澤慎冤,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站社牲,受9級(jí)特大地震影響粪薛,放射性物質(zhì)發(fā)生泄漏悴了。R本人自食惡果不足惜搏恤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望湃交。 院中可真熱鬧熟空,春花似錦、人聲如沸搞莺。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)才沧。三九已至迈喉,卻和暖如春绍刮,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背挨摸。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工孩革, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人得运。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓膝蜈,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親熔掺。 傳聞我的和親對(duì)象是個(gè)殘疾皇子饱搏,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容