Vue高德地圖

一、高德地圖介紹

高德地圖是中國領先的數字地圖內容、導航和位置服務解決方案提供商,其提供了各種地圖服務,如:定位,搜索,導航,交通等。高德地圖的主要目標是為用戶提供實用、智能、人性化的信息服務,成為人們移動出行生活的最佳夥伴。

二、Vue.js簡介

Vue.js是一個流行的漸進式Javascript框架,其核心庫在2021年4月發佈了最新的版本。Vue.js使用單文件組件風格,將模板、腳本和樣式都寫在一個文件中,使代碼更清晰和維護起來更容易。Vue.js還提供了許多實用的指令和插件,如v-bind、v-model等,使Vue.js非常適合構建複雜的單頁應用程序(SPA)。

三、Vue高德地圖使用

1、安裝

首先需要安裝高德地圖的Javascript API以及Vue.js官方提供的VueAMap插件:

npm install vue-amap amap-jsapi-loader --save

2、註冊插件

在main.js文件中引入VueAMap插件,並註冊到Vue實例中:

// main.js

import Vue from 'vue'
import VueAMap from 'vue-amap'

Vue.use(VueAMap)

VueAMap.initAMapApiLoader({
  // 高德的key
  key: 'your amap key',
  // 插件集合
  plugin: [
    'AMap.Geolocation',
    'AMap.Marker',
    'AMap.Walking',
    'AMap.MapType',
    'AMap.PolyEditor'
  ],
  // 高德 sdk 版本,默認為 1.4.15
  v: '1.4.15',
  uiVersion: '1.0.11' // UI庫版本號
})

3、創建地圖組件

在Vue組件中添加高德地圖,使用VueAMap提供的`amp-map`標籤,並設置各種屬性來自定義地圖實例:

<template>
  <div>
    <amap-map :zoom="13">
      </amap-map>
  </div>
</template>

<script>
export default {
  name: 'MapDemo'
}
</script>

4、標記和信息窗體

創建一個標記對象,並為其添加事件處理程序來顯示信息窗口:

<template>
  <div>
    <amap-map :zoom="13">
      <amap-marker :position="markerPosition" @click="showInfoWindow"/>
      <amap-info-window :position="infoWindowPosition">
        <div>{{infoWindowText}}</div>
      </amap-info-window>
    </amap-map>
  </div>
</template>

<script>
export default {
  name: 'MapDemo',
  data () {
    return {
      markerPosition: [121.484170, 31.233385],
      infoWindowPosition: [121.484170, 31.233385],
      infoWindowText: '信息窗體內容'
    }
  },
  methods: {
    showInfoWindow () {
      this.$refs.infoWindow.open(this.markerPosition)
    }
  }
}
</script>

5、搜索

使用高德地圖的JavaScript API和VueAMap插件進行地址搜索:

<template>
  <div>
    <amap-map :zoom="zoom">
      <amap-marker :position="markerPosition"/>
    </amap-map>
  </div>
</template>

<script>
export default {
  name: 'MapDemo',
  data () {
    return {
      address: '上海市黃浦區南京東路',
      zoom: 13,
      markerPosition: []
    }
  },
  mounted () {
    this.searchAddress(this.address)
  },
  methods: {
    searchAddress (address) {
      this.$amap.getGeocoder().getLocation(address, (status, result) => {
        if (status === 'complete' && result.geocodes.length) {
          this.markerPosition = result.geocodes[0].location
        } else {
          alert('地址不存在')
        }
      })
    }
  }
}
</script>

6、路線規劃

使用高德地圖提供的JavaScript API和VueAMap插件進行步行和公共交通路線規劃:

<template>
  <div>
    <amap-map :zoom="zoom">
      <amap-marker :position="startPosition"></amap-marker>
      <amap-marker :position="endPosition"></amap-marker>
      <amap-polyline :path="polylinePath">
        <amap-icon :icon="walkIcon" offset="-5,-10"></amap-icon>
      </amap-polyline>
    </amap-map>
  </div>
</template>

<script>
export default {
  name: 'MapDemo',
  data () {
    return {
      zoom: 13,
      startPosition: [121.484170, 31.233385],
      endPosition: [121.537018, 31.245945],
      polylinePath: [],
      walkIcon: {
        size: new this.$amap.Size(23, 25),
        image: '//a.amap.com/jsapi_demos/static/demo-center/icons/dir-marker.png',
        imageOffset: new this.$amap.Pixel(-2, -13)
      }
    }
  },
  mounted () {
    this.walkRoutePlan()
  },
  methods: {
    walkRoutePlan () {
      const walker = new this.$amap.Walking({
        map: this.$refs.map,
        panel: '',
        // from和to同時存在時,from優先
        from: this.startPosition,
        to: this.endPosition
      })

      walker.search((status, result) => {
        if (status === 'complete' && result.routes.length) {
          const path = result.routes[0].path
          this.polylinePath = path.map((p) => [p.lng, p.lat])
          this.$refs.map.setFitView()
        }
      })
    }
  }
}
</script>

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/291092.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-24 13:14
下一篇 2024-12-24 13:14

相關推薦

  • 使用Vue實現前端AES加密並輸出為十六進制的方法

    在前端開發中,數據傳輸的安全性問題十分重要,其中一種保護數據安全的方式是加密。本文將會介紹如何使用Vue框架實現前端AES加密並將加密結果輸出為十六進制。 一、AES加密介紹 AE…

    編程 2025-04-29
  • 用Python畫疫情地圖

    COVID-19疫情在全世界範圍內肆虐了數月,為了讓人們了解當前疫情的最新情況,很多技術人員都開始使用數據可視化的手段展示疫情數據。其中一個重要的展示形式就是利用Python編程語…

    編程 2025-04-29
  • Echarts 地圖 Label 增加背景圖

    本文將從多個方面對 Echarts 地圖 Label 增加背景圖進行詳細的闡述。 一、背景圖的作用 為 Echarts 地圖添加背景圖可以使 Label 更加直觀、美觀,提升視覺效…

    編程 2025-04-29
  • Vue TS工程結構用法介紹

    在本篇文章中,我們將從多個方面對Vue TS工程結構進行詳細的闡述,涵蓋文件結構、路由配置、組件間通訊、狀態管理等內容,並給出對應的代碼示例。 一、文件結構 一個好的文件結構可以極…

    編程 2025-04-29
  • Vue3的vue-resource使用教程

    本文將從以下幾個方面詳細闡述Vue3如何使用vue-resource。 一、安裝Vue3和vue-resource 在使用vue-resource前,我們需要先安裝Vue3和vue…

    編程 2025-04-27
  • Vue模擬按鍵按下

    本文將從以下幾個方面對Vue模擬按鍵按下進行詳細闡述: 一、Vue 模擬按鍵按下的場景 在前端開發中,我們常常需要模擬按鍵按下的場景,比如在表單中填寫內容後,按下「回車鍵」提交表單…

    編程 2025-04-27
  • ThinkPHP6 + Vue.js: 不使用Fetch的數據請求方法

    本文將介紹如何在ThinkPHP6和Vue.js中進行數據請求,同時避免使用Fetch函數。 一、AJAX:XMLHttpRequest的基礎使用 在進行數據請求時,最基礎的方式就…

    編程 2025-04-27
  • 開發前端程序,Vue是否足夠?

    Vue是一個輕量級,高效,漸進式的JavaScript框架,用於構建Web界面。開發人員可以使用Vue輕鬆完成前端編程,開發響應式應用程序。然而,當涉及到需要更大的生態系統,或利用…

    編程 2025-04-27
  • ECharts地圖輪播

    本文將從插件基礎、數據準備及處理、地圖呈現、輪播控制等方面,對ECharts地圖輪播進行詳細闡述。 一、插件基礎 ECharts官方提供了一個名為「echarts-gl」的插件,它…

    編程 2025-04-27
  • 如何在Vue中點擊清除SetInterval

    在Vue中點擊清除SetInterval是常見的需求之一。本文將介紹如何在Vue中進行這個操作。 一、使用setInterval和clearInterval 在Vue中,使用set…

    編程 2025-04-27

發表回復

登錄後才能評論