前端vue如何使用高德地圖
首先,注冊Key
1、注冊開發(fā)者賬號,成為高德開放平臺開發(fā)者
2、登陸之后,在進入「應用管理」 頁面「創(chuàng)建新應用」
3、為應用添加 Key,「服務平臺」一項請選擇「 Web 端 ( JSAPI ) 」
然后,書寫代碼
在vuecli public文件夾中的index.html添加導入 JS API 的入口腳本標簽
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <link rel="icon" href="./iconLink.png" rel="external nofollow" > <title>高德地圖</title> <!-- 建議將導入的script寫在body前面,以便提前加載 --> <script type="text/javascript" src="http://webapi.amap.com/maps?v=2.0&key=d6c35bb2619f107f86ccda4b378415f6&plugin=AMap.MouseTool"></script> <!-- 只是我的key,可以暫時用為測試(不定什么時候失效) --> <script src="http://webapi.amap.com/ui/1.1/main.js?v=1.1.1"></script> <!-- 高德官方文檔的ui庫 --> </head> <body> <div id="app"></div> </body> </html>
在vue.config.js中修改配置
module.exports = { publicPath: "./", configureWebpack: { externals: { AMap: 'window.AMap', AMapUI: 'window.AMapUI' // 高德地圖配置 }, } };
接下來,正式書寫vue組件
廢話不多說,直接上代碼
<template> <div class="map_wrapper"> <div class="box"> <div id="container" style="width: 100%; height: 500px"></div> </div> </div> </template> <script lang="ts"> import { Vue, Component, Prop, Watch } from 'vue-property-decorator'; @Component({ }) export default class page extends Vue { // 使用 import AMap from 'amap'; 會有編輯器報錯Cannot find module 'amap'; 所以變通換一種寫法 protected AMap: any = (window as any).AMap; protected AMapUI: any = (window as any).AMapUI; // 如果寫在created會報錯 "Error: Map container div not exist" mounted() { let map = new this.AMap.Map('container', { center: [121.227577, 31.101471], // 中心點坐標 resizeEnable: true, // 是否監(jiān)控地圖容器尺寸變化 zoom: 10, // 初始化地圖層級,可以理解為縮放比例 showMarker: true, // 定位成功后在定位到的位置顯示點標記,默認:true }); //加載SimpleInfoWindow,loadUI的路徑參數(shù)為模塊名中 'ui/' 之后的部分 this.AMapUI.loadUI(['overlay/SimpleInfoWindow'], (SimpleInfoWindow: any) => { let marker = new this.AMap.Marker({ map: map, zIndex: 9999999, position: map.getCenter(), }); let infoWindow = new SimpleInfoWindow({ infoTitle: '<strong>這里是標題</strong>', infoBody: '<p>這里是內(nèi)容。</p>', offset: new this.AMap.Pixel(0, -31), // 文本定位偏移 }); //顯示在map上 function openInfoWin() { infoWindow.open(map, marker.getPosition()); } marker.on('click', () => { openInfoWin(); // 點擊標記時顯示文本 }); openInfoWin(); }); } } </script> <style scoped lang="scss"></style>
頁面效果如下
以上就是前端vue如何使用高德地圖的詳細內(nèi)容,更多關于vue 高德地圖的資料請關注腳本之家其它相關文章!
相關文章
vue3?Error:Unknown?variable?dynamic?import:?../views/的解
這篇文章主要給大家介紹了關于vue3?Error:Unknown?variable?dynamic?import:?../views/的解決方案,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下2023-07-07vue2.0中自適應echarts圖表、全屏插件screenfull的使用
這篇文章主要介紹了vue2.0中自適應echarts圖表、全屏插件screenfull的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08詳解從零搭建 vue2 vue-router2 webpack3 工程
本篇文章主要介紹了詳解從零搭建 vue2 vue-router2 webpack3 工程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11詳解Vue開發(fā)網(wǎng)站seo優(yōu)化方法
這篇文章主要介紹了Vue開發(fā)網(wǎng)站seo優(yōu)化方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05vue?el-table中使用el-select選中后無效的解決
這篇文章主要介紹了vue?el-table中使用el-select選中后無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08