首先    npm install -S vue-amap

  1.  
  2. 然后在 main.js
  1. import VueAMap from 'vue-amap'; //注意不要和 AMap原始名称覆盖
  2. Vue.use(VueAMap);
  3. // 初始化vue-amap
  4. VueAMap.initAMapApiLoader({
  5. // 高德的key
  6. key: 'you key',
  7. // 插件集合
  8. plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor','AMap.Geolocation'],
  9. v: '1.4.4'
  10.  
  11. });

 

 map.vue文件

其中有个BUS.js,是基于观察者模式的发布订阅封装

  1. <template>
  2. <div class="_map">
  3. <div class="amap-page-container">
  4. <el-amap-search-box class="search-box" :search-option="searchOption" :on-search-result="onSearchResult" ></el-amap-search-box>
  5. <el-amap ref="map" vid="amapDemo" :plugin="plugin" :zoom="zoom" :center="center" class="amap-demo" :events="events">
  6. <el-amap-marker vid="component-marker" :position="makerConf.position" :content="makerConf.content" ></el-amap-marker>
  7. </el-amap>
  8. </div>
  9. <div class="adrs">
  10. <ul>
  11. <li class="" v-for="(item,index) in list" :key="index" :class="currIndex == index ? 'active':''" @click="select(item,index)">
  12. <p class="address">{{item.address}}</p>
  13. <p class="nm">{{item.name}}</p>
  14. </li>
  15. </ul>
  16. </div>
  17. </div>
  18. </template>
  19.  
  20. <style>
  21. .amap-page-container{
  22. height: 300px;
  23. position: relative;
  24. }
  25. .search-box {
  26. position: absolute !important;
  27. top: 25px;
  28. left: 20px;
  29. z-index: 200 !important;
  30. }
  31. .amap-demo {
  32. height: 300px;
  33. }
  34. .amap-logo {
  35. display: none;
  36. }
  37. .amap-copyright {
  38. bottom:-100px;
  39. display: none;
  40. }
  41. .amap-scalecontrol{
  42. bottom: 4px !important;
  43. }
  44. .amap-geolocation-con{
  45. bottom: 30px !important;
  46. z-index: 199 !important;
  47. }
  48. ul li.active{
  49. color: deeppink;
  50. }
  51. </style>
  52.  
  53. <script>
  54.  
  55. export default {
  56. name: 'amap-page',
  57. components: {},
  58. data() {
  59. var me = this;
  60. me.city = me.city || '武汉';
  61. return {
  62. list:[],
  63. currIndex:0,
  64. zoom: 16,
  65. center: [114.397169, 30.50576],
  66. events:{
  67. init: (o) => {
  68. o.setCity(me.city,result => {
  69. console.log("----------setCity",result);
  70. if(result && result.length > 0){
  71. me.zoom = 16;
  72. me.makerConf.position = result;
  73. me.getList(result);
  74. }
  75. });
  76. //去掉logo
  77. document.getElementsByClassName("amap-logo")[0].style.display = "none";
  78. },
  79. "dragend":function(e){
  80. //console.log("dragging",e,this.getCenter());
  81. var point = this.getCenter();
  82. var pos = [point.lng,point.lat];
  83. me.makerConf.position = [point.lng,point.lat];
  84. me.getList(pos);
  85. }
  86. },
  87. makerConf: {
  88. position: [114.397169, 30.50576],
  89. content:""
  90. },
  91. searchOption: {
  92. city: me.city,
  93. citylimit: true
  94. },
  95. plugin:[
  96. 'ToolBar',
  97. 'Scale',
  98. {
  99. pName: 'Geolocation',
  100. events: {
  101. init(o) {
  102.  
  103. },
  104. complete:function(result){
  105. //定位成功
  106. var address = result.formattedAddress
  107. var point = result.position;
  108. var obj = {
  109. address:address,
  110. name:"",
  111. location:point
  112. }
  113. me.list = [obj];
  114. me.makerConf.position = [point.lng,point.lat];
  115. },
  116. error:function(){
  117.  
  118. }
  119. }
  120. }
  121. ]
  122. };
  123. },
  124. created(){
  125. var me = this;
  126.  
  127. },
  128. mounted(){
  129.  
  130. },
  131. methods: {
  132. select:function(item,index){
  133. var me = this;
  134. me.currIndex = index;
  135. var point = item.location;
  136. me.makerConf.position = [point.lng,point.lat];
  137. me.center = [point.lng,point.lat];
  138.  
  139. },
  140. //this.$refs.map.$$getCenter()
  141. getList:function(result){
  142. //获取列表
  143. var me = this;
  144. me.$Geocoder({
  145. lnglatXY:result,
  146. success:function(res){
  147. if(res.regeocode && res.regeocode.pois){
  148. me.list = res.regeocode.pois;
  149. }else{
  150. me.list = [];
  151. }
  152. },
  153. error:function(res){
  154. me.list = [];
  155. }
  156. });
  157.  
  158. },
  159. onSearchResult(pois) {
  160. //搜索
  161. let latSum = 0;
  162. let lngSum = 0;
  163. var me = this;
  164.  
  165. var mymap = me.$refs.map.$$getInstance();
  166.  
  167. if (pois && pois.length > 0) {
  168.  
  169. //如果长度为1则无需转化
  170. var poi = pois[0];
  171. var lng = poi["lng"];
  172. var lat = poi["lat"];
  173. me.center = [lng, lat];
  174. me.makerConf.position = [lng, lat];
  175. //me.makerConf.content = poi.name;
  176. me.list = pois;
  177. }else{
  178. me.list = [];
  179. }
  180. },
  181.  
  182. $Geocoder(options){
  183. //将坐标点转化为,详细地址
  184. options = options || {};
  185. if(AMap){
  186. AMap.plugin(['AMap.Geocoder'], () => {
  187. const geocoder = new AMap.Geocoder({
  188. radius: options.radius || 1000,
  189. extensions: options.extensions || "all"
  190. })
  191. var lnglatXY = options.lnglatXY || [114.397169, 30.50576]; //已知点坐标
  192. geocoder.getAddress(lnglatXY, function(status, result) {
  193. if (status === 'complete' && result.info === 'OK') {
  194. options.success && options.success(result);
  195. }else{
  196. options.error && options.error(status,result);
  197. }
  198. });
  199. });
  200.  
  201. }
  202.  
  203. }
  204. },
  205. "watch":{
  206. list:function(){
  207. this.currIndex = 0;
  208. }
  209. }
  210.  
  211. };
  212.  
  213. /*
  214. me.$Geocoder({
  215. lnglatXY:[center.lng, center.lat],
  216. success:function(res){
  217. console.log(res);
  218. }
  219. });
  220. *
  221. * */
  222. </script>

  

 

  bus.js

  1. let instance = null;
  2.  
  3. class EventBus {
  4. constructor() {
  5. if (!instance) {
  6. this.events = {};
  7. instance = this;
  8. }
  9. return instance;
  10. }
  11.  
  12. $emit(event, message) {
  13. if (!this.events[event])
  14. return;
  15.  
  16. const callbacks = this.events[event];
  17.  
  18. for (let i = 0, l = callbacks.length; i < l; i++) {
  19. const callback = callbacks[i];
  20.  
  21. callback.call(this, message);
  22. }
  23. }
  24.  
  25. $on(event, callback) {
  26. if (!this.events[event])
  27. this.events[event] = [];
  28.  
  29. this.events[event].push(callback);
  30. }
  31. }
  32.  
  33. export default new EventBus();

  

效果图

相关文档地址: https://elemefe.github.io/vue-amap/#/zh-cn/introduction/install

vue 使用高德地图vue-amap组件的更多相关文章

  1. 基于vue 2.X和高德地图的vue-amap组件获取经纬度

    今天我就讲了一下怎么通过vue和高德地图开发的vue-amap组件来获取经纬度. 这是vue-amap的官网文档:https://elemefe.github.io/vue-amap/#/ 这是我的码 ...

  2. vue调用高德地图:vue-amap

    前言:之前没有接触过页面调用地图的项目,某次面试,老板要求我用vue-amap调用高德地图,回家以后,我去网上查了一些案例和教程,看似很简单的引入调用,我却整整弄了一宿,还没弄出来!!!百般无奈之下, ...

  3. vue集成高德地图

    vue集成高德地图 前言 二.使用步骤 1.注册高德开发平台 2.vue 结尾 前言 之前玩Thymeleaf的时候玩过高德地图,现在无聊Vue项目也整个地图进去~ 二.使用步骤 1.注册高德开发平台 ...

  4. Android 编程 AMapLocationClientOption 类中的 setNeedAddress 方法用处 (高德地图 com.amap.api.location.AMapLocationClientOption 中的类)

    最近在用高德地图来写Android App, 其中有一些 方法是不太理解的,这里写一下 对  高德地图  com.amap.api.location.AMapLocationClientOption ...

  5. VUE 高德地图选取地址组件开发

    高德地图文档地址 http://lbs.amap.com/api/lightmap/guide/picker/ 结合步骤: 1.通过iframe内嵌引入高德地图组件 key就选你自己申请的key &l ...

  6. VUE 2.0 引入高德地图,自行封装组件

    1. 高德地图官网 申请帐号, 申请相应(JavaScript API)的 Key 2. 在项目中引入, 这里和其他的引入不同的是 直接在 index.html, 不是在 main.js 引入, 博主 ...

  7. vue+vant ui+高德地图的选址组件

    首先在index.html引入高德地图的js <script src="https://webapi.amap.com/maps?v=1.4.14&key=你的key" ...

  8. vue 调用高德地图

    一. vue-amap,一个基于 Vue 2.x 和高德地图的地图组件 https://elemefe.github.io/vue-amap/#/ 这个就不细说了,按照其文档,就能够安装下来. 二. ...

  9. 前端vue使用高德地图

    首先,注册Key 1.注册开发者账号,成为高德开放平台开发者 2.登陆之后,在进入「应用管理」 页面「创建新应用」 3.为应用添加 Key,「服务平台」一项请选择「 Web 端 ( JSAPI ) 」 ...

  10. Android 编程 AMapLocationClientOption 类中的 setMockEnable (高德地图 com.amap.api.location.AMapLocationClientOption 中的类)

    setMockEnable 高德地图中 AMapLocationClientOption 中有一个方法是设置APP是否接受模拟定位的设置,就是方法 setMockEnable //设置是否允许模拟位置 ...

随机推荐

  1. Diagnostics: File file:/tmp/spark-***/__spark_libs__***.zip does not exist

    Diagnostics: File file:/tmp/spark-c03df206-c90e-4c97-a2d6-a5d3fdb17811/__spark_libs__303213348409500 ...

  2. 修改Jenkins的主目录步骤

    在使用Jenkins做持续集成过程中,在构建很多次后发现有时在构建的时候系统提示磁盘空间不足,此时检查发现Jenkins的主目录挂载区放在了服务器根目录下,占用空间较大,此时除了对服务器的磁盘进行扩容 ...

  3. Flask web开发之路十四

    今天开始Flask的实战,创建一个项目,实现包括用户登录.注册.注销.发表博客.评论以及检索等功能 首先给出项目结构: 1.config.py文件: 存放各种配置信息 import os # dial ...

  4. [No0000101]JavaScript-基础课程1

    JavaScript 是一种轻量级的编程语言,很容易学习,同时也是一种被广泛用于客户端Web开发的脚本语言.通过本课程学习,我们可以了解到JavaScript的基本语法知识,以及怎样使用它去创建简单的 ...

  5. mac休眠掉电快,更改休眠模式

    打开终端输入: $ pmset -g 查看休眠模式 hibernatemode 发现值为3, 这是大多数的设置,如果为0 ,那么休眠时严重掉电, 我们可以改变这个模式: $ sudo pmset -a ...

  6. day4:数据结构list

    1,一直输入用户名,输入Q退出,注意用户的输入别忘了加strip,和upper不区分大小写,list最后一位添加append li = [] while 1: name = input("& ...

  7. 分析java的堆栈信息 内存模型

    package com.test.learnJava; public class LineNum { public static void main(String[] args) { System.o ...

  8. hyperledge vagrant docker development environment

    https://blog.csdn.net/zgljl2012/article/details/52896372

  9. [daily][archlinux][rsync] rsync

    科普文档:https://wiki.archlinux.org/index.php/Rsync 之前改文件系统时,用过. 然而用的不太对,导致一部分文件的权限出了问题. [troubleshoot][ ...

  10. ORACLE之PACKAGE-包、存储过程、函数

    1,简单的包. 创建包规范: create or replace package pack_test1 is -- 定义过程1 procedure p_test1(p_1 in varchar2); ...