vue2 使用高德地图JSAPI

成为开发者并创建 key

为了正常调用 API ,请先注册成为高德开放平台开发者,并申请 web 平台(JS API)的 key 和安全密钥
高德开放平台控制台
操作步骤链接网址

NPM 安装 Loader

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

配置安全秘钥
注意:这个设置必须是在 JSAPI 的脚本加载之前进行设置,否则设置无效。所以安全密钥在入口的 html 文件中引入,页面中正常使用key值即可。
在这里插入图片描述

<!DOCTYPE html>
<html lang="">
  <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="<%= BASE_URL %>favicon.ico">
    <title><%= htmlWebpackPlugin.options.title %></title>
	<!-- 高德安全秘钥 -->
	<!-- 高德地图秘钥,必须在加载load.js文件之前 -->
	<script type="text/javascript">
	        window._AMapSecurityConfig = {
	            securityJsCode:'您申请的安全密钥',
	        }
	</script>
  </head>
  <body>
    <noscript>
      <strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
    </noscript>
    <div id="app"></div>
    <!-- built files will be auto injected -->
  </body>
</html>

引入JS API Loader

import AMapLoader from '@amap/amap-jsapi-loader';

完整的代码vue

<template>
    <div class="home_div">
        <div class="map_title">
            <h3>JSAPI Vue2地图组件示例</h3>
        </div>
        <div id="container"></div>
		<div id="panel" style='position: absolute; right: 10px;top: 10px;height: 480px;width: 300px;z-index: 100;overflow: auto;'></div>
    </div>
</template>
<script>
import AMapLoader from '@amap/amap-jsapi-loader';

export default {
    name: "Mapview",
    data() {
        return {
            //map:null,
        }
    },
    created() {

    },
    mounted() {
        this.initAMap();
    },
    methods: {

        initAMap() {
            AMapLoader.load({
                key: '你申请的key',  //设置您的key
                version: "2.0", //指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
                plugins: ['AMap.ToolBar', 'AMap.Driving'],//需要使用的的插件列表,如比例尺'AMap.Scale'等
                AMapUI: {
                    version: "1.1",
                    plugins: [],

                },
                Loca: {
                    version: "2.0"  //数据可视化
                },
            }).then((AMap) => {
                this.map = new AMap.Map("container", {
					 viewMode: '2D',  // 默认使用 2D 模式
                      zoom:5,  //初始化地图层级
                      center: [116.397428, 39.90923]  //初始化地图中心点
                });
				
				  this.driving = new AMap.Driving({
				     // 驾车路线规划策略,AMap.DrivingPolicy.LEAST_TIME是最快捷模式
				         policy: AMap.DrivingPolicy.LEAST_TIME,
				         // map 指定将路线规划方案绘制到对应的AMap.Map对象上
				         map: this.map,
				         // panel 指定将结构化的路线详情数据显示的对应的DOM上,传入值需是DOM的ID
				         panel: 'panel'
				  })
			
				  this.points = [
				    { keyword: '北京市地震局(公交站)',city:'北京' },
				      { keyword: '陆家嘴',city:'上海' }
				  ]
				
				  this.driving.search(this.points, function (status, result) {
				    console.log(status, result);
				  })
				
            }).catch(e => {
                console.log(e);
            })
        },
    }


}
</script>
<style  scoped>
.home_div {
    padding: 0px;
    margin: 0px;
    width: 100%;
    height: 100%;
    position: relative;
}

#container {
    padding: 0px;
    margin: 0px;
    width: 100%;
    height: 100%;
    position: absolute;
}

.map_title {
    position: absolute;
    z-index: 1;
    width: 100%;
    height: 50px;
    background-color: rgba(27, 25, 27, 0.884);

}

h3 {
    position: absolute;
    left: 10px;
    z-index: 2;
    color: white;
}
</style>

注意:高德地图在2021.12月以后新申请的key必须要搭配安全密钥一起使用
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果在Vue项目中使用高德地图时遇到了“AMap.Driving is not a constructor”错误,这可能是因为您未正确引入Driving插件或引入方式不正确。 在Vue项目中使用高德地图,可以通过在main.js文件中引入高德地图API并注册为全局变量来实现全局使用。例如: ```javascript // main.js import Vue from 'vue' import App from './App.vue' // 引入高德地图API import AMap from 'AMap' Vue.config.productionTip = false // 注册高德地图API为全局变量 Vue.prototype.AMap = AMap new Vue({ render: h => h(App), }).$mount('#app') ``` 在上面的代码中,我们引入了高德地图API,并通过Vue.prototype将其注册为Vue的全局变量,使其在所有组件中可用。 然后,您可以在Vue组件中使用AMap对象和Driving类来计算路线并绘制路径。例如: ```javascript <template> <div id="mapContainer" style="height:500px"></div> </template> <script> export default { data() { return { map: null, driving: null } }, mounted() { // 初始化地图 this.map = new this.AMap.Map('mapContainer', { center: [116.397428, 39.90923], zoom: 13 }); // 创建Driving对象 this.driving = new this.AMap.Driving({ map: this.map, panel: 'panel' }); // 计算路线 var points = [ [116.379028, 39.865042], [116.414032, 39.865042], [116.414032, 39.899042], [116.379028, 39.899042] ]; this.driving.search(points, (status, result) => { if (status === 'complete' && result.info === 'OK') { // 获取路线经纬度坐标数组 var path = result.routes[0].path; // 创建Polyline对象绘制路径曲线 var polyline = new this.AMap.Polyline({ path: path, strokeColor: '#3366FF', strokeWeight: 5, strokeOpacity: 0.8 }); polyline.setMap(this.map); } else { alert('路线计算失败'); } }); } } </script> ``` 在上面的代码中,我们在Vue组件的mounted生命周期函数中创建了地图和Driving对象,并计算了路线。请注意,我们使用Vue.prototype.AMap和this.AMap来访问全局的AMap对象,并使用this.AMap.Driving来访问Driving类。这样,您就可以在Vue项目中使用高德地图API,而不必担心“AMap.Driving is not a constructor”错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时光浅止

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值