最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue 使用高德地圖添加點(diǎn)標(biāo)記 + 點(diǎn)擊地圖獲取坐標(biāo) + 帶搜索(即地理編碼 + 逆地理編碼)  附完整示例

 更新時(shí)間:2024年01月23日 15:19:06   作者:Web - Nancy  
這篇文章主要介紹了Vue 使用高德地圖添加點(diǎn)標(biāo)記 + 點(diǎn)擊地圖獲取坐標(biāo) + 帶搜索(即地理編碼 + 逆地理編碼)  附完整示例,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧

高德地圖

與真實(shí)世界聯(lián)通 - 高德開放平臺(tái)為開發(fā)者賦能,將地圖精致地呈現(xiàn)在您的應(yīng)用中

無論基于哪種平臺(tái),都可以通過高德開放平臺(tái)API和SDK,輕松地完成地圖的構(gòu)建工作

效果

一、介紹

1、官方文檔:地圖 | 高德地圖API

地圖 | 高德地圖API地圖,地圖sdk,地圖JS API,地圖定制,自定義地圖,地圖覆蓋物,地圖繪制,路線規(guī)劃,坐標(biāo)轉(zhuǎn)換,距離/面積計(jì)算,距離測量,室內(nèi)地圖,地圖顯示,地圖個(gè)性化,地圖開發(fā),室內(nèi)定位

https://lbs.amap.com/product/map#/

二、準(zhǔn)備工作

1、注冊/登錄賬號(hào)

2、點(diǎn)擊控制臺(tái)

3、創(chuàng)建應(yīng)用

4、獲取key和密鑰,如上圖所示

注:使用web服務(wù)API,如下圖所示

三、安裝依賴包

1、安裝命令

npm i @amap/amap-jsapi-loader --save

 2、這是我的版本 

"@amap/amap-jsapi-loader": "^1.0.1",

四、使用步驟

1、在index.html文件中引入密鑰

代碼如下(示例):

<script type="text/javascript">
    window._AMapSecurityConfig = {
      securityJsCode: '', // 你的密鑰
    }
  </script>

2、在vue文件中引入依賴包

代碼如下(示例):

import AMapLoader from "@amap/amap-jsapi-loader"

3、申明變量并初始化調(diào)用

代碼如下(示例):

 
import { shallowRef, defineEmits, ref, onBeforeUnmount } from 'vue';
const map = shallowRef(null);
let AMapObj, placeSearch, marker, geocoder;
function initMap(){
  AMapLoader.load({
    key:'',  //設(shè)置您的key
    version:"2.0",
    plugins:['AMap.ToolBar','AMap.Driving'],
    AMapUI:{
      version:"1.1",
      plugins:[],
    },
    Loca:{
      version:"2.0.0"
    },
  }).then((AMap)=>{
    // console.log(AMap);
    AMapObj = AMap;
    map.value = new AMap.Map("map-box",{
      viewMode:"3D",
      zoom:10,
      zooms:[2,22],
      center: [105.602725,37.076636],
    });
    map.value.on('click', onMapClick);
    AMap.plugin(
      ['AMap.ToolBar','AMap.Scale','AMap.Geolocation','AMap.PlaceSearch',
        'AMap.Geocoder','AMap.AutoComplete'],
      () => {
        // 縮放條
        const toolbar = new AMap.ToolBar();
        // 比例尺
        const scale = new AMap.Scale();
        // 定位
        const geolocation = new AMap.Geolocation({
          enableHighAccuracy: true, //是否使用高精度定位,默認(rèn):true
          timeout: 10000, //超過10秒后停止定位,默認(rèn):5s
          position: 'RT', //定位按鈕的??课恢?
          buttonOffset: new AMap.Pixel(10, 20), //定位按鈕與設(shè)置的停靠位置的偏移量,默認(rèn):Pixel(10, 20)
          zoomToAccuracy: true, //定位成功后是否自動(dòng)調(diào)整地圖視野到定位點(diǎn)
        });
        geocoder = new AMap.Geocoder({
          city: '全國',
        });
        map.value.addControl(geolocation);
        map.value.addControl(toolbar);
        map.value.addControl(scale);
        placeSearch = new AMap.PlaceSearch({
          // map: map.value,
          city: '全國',
          pageSize: 10, // 單頁顯示結(jié)果條數(shù)
          pageIndex: 1, // 頁碼
          citylimit: false, // 是否強(qiáng)制限制在設(shè)置的城市內(nèi)搜索
          autoFitView: true,
        });
      });
  }).catch(e=>{
    console.log(e);
  })
}
initMap();

五、完整示例

Index.html

<!DOCTYPE html>
<html lang="zh_CN" id="htmlRoot">
<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="renderer" content="webkit" />
  <meta name="viewport"
    content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0" />
  <title>
    <%= title %>
  </title>
  <script type="text/javascript">
    window._AMapSecurityConfig = {
      securityJsCode: '', // 你的密鑰
    }
  </script>
</head>
<body>
  <div id="app">
  </div>
</body>
</html>

Map.vue

<template>
  <div class="home">
    <div id="map-box"></div>
    <div class="info-box">
      <a-select
        v-model:value="keyword"
        show-search
        placeholder="輸入關(guān)鍵字搜索"
        style="width: 300px"
        :default-active-first-option="false"
        :show-arrow="false"
        :filter-option="false"
        :not-found-content="null"
        @search="handleSearch"
      >
        <a-select-option v-for="item in data" :key="item.id" :value="item.id" @click="handleSelect(item)">
          <div class="d-flex flex-column">
            <span>{{item.name}}</span>
            <span style="font-size: '10px'; color: #999999">{{item.address}}</span>
          </div>
        </a-select-option>
      </a-select>
      <a-tooltip>
        <template #title v-if="coord">點(diǎn)擊復(fù)制</template>
        <span style="margin: 5px 8px;">
          經(jīng)緯度:<span class="copy" style="cursor: pointer;" :data-clipboard-text="coord">{{coord}}</span>
        </span>
      </a-tooltip>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { shallowRef, ref, onBeforeUnmount } from 'vue';
import AMapLoader from "@amap/amap-jsapi-loader";
import { message } from 'ant-design-vue';
import ClipboardJS from 'clipboard';
const clipboard = new ClipboardJS('.copy');
clipboard.on('success', function(e) {
  console.log(e);
  console.info('Text:', e.text);
  message.info('復(fù)制成功');
  e.clearSelection();
});
clipboard.on('error', function(e) {
  if(!e.text) message.error('暫無可復(fù)制的內(nèi)容')
});
onBeforeUnmount(() => {
  clipboard.destroy();
})
const map = shallowRef(null);
const keyword = ref(null);
const data = ref([]);
const coord = ref('');
let AMapObj, placeSearch, marker, geocoder;
function initMap(){
  AMapLoader.load({
    key: '',  //設(shè)置您的key
    version: "2.0",
    plugins: ['AMap.ToolBar','AMap.Driving'],
    AMapUI: {
      version: "1.1",
      plugins: [],
    },
    Loca:{
      version: "2.0.0"
    },
  }).then((AMap)=>{
    // console.log(AMap);
    AMapObj = AMap;
    map.value = new AMap.Map("map-box",{
      viewMode:"3D",
      zoom:10,
      zooms:[2,22],
      center: [105.602725,37.076636],
    });
    map.value.on('click', onMapClick);
    AMap.plugin(
      ['AMap.ToolBar','AMap.Scale','AMap.Geolocation','AMap.PlaceSearch',
        'AMap.Geocoder','AMap.AutoComplete'],
      () => {
        // 縮放條
        const toolbar = new AMap.ToolBar();
        // 比例尺
        const scale = new AMap.Scale();
        // 定位
        const geolocation = new AMap.Geolocation({
          enableHighAccuracy: true, // 是否使用高精度定位,默認(rèn):true
          timeout: 10000, // 超過10秒后停止定位,默認(rèn):5s
          position: 'RT', // 定位按鈕的??课恢?
          buttonOffset: new AMap.Pixel(10, 20), // 定位按鈕與設(shè)置的??课恢玫钠屏?,默認(rèn):Pixel(10, 20)
          zoomToAccuracy: true, // 定位成功后是否自動(dòng)調(diào)整地圖視野到定位點(diǎn)
        });
        geocoder = new AMap.Geocoder({
          city: '全國',
        });
        map.value.addControl(geolocation);
        map.value.addControl(toolbar);
        map.value.addControl(scale);
        placeSearch = new AMap.PlaceSearch({
          city: '全國',
          pageSize: 10, // 單頁顯示結(jié)果條數(shù)
          pageIndex: 1, // 頁碼
          citylimit: false, // 是否強(qiáng)制限制在設(shè)置的城市內(nèi)搜索
          autoFitView: true,
        });
      });
  }).catch(e=>{
    console.log(e);
  })
}
// 搜索地圖
function handleSearch(str) {
  placeSearch.search(str, (status, result) => {
    console.log(result);
    if (result && typeof result === 'object' && result.poiList) {
      const list = result.poiList.pois;
      list.forEach(item => {
        item.value = item.name;
        item.label = item.name;
      });
      data.value = list
    }
  });
}
// 點(diǎn)擊地圖
function onMapClick(e) {
  coord.value = e.lnglat.lng + ',' + e.lnglat.lat
  geocoder.getAddress([e.lnglat.lng, e.lnglat.lat], function(status, result) {
    if (status === 'complete' && result.info === 'OK') {
      // result為對應(yīng)的地理位置詳細(xì)信息
      keyword.value = result.regeocode.formattedAddress
    }
  })
  drawMarker(e.lnglat)
}
// 點(diǎn)擊搜索項(xiàng)
function handleSelect(item) {
  console.log(item);
  drawMarker(item.location)
  if (item.location != null) {
    coord.value = item.location.lng + ',' + item.location.lat
  }
}
// 繪制地點(diǎn)marker
function drawMarker(location) {
  if (location == null) return
  let longitude = location.lng, latitude = location.lat
  if (marker) {
    marker.setMap(null);
  }
  marker = new AMapObj.Marker({
    position: new AMapObj.LngLat(longitude, latitude),
    anchor: 'bottom-center',
  });
  marker.on('click', () => {
    coord.value = location;
  })
  map.value.add(marker);
  map.value.setZoomAndCenter(16, [longitude, latitude]);
}
initMap();
</script>
<style lang="less" scoped>
.home{
  height: 500px;
  width: 100%;
  padding: 0px;
  margin: 0px;
  position: relative;
  .info-box {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 300px;
    background-color: #1f1f1f;
    display: flex;
    flex-direction: column;
  }
}
#map-box{
  height: 100%;
  width: 100%;
  padding: 0px;
  margin: 0px;
}
</style>
<style scoped>
:deep() .amap-logo {
  display: none !important;
}
:deep() .amap-copyright {
  display: none !important;
}
</style>

注:clipboard一鍵復(fù)制的詳細(xì)使用方法參考地址

到此這篇關(guān)于Vue 使用高德地圖添加點(diǎn)標(biāo)記 + 點(diǎn)擊地圖獲取坐標(biāo) + 帶搜索(即地理編碼 + 逆地理編碼) 附完整示例的文章就介紹到這了,更多相關(guān)vue使用高德地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

金堂县| 乐至县| 扶绥县| 临湘市| 南召县| 盐津县| 丹阳市| 栾川县| 新丰县| 轮台县| 务川| 右玉县| 新乡县| 宜兰县| 石家庄市| 汤阴县| 科技| 双城市| 合肥市| 汉源县| 玛多县| 萨迦县| 钟祥市| 呼伦贝尔市| 香格里拉县| 甘谷县| 陆良县| 虞城县| 葫芦岛市| 新泰市| 晋州市| 门源| 左贡县| 夏津县| 大关县| 宜良县| 沁阳市| 永平县| 光泽县| 宁晋县| 宁远县|