zoukankan      html  css  js  c++  java
  • 基于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储存。
  • 相关阅读:
    文档库文件上传Webpart(原创)
    SharePointWebControls帮助类
    使用反射创建动态程序集
    Silverlight Workflow 工作流设计器和工作流编辑器的若干截图,先睹为快(Workflow Designer)
    Using Networking to Retrieve Data and Populate a DataGrid
    昕友silverlight表单设计器的使用 (原创 Form Designer)
    Bing翻译和Google翻译的比较
    具有某接口与是某类型
    UI Automation in WPF/Silverlight
    RTP/RTCP/RTSP/SIP/SDP
  • 原文地址:https://www.cnblogs.com/don-yang/p/8670782.html
Copyright © 2011-2022 走看看