vue中拿到高德組件實(shí)例

example.png
<template>
  <section id="geo-info">
    <el-form-item label="經(jīng)度" prop="lngitude">
      <el-input placeholder="" v-model="renderLongitude" disabled> </el-input>
    </el-form-item>
    <el-form-item label="緯度" prop=" latitude">
      <el-input placeholder="" v-model="renderlatitude" disabled> </el-input>
    </el-form-item>
    <el-form-item label="地址" prop=" address">
      <el-input placeholder="" v-model="renderAdderss" id="pickerInput"> </el-input>
    </el-form-item>
    <el-form-item label="地標(biāo)" prop=" landmark">
      <el-select v-model="renderLandmark" placeholder="請(qǐng)選擇">
        <el-option v-for="item in landmarkArr" :key="item.value" :label="item.label" :value="item.value">
          <span class="name">{{ item.label }}</span>
          <span class="addr">{{ item.address }}</span>
        </el-option>
      </el-select>
    </el-form-item>
    <el-button type="primary" id="addMark">添加坐標(biāo)</el-button>
    <el-button type="primary" id="clearMark">清除坐標(biāo)</el-button>
    <el-button type="primary" id="addPolygon">繪制圍欄</el-button>
    <el-button type="primary" id="clearPolygon">清除圍欄</el-button>
    <el-button type="primary" @click="dianji">外部調(diào)用</el-button>
    <div class="geo-info">
      <el-amap :zoom="20" :center="poiResultLocation" :events="mapEvents" :amap-manager="amapManager"></el-amap>
    </div>
    <el-form-item label="半徑" prop=" radius">
      <el-input placeholder="" v-model="radius" :map-manager="amapManager"> </el-input>
    </el-form-item>
  </section>
</template>
<script>
  import {
    AMapManager
  } from "vue-amap";
  // 拿到高德原生 AMap.Map 實(shí)例  可以在mathods使用  用的時(shí)候  map:amapManager.getMap()
  let amapManager = new AMapManager();
  export default {
    watch: {
      // 把搜索位置傳入拖拽選址中
      poiResultLocation(newLocation) {
        this.positionPicker.start(newLocation);
      },
      fenceArr(newFenceArr) {
      }
    },
    data() {
      let self = this;
      return {
        renderAdderss: this.address,
        renderlatitude: this.latitude,
        renderLongitude: this.lngitude,
        renderLandmark: this.landmark,
        landmarkArr: [],
        amapManager,
        poiResultLocation: [113.855443, 34.526265], // 初始位置
        positionPicker: null, //拿到拖拽選址組件實(shí)例
        mouseTool: null, //注冊(cè)全局繪制圍欄插件實(shí)例
        polygon: null, //繪制圍欄成功時(shí)的插件實(shí)例 (連接坐標(biāo)點(diǎn))
        fenceArr: [], //  圍欄·經(jīng)緯度數(shù)組
        flagBit: false, //繪制圍欄標(biāo)志位
        renderFenceOk: false, //繪制圍欄成功標(biāo)志位
        positionMark: [],
        mapEvents: {
          // 初始化時(shí)加載
          init(map) {
            let addPolygonButton = document.getElementById("addPolygon")
            let clearPolygonButton = document.getElementById("clearPolygon")
            let clearMarkButton = document.getElementById("clearMark");
            let addMarkButtom = document.getElementById("addMark");
            // 引入放大縮小工具插件
            map.addControl(
              new AMap.ToolBar({
                liteStyle: true
              })
            );
            // 引入繪圖插件  全局調(diào)用繪圖插件
            self.mouseTool = new AMap.MouseTool(map);
            // 繪制圍欄
            AMap.event.addDomListener(addPolygonButton, "click", (resData) => {
              if (!self.positionPicker.started) {
                self.$message.warning("請(qǐng)先添加坐標(biāo)")
                self.mouseTool.close(true);
                return
              } else {
                if (self.flagBit) {
                  self.mouseTool.close(false);
                } else {
                  self.mouseTool.polygon();
                }
              }
            }, false);
            // 清除圍欄
            AMap.event.addDomListener(clearPolygonButton, "click", () => {
              //移除計(jì)算的圍欄
              self.polygon.setMap(null)
              self.mouseTool.close(true);
              self.flagBit = false
              self.renderFenceOk = false
            }, false);
            //繪制完成觸發(fā)此事件
            self.mouseTool.on("draw", (resData) => {
              self.mouseTool.close(false)
              self.flagBit = true
              // 把坐標(biāo)點(diǎn)傳給連接坐標(biāo)點(diǎn)插件
              let arr = resData.obj.G.path.map(item => {
                return [item.lng, item.lat]
              })
              self.fenceArr = arr 
              // 計(jì)算這個(gè)點(diǎn)是否在圍欄外
              self.polygon = new AMap.Polygon({
                map: map,
                path: self.fenceArr,
                draggable: false
              });
              if (!self.polygon.contains(self.positionMark)) {
                self.$message.warning("圍欄范圍必須將坐標(biāo)點(diǎn)包含在內(nèi)")
                self.polygon.setMap(null)
                self.mouseTool.close(true);
                self.flagBit = false
              } else {
                self.$message.success("繪制圍欄成功")
                self.renderFenceOk = true
              }
            })
            // 搜索位置插件
            AMapUI.loadUI(["misc/PoiPicker"], (PoiPicker) => {
              let poiPicker = new PoiPicker({
                input: "pickerInput"
              });
              //初始化poiPicker
              poiPickerReady(poiPicker);
            });
            //監(jiān)聽poi選中信息
            function poiPickerReady(poiPicker) {
              poiPicker.on("poiPicked", (poiResult) => {
                // 清除圍欄
                self.mouseTool.close(true);
                let {  lng,  lat} = poiResult.item.location;
                self.poiResultLocation =  [lng, lat];
              });
            }
            // 拖拽選址組件
            AMapUI.loadUI(["misc/PositionPicker"], (PositionPicker) => {
              const positionPicker = new PositionPicker({
                mode: "dragMarker",
                map: amapManager.getMap(),
                iconStyle: {
                  //自定義外觀
                  url: "http://webapi.amap.com/ui/1.0/ui/misc/PointSimplifier/examples/imgs/people.png",
                  ancher: [24, 40],
                  size: [48, 48]
                }
              });
              positionPicker.on("success", (positionResult) => {
                console.log(positionResult);
                let { lng,  lat } = positionResult.position;
                self.positionMark = [lng, lat]
                if (self.renderFenceOk) {
                  if (!self.polygon.contains(self.positionMark)) {
                    self.$message.warning("坐標(biāo)點(diǎn)必需包含在圍欄內(nèi)")
                    self.polygon.setMap(null)
                    self.mouseTool.close(true);
                    self.flagBit = false
                    self.renderFenceOk = false
                  }
                }
                self.renderAdderss = positionResult.address;
                self.renderLongitude = positionResult.position.lng;
                self.renderlatitude = positionResult.position.lat;
                self.renderLandmark = positionResult.regeocode.pois[0].name;
                let landmarkArr = positionResult.regeocode.pois.map(item => {
                  return {
                    label: item.name || "",
                    value: item.id || "",
                    address: item.address || ""
                  };
                });
                self.landmarkArr = landmarkArr;
              });
              positionPicker.on("fail", (positionResult) => {
                console.log(positionResult);
                self.renderAdderss = "獲取地址失敗";
                self.renderLongitude = "獲取經(jīng)度失敗";
                self.renderlatitude = "獲取緯度失敗";
                self.renderLandmark = "獲取失敗";
              });
              // 注冊(cè)DOM對(duì)象事件:給DOM對(duì)象注冊(cè)事件
              // 添加坐標(biāo)
              AMap.event.addDomListener(addMarkButtom, "click", (resData) => {
                if (self.renderFenceOk) {
                  return
                } else {
                  self.mouseTool.close(true);
                  positionPicker.start();
                }
              });
              // 清除坐標(biāo)
              AMap.event.addDomListener(clearMarkButton, "click", (resData) => {
                // 繪制圍欄成功就不能清除坐標(biāo)
                if (self.renderFenceOk) {
                  return
                } else {
                  self.flagBit = false
                  self.mouseTool.close(true);
                  positionPicker.stop();
                }
              });
              positionPicker.start(self.poiResultLocation);
              self.positionPicker = positionPicker;
            });
          }
        }
      };
    },
    mounted() {
      this.$nextTick(() => {});
    },
    methods: {
      dianji() {
        // 拿到高德原生組件實(shí)例
        //  map:amapManager.getMap()
        this.mouseTool.close(true)
        console.log(this.mouseTool)
      },
      addMark() {
        // marker.setMap(o);
      }
    },
    props: {
      // 經(jīng)度
      lngitude: {
        type: [String, Number]
      },
      // 緯度
      latitude: {
        type: [String, Number]
      },
      // 地址
      address: {
        type: [String, Number]
      },
      // 地標(biāo)
      landmark: {
        type: [String, Number]
      },
      radius: {
        type: [String, Number]
      }
    }
  };
</script>
<style lang="scss">
  .geo-info {
    margin-top: 20px;
    height: 600px;
  }
  .name {
    text-overflow: ellipsis;
    overflow: hidden;
  }
  .addr {
    font-size: 12px;
    color: #b4b4b4;
  }
  .highlighted .addr {
    color: #ddd;
  }
</style>


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末袋马,一起剝皮案震驚了整個(gè)濱河市宝与,隨后出現(xiàn)的幾起案子丰榴,更是在濱河造成了極大的恐慌吸耿,老刑警劉巖滑臊,帶你破解...
    沈念sama閱讀 218,682評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異粹懒,居然都是意外死亡嘱吗,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門整以,熙熙樓的掌柜王于貴愁眉苦臉地迎上來胧辽,“玉大人,你說我怎么就攤上這事公黑∫厣蹋” “怎么了?”我有些...
    開封第一講書人閱讀 165,083評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵凡蚜,是天一觀的道長人断。 經(jīng)常有香客問我,道長朝蜘,這世上最難降的妖魔是什么含鳞? 我笑而不...
    開封第一講書人閱讀 58,763評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮芹务,結(jié)果婚禮上蝉绷,老公的妹妹穿的比我還像新娘。我一直安慰自己枣抱,他們只是感情好熔吗,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著佳晶,像睡著了一般桅狠。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上轿秧,一...
    開封第一講書人閱讀 51,624評(píng)論 1 305
  • 那天中跌,我揣著相機(jī)與錄音,去河邊找鬼菇篡。 笑死漩符,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的驱还。 我是一名探鬼主播嗜暴,決...
    沈念sama閱讀 40,358評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼凸克,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了闷沥?” 一聲冷哼從身側(cè)響起萎战,我...
    開封第一講書人閱讀 39,261評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎舆逃,沒想到半個(gè)月后蚂维,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,722評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡路狮,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年鸟雏,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片览祖。...
    茶點(diǎn)故事閱讀 40,030評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡孝鹊,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出展蒂,到底是詐尸還是另有隱情又活,我是刑警寧澤,帶...
    沈念sama閱讀 35,737評(píng)論 5 346
  • 正文 年R本政府宣布锰悼,位于F島的核電站柳骄,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏箕般。R本人自食惡果不足惜耐薯,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望丝里。 院中可真熱鬧曲初,春花似錦、人聲如沸杯聚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽幌绍。三九已至颁褂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間傀广,已是汗流浹背颁独。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評(píng)論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留伪冰,地道東北人誓酒。 一個(gè)月前我還...
    沈念sama閱讀 48,237評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像糜值,于是被迫代替她去往敵國和親丰捷。 傳聞我的和親對(duì)象是個(gè)殘疾皇子坯墨,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評(píng)論 2 355