vue调用高德地图实例代码

作者:starWind 时间:2024-06-07 15:20:47 

一. vue-amap,一个基于 Vue 2.x 和高德地图的地图组件 https://elemefe.github.io/vue-amap/#/

这个就不细说了,按照其文档,就能够安装下来。

二. 按照官方提供的方法引入

1.修改webpac.base.conf.js文件


externals: {
 'AMap': 'AMap'
}

2.引入sdk

引入有两种方式,一种是页面直接引入


<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=您申请的key值"></script>

还有一种是异步加载


<script src="http://webapi.amap.com/maps?v=1.3&amp;key=您申请的key值&callback=init"></script>
<script>
 function init(){
   var map = new AMap.Map('container', {
     center: [117.000923, 36.675807],
     zoom: 6
   });
   map.plugin(["AMap.ToolBar"], function() {
     map.addControl(new AMap.ToolBar());
   });
 }
</script>

需要注意的是:

你也可以去动态去创造,例如这样 


var script = document.createElement('script')
script.type = 'text/javascript'
script.src = 'https://webapi.amap.com/maps?v=1.3&key=yourKey'  // 高德地图
document.body.appendChild(script)

不管是采用哪种方式,都要保证你想要加载地图的js文件,在引入的sdk之后

vue调用高德地图实例代码

这样,在第三步的时候,才不会报错

 三. 在当前需要加载vue页面引入


import AMap from 'AMap'

四. 页面实例

这是初始化地图,并且调用插件的代码(map.vue)如:


<template>
<div>
 <div id="container" style="width:500px; height:300px"></div>
</div>
</template>
<script>
import AMap from 'AMap'
var map
export default {
 mounted: function () {
  this.init()
 },
 methods: {
  init: function () {
   map = new AMap.Map('container', {
    center: [116.397428, 39.90923],
    resizeEnable: true,
    zoom: 10
   })
   AMap.plugin(['AMap.ToolBar', 'AMap.Scale'], function () {
    map.addControl(new AMap.ToolBar())
    map.addControl(new AMap.Scale())
   })
  }
 }
}
</script>
<style>
</style>

效果如图:

vue调用高德地图实例代码

来源:http://www.cnblogs.com/star-wind/p/6774204.html

标签:vue,高德地图
0
投稿

猜你喜欢

  • sql server 锁表语句分享

    2012-02-12 15:49:20
  • python实现趣味图片字符化

    2022-01-22 15:29:35
  • Golang 正则匹配效率详解

    2024-01-30 03:41:47
  • Python的Flask框架中@app.route的用法教程

    2022-05-14 07:25:19
  • jQuery.data()方法与内存泄漏

    2010-04-06 17:20:00
  • Python3利用SMTP协议发送E-mail电子邮件的方法

    2023-10-12 17:39:32
  • Java连接数据库oracle中文乱码解决方案

    2024-01-19 02:48:10
  • pandas删除部分数据后重新生成索引的实现

    2023-11-18 04:44:13
  • 解决idea打开窗口/tab过多导致隐藏的问题

    2022-12-29 10:45:42
  • php中关于hook钩子函数底层理解

    2023-06-12 06:49:55
  • 比较SQL Server与Oracle、DB2三种数据库

    2008-09-12 17:24:00
  • JavaScript中clientWidth,offsetWidth,scrollWidth的区别

    2024-04-22 22:24:59
  • Qt5 实现主窗口状态栏显示时间

    2022-05-29 23:54:45
  • 通过Cursor工具使用GPT-4的方法详解

    2023-08-28 05:08:34
  • 在Win2003 64位下ASP无法连接Access数据库

    2011-03-30 11:22:00
  • 将MySQL数据库移植为PostgreSQL

    2024-01-21 22:20:09
  • Python 数据库操作 SQLAlchemy的示例代码

    2024-01-28 04:42:28
  • 十万条Access数据表分页的两个解决方法

    2008-05-23 18:24:00
  • 总结Python函数参数的六种类型

    2021-12-21 02:55:56
  • python机器学习实战之最近邻kNN分类器

    2021-11-11 14:55:52
  • asp之家 网络编程 m.aspxhome.com