vue 使用高德地图vue-amap组件
首先 npm install -S vue-amap
- 然后在 main.js
- import VueAMap from 'vue-amap'; //注意不要和 AMap原始名称覆盖
- Vue.use(VueAMap);
- // 初始化vue-amap
- VueAMap.initAMapApiLoader({
- // 高德的key
- key: 'you key',
- // 插件集合
- plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor','AMap.Geolocation'],
- v: '1.4.4'
- });
map.vue文件
其中有个BUS.js,是基于观察者模式的发布订阅封装
- <template>
- <div class="_map">
- <div class="amap-page-container">
- <el-amap-search-box class="search-box" :search-option="searchOption" :on-search-result="onSearchResult" ></el-amap-search-box>
- <el-amap ref="map" vid="amapDemo" :plugin="plugin" :zoom="zoom" :center="center" class="amap-demo" :events="events">
- <el-amap-marker vid="component-marker" :position="makerConf.position" :content="makerConf.content" ></el-amap-marker>
- </el-amap>
- </div>
- <div class="adrs">
- <ul>
- <li class="" v-for="(item,index) in list" :key="index" :class="currIndex == index ? 'active':''" @click="select(item,index)">
- <p class="address">{{item.address}}</p>
- <p class="nm">{{item.name}}</p>
- </li>
- </ul>
- </div>
- </div>
- </template>
- <style>
- .amap-page-container{
- height: 300px;
- position: relative;
- }
- .search-box {
- position: absolute !important;
- top: 25px;
- left: 20px;
- z-index: 200 !important;
- }
- .amap-demo {
- height: 300px;
- }
- .amap-logo {
- display: none;
- }
- .amap-copyright {
- bottom:-100px;
- display: none;
- }
- .amap-scalecontrol{
- bottom: 4px !important;
- }
- .amap-geolocation-con{
- bottom: 30px !important;
- z-index: 199 !important;
- }
- ul li.active{
- color: deeppink;
- }
- </style>
- <script>
- export default {
- name: 'amap-page',
- components: {},
- data() {
- var me = this;
- me.city = me.city || '武汉';
- return {
- list:[],
- currIndex:0,
- zoom: 16,
- center: [114.397169, 30.50576],
- events:{
- init: (o) => {
- o.setCity(me.city,result => {
- console.log("----------setCity",result);
- if(result && result.length > 0){
- me.zoom = 16;
- me.makerConf.position = result;
- me.getList(result);
- }
- });
- //去掉logo
- document.getElementsByClassName("amap-logo")[0].style.display = "none";
- },
- "dragend":function(e){
- //console.log("dragging",e,this.getCenter());
- var point = this.getCenter();
- var pos = [point.lng,point.lat];
- me.makerConf.position = [point.lng,point.lat];
- me.getList(pos);
- }
- },
- makerConf: {
- position: [114.397169, 30.50576],
- content:""
- },
- searchOption: {
- city: me.city,
- citylimit: true
- },
- plugin:[
- 'ToolBar',
- 'Scale',
- {
- pName: 'Geolocation',
- events: {
- init(o) {
- },
- complete:function(result){
- //定位成功
- var address = result.formattedAddress
- var point = result.position;
- var obj = {
- address:address,
- name:"",
- location:point
- }
- me.list = [obj];
- me.makerConf.position = [point.lng,point.lat];
- },
- error:function(){
- }
- }
- }
- ]
- };
- },
- created(){
- var me = this;
- },
- mounted(){
- },
- methods: {
- select:function(item,index){
- var me = this;
- me.currIndex = index;
- var point = item.location;
- me.makerConf.position = [point.lng,point.lat];
- me.center = [point.lng,point.lat];
- },
- //this.$refs.map.$$getCenter()
- getList:function(result){
- //获取列表
- var me = this;
- me.$Geocoder({
- lnglatXY:result,
- success:function(res){
- if(res.regeocode && res.regeocode.pois){
- me.list = res.regeocode.pois;
- }else{
- me.list = [];
- }
- },
- error:function(res){
- me.list = [];
- }
- });
- },
- onSearchResult(pois) {
- //搜索
- let latSum = 0;
- let lngSum = 0;
- var me = this;
- var mymap = me.$refs.map.$$getInstance();
- if (pois && pois.length > 0) {
- //如果长度为1则无需转化
- var poi = pois[0];
- var lng = poi["lng"];
- var lat = poi["lat"];
- me.center = [lng, lat];
- me.makerConf.position = [lng, lat];
- //me.makerConf.content = poi.name;
- me.list = pois;
- }else{
- me.list = [];
- }
- },
- $Geocoder(options){
- //将坐标点转化为,详细地址
- options = options || {};
- if(AMap){
- AMap.plugin(['AMap.Geocoder'], () => {
- const geocoder = new AMap.Geocoder({
- radius: options.radius || 1000,
- extensions: options.extensions || "all"
- })
- var lnglatXY = options.lnglatXY || [114.397169, 30.50576]; //已知点坐标
- geocoder.getAddress(lnglatXY, function(status, result) {
- if (status === 'complete' && result.info === 'OK') {
- options.success && options.success(result);
- }else{
- options.error && options.error(status,result);
- }
- });
- });
- }
- }
- },
- "watch":{
- list:function(){
- this.currIndex = 0;
- }
- }
- };
- /*
- me.$Geocoder({
- lnglatXY:[center.lng, center.lat],
- success:function(res){
- console.log(res);
- }
- });
- *
- * */
- </script>
bus.js
- let instance = null;
- class EventBus {
- constructor() {
- if (!instance) {
- this.events = {};
- instance = this;
- }
- return instance;
- }
- $emit(event, message) {
- if (!this.events[event])
- return;
- const callbacks = this.events[event];
- for (let i = 0, l = callbacks.length; i < l; i++) {
- const callback = callbacks[i];
- callback.call(this, message);
- }
- }
- $on(event, callback) {
- if (!this.events[event])
- this.events[event] = [];
- this.events[event].push(callback);
- }
- }
- export default new EventBus();
效果图
相关文档地址: https://elemefe.github.io/vue-amap/#/zh-cn/introduction/install
vue 使用高德地图vue-amap组件的更多相关文章
- 基于vue 2.X和高德地图的vue-amap组件获取经纬度
今天我就讲了一下怎么通过vue和高德地图开发的vue-amap组件来获取经纬度. 这是vue-amap的官网文档:https://elemefe.github.io/vue-amap/#/ 这是我的码 ...
- vue调用高德地图:vue-amap
前言:之前没有接触过页面调用地图的项目,某次面试,老板要求我用vue-amap调用高德地图,回家以后,我去网上查了一些案例和教程,看似很简单的引入调用,我却整整弄了一宿,还没弄出来!!!百般无奈之下, ...
- vue集成高德地图
vue集成高德地图 前言 二.使用步骤 1.注册高德开发平台 2.vue 结尾 前言 之前玩Thymeleaf的时候玩过高德地图,现在无聊Vue项目也整个地图进去~ 二.使用步骤 1.注册高德开发平台 ...
- Android 编程 AMapLocationClientOption 类中的 setNeedAddress 方法用处 (高德地图 com.amap.api.location.AMapLocationClientOption 中的类)
最近在用高德地图来写Android App, 其中有一些 方法是不太理解的,这里写一下 对 高德地图 com.amap.api.location.AMapLocationClientOption ...
- VUE 高德地图选取地址组件开发
高德地图文档地址 http://lbs.amap.com/api/lightmap/guide/picker/ 结合步骤: 1.通过iframe内嵌引入高德地图组件 key就选你自己申请的key &l ...
- VUE 2.0 引入高德地图,自行封装组件
1. 高德地图官网 申请帐号, 申请相应(JavaScript API)的 Key 2. 在项目中引入, 这里和其他的引入不同的是 直接在 index.html, 不是在 main.js 引入, 博主 ...
- vue+vant ui+高德地图的选址组件
首先在index.html引入高德地图的js <script src="https://webapi.amap.com/maps?v=1.4.14&key=你的key" ...
- vue 调用高德地图
一. vue-amap,一个基于 Vue 2.x 和高德地图的地图组件 https://elemefe.github.io/vue-amap/#/ 这个就不细说了,按照其文档,就能够安装下来. 二. ...
- 前端vue使用高德地图
首先,注册Key 1.注册开发者账号,成为高德开放平台开发者 2.登陆之后,在进入「应用管理」 页面「创建新应用」 3.为应用添加 Key,「服务平台」一项请选择「 Web 端 ( JSAPI ) 」 ...
- Android 编程 AMapLocationClientOption 类中的 setMockEnable (高德地图 com.amap.api.location.AMapLocationClientOption 中的类)
setMockEnable 高德地图中 AMapLocationClientOption 中有一个方法是设置APP是否接受模拟定位的设置,就是方法 setMockEnable //设置是否允许模拟位置 ...
随机推荐
- Diagnostics: File file:/tmp/spark-***/__spark_libs__***.zip does not exist
Diagnostics: File file:/tmp/spark-c03df206-c90e-4c97-a2d6-a5d3fdb17811/__spark_libs__303213348409500 ...
- 修改Jenkins的主目录步骤
在使用Jenkins做持续集成过程中,在构建很多次后发现有时在构建的时候系统提示磁盘空间不足,此时检查发现Jenkins的主目录挂载区放在了服务器根目录下,占用空间较大,此时除了对服务器的磁盘进行扩容 ...
- Flask web开发之路十四
今天开始Flask的实战,创建一个项目,实现包括用户登录.注册.注销.发表博客.评论以及检索等功能 首先给出项目结构: 1.config.py文件: 存放各种配置信息 import os # dial ...
- [No0000101]JavaScript-基础课程1
JavaScript 是一种轻量级的编程语言,很容易学习,同时也是一种被广泛用于客户端Web开发的脚本语言.通过本课程学习,我们可以了解到JavaScript的基本语法知识,以及怎样使用它去创建简单的 ...
- mac休眠掉电快,更改休眠模式
打开终端输入: $ pmset -g 查看休眠模式 hibernatemode 发现值为3, 这是大多数的设置,如果为0 ,那么休眠时严重掉电, 我们可以改变这个模式: $ sudo pmset -a ...
- day4:数据结构list
1,一直输入用户名,输入Q退出,注意用户的输入别忘了加strip,和upper不区分大小写,list最后一位添加append li = [] while 1: name = input("& ...
- 分析java的堆栈信息 内存模型
package com.test.learnJava; public class LineNum { public static void main(String[] args) { System.o ...
- hyperledge vagrant docker development environment
https://blog.csdn.net/zgljl2012/article/details/52896372
- [daily][archlinux][rsync] rsync
科普文档:https://wiki.archlinux.org/index.php/Rsync 之前改文件系统时,用过. 然而用的不太对,导致一部分文件的权限出了问题. [troubleshoot][ ...
- ORACLE之PACKAGE-包、存储过程、函数
1,简单的包. 创建包规范: create or replace package pack_test1 is -- 定义过程1 procedure p_test1(p_1 in varchar2); ...