基于vue 2.X和高德地图的vue-amap组件获取经纬度
今天我就讲了一下怎么通过vue和高德地图开发的vue-amap组件来获取经纬度。
这是vue-amap的官网文档:https://elemefe.github.io/vue-amap/#/
这是我的码云项目的地址:http://git.oschina.net/ye_a_rs/project-vue-ele/tree/master/src
- 用 vue init webpack 项目名称 创建一个项目
- npm安装vue-amap组件 :
npm install vue-amap --save
- 在main.js引入vue-amap :
import Vue from 'vue';
import AMap from 'vue-amap';
import App from './App.vue'; Vue.use(AMap);
AMap.initAMapApiLoader({
key: 'your amap key',
plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView',
'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor',
'AMap.CircleEditor','AMap.Geolocation'] });
new Vue({
el: '#app',
render: h => h(App) })
- 在initAMapApiLoader里面用到什么插件就在plugin里面写什么插件名;
如果用到定位,就在app.vue这样写:
<template>
<div id="app">
<router-view name='index'></router-view>
<router-view name='others'></router-view>
<el-amap vid="amap" :plugin="plugin" class="amap-demo"></el-amap>
<router-view></router-view>
<!-- <foot></foot> -->
</div>
</template>
<script>
// import foot from './components/Footer';
export default {
name: 'app',
data() {
let self = this;
return {
positions: {
lng: '',
lat: '',
address: '',
loaded: false
},
center: [121.59996, 31.197646],
plugin: [{
pName: 'Geolocation',
events: {
init(o) {
// o 是高德地图定位插件实例
o.getCurrentPosition((status, result) => {
// console.log(result); // 能获取定位的所有信息
if (result && result.position) {
self.str = result.formattedAddress;
self.positions.address = self.str.substring(self.str.indexOf('市') + 1);
self.positions.lng = result.position.lng;
self.positions.lat = result.position.lat;
self.positions.loaded = true;
self.$nextTick();
// 把获取的数据提交到 store 的数据中,以便其他单文件组件使用
self.$store.commit('POSITION', self.positions);
// console.log(self.positions);
sessionStorage.setItem('id', JSON.stringify(self.positions));
}
});
}
}
}]
};
}
// components: { foot }
}
</script>
<style lang='scss'>
@import '../static/hotcss/px2rem.scss';
@import './common/css/resert.scss';
#app {
height: 100%;
.amap-demo {
display: none;
}
}
</style>
- 在pName:写入'Geolocation',并在main.js的AMap.initAMapApiLoader引入‘AMap.Geolocation’,在app里写以上代码,定位的所有信息都在o.getCurrentPosition((status, result) 的result里,再对里面进行解析、获取,可以将经纬度和地址通过sessionStorage的setItem储存。
基于vue 2.X和高德地图的vue-amap组件获取经纬度的更多相关文章
- 微信小程序-基于高德地图API实现天气组件(动态效果)
微信小程序-基于高德地图API实现天气组件(动态效果) 在社区翻腾了许久,没有找到合适的天气插件.迫不得已,只好借鉴互联网上的web项目,手动迁移到小程序中使用.现在分享到互联网社区中,帮助后续有 ...
- 关于Vue中,在方法中使用(操作)子组件获取到的数据
已知,子组件通过props获取父组件传过来的数据,而这个数据是无法在created.mounted生命周期中使用的,只能在beforeUpdated或者updated获取到: 但是如果我们要使用这个数 ...
- Vue异步加载高德地图API
项目中用到了高德地图的API以及UI组件库,因为是直接把引入script写在index.html中,项目打包后运行在服务器,用浏览器访问加载第一次时会非常慢,主要原因是加载高德地图相关的js(近一分钟 ...
- VUE 2.0 引入高德地图,自行封装组件
1. 高德地图官网 申请帐号, 申请相应(JavaScript API)的 Key 2. 在项目中引入, 这里和其他的引入不同的是 直接在 index.html, 不是在 main.js 引入, 博主 ...
- java调用百度地图API依据地理位置中文获取经纬度
百度地图api提供了非常多地图相关的免费接口,有利于地理位置相关的开发,百度地图api首页:http://developer.baidu.com/map/. 博主使用过依据地理依据地理位置中文获取经纬 ...
- 最全vue的vue-amap使用高德地图插件画多边形范围
一.在vue-cli的框架下的main.js(或者main.ts)中引入高德插件,代码如下: import Vue from 'vue' import VueAMap from 'vue-amap' ...
- vue项目中使用高德地图(根据坐标定位点)
前言 项目中需要根据坐标定位,将自己的实现过程写下来,废话不多说,上代码 正文 <script> var map,marker; export default { data(){ retu ...
- 用高德地图API 通过详细地址获得经纬度
http://cloud.sinyway.com/Service/amap.html http://restapi.amap.com/v3/geocode/geo?key=xxxxxxxxxxxxxx ...
- 百度地图中使用mouseover事件获取经纬度时无法拿到鼠标所在位置的经纬度。
用百度2.0的话使用mousemove 鼠标在地图区域移动过程中触发此事件.mouseover参数e中没有point参数
随机推荐
- 进程—内存描述符(mm_struct)
http://blog.csdn.net/qq_26768741/article/details/54375524 前言 上一篇我们谈论了task_struct这个结构体,它被叫做进程描述符,内部成员 ...
- javascript入门笔记5-事件
1.继续循环continue; continue的作用是仅仅跳过本次循环,而整个循环体继续执行. 语句结构: for(初始条件;判断条件;循环后条件值更新) { if(特殊情况) { continue ...
- wm_concat()函数
工作中遇到这样一个问题,一张数据库表中有一个字段file_id,还有一个主键f_id(唯一),而file_id不唯一,我想把file_id=‘123456789’的记录中的f_id(主键)连接成一个字 ...
- Objective-c 单例设计模式
Objective-c 单例设计模式 一.什么是单例模式:(Singleton) 单例模式的意图是是的类的对象成为系统中唯一的实例,提供一个访问点,供客户类共享资源. 二.什么情况下使用 ...
- hdu_3501_Calculation 2
Given a positive integer N, your task is to calculate the sum of the positive integers less than N w ...
- MySQL巧用FIND_IN_SET和GROUP_CONCAT函数减少Java代码量
数据库表简介:物品表 `id` int(11) '物品id,唯一标识', `name` varchar(255) '物品名称', `level` int(11) '物品类别等级,礼品包为最高级1,类 ...
- 基于WSAAsyncSelect模型的两台计算机之间的通信
任务目标 编写Win32程序模拟实现基于WSAAsyncSelect模型的两台计算机之间的通信,要求编程实现服务器端与客户端之间双向数据传递.客户端向服务器端发送"请输出从1到1000内所有 ...
- C#基础-异常处理与自定义异常
异常处理 static void Main(string[] args) { Console.WriteLine("请输入一个数字:"); try { // 监测可能出现异常代码 ...
- 想学习一下node.js,重新安装配置了node
根据这个网站上的教程安装配置的,还不错一次就成功了.觉得安装没什么,就是配置路径的时候容易错. http://www.runoob.com/nodejs/nodejs-install-setup.ht ...
- 面向对象特性 - php
1.类的字段调用格式 公用字段 类内调用 $this->字段名 类外调用 $对象名->字段名 静态 类内调用 self::$字段名 类外调用 类名::$字段名 常量 类内调用 ...