首页 > 其他分享 >vue2项目使用vueAMap

vue2项目使用vueAMap

时间:2023-11-09 11:59:31浏览次数:29  
标签:vueAMap 项目 self latitude let result vue2 lat lng

npm install vue-amap -D

在main.js

import VueAMap from "vue-amap";
Vue.use(VueAMap);

VueAMap.initAMapApiLoader({
  key: "高德地图的key", // 这里写你申请的高德地图的key
  plugin: ["AMap.Autocomplete", "AMap.Geocoder", "AMap.Geolocation"],
  v: "1.4.15",
  uiVersion: "1.1"
});
window._AMapSecurityConfig = {
  securityJsCode: '高的地图安全密钥',
}

封装组件使用


<template>
    <div>
      <div class="search-box">
        <el-input
          v-model="searchKey"
          type="search"
          id="search"
          placeholder="请选择详细地址"
          prefix-icon="el-icon-search"
          @keyup.enter.native="searchByHand"
          style="margin-bottom: 5px;"
          @change="searchChange"
        ></el-input>
        <div class="tip-box" id="searchTip"></div>
      </div>
      <!--
        amap-manager: 地图管理对象
        vid:地图容器节点的ID
        zooms: 地图显示的缩放级别范围,在PC上,默认范围[3,18],取值范围[3-18];在移动设备上,默认范围[3-19],取值范围[3-19]
        center: 地图中心点坐标值
        plugin:地图使用的插件
        events: 事件
      -->
      <div class="amap-box">
        <el-amap
          :amap-manager="amapManager"
          :vid="'amap-vue'"
          :zoom="zoom"
          :plugin="plugin"
          :center="center"
          :events="events"
        >
          <!-- 标记 -->
          <el-amap-marker
            v-for="(marker, index) in markers"
            :position="marker"
            :key="index"
          ></el-amap-marker>
        </el-amap>
      </div>
    </div>
  </template>
  <script>
  import { AMapManager, lazyAMapApiLoaderInstance } from "vue-amap";
  let amapManager = new AMapManager();
  export default {
    props: ["city", "value", "longitude", "latitude", "isEdit"],
    data() {
      let self = this;
      return {
        address: null,
        searchKey: "",
        amapManager,
        markers: [],
        searchOption: {
          city: this.city ? this.city : "全国",
          citylimit: true
        },
        center:[108.887441,34.2159],
        zoom: 17,
        lng: 0,
        lat: 0,
        loaded: false,
        // showCircle: false,
        events: {
          init() {
            lazyAMapApiLoaderInstance.load().then(() => {
              self.initSearch();
            });
          },
          // 点击获取地址的数据
          click(e) {
            self.markers = [];
            let { lng, lat } = e.lnglat;
            self.lng = lng;
            self.lat = lat;
            self.center = [lng, lat];
            self.markers.push([lng, lat]);
            // 这里通过高德 SDK 完成。
            let geocoder = new AMap.Geocoder({
              radius: 1000,
              extensions: "all"
            });
            geocoder.getAddress([lng, lat], function(status, result) {
              if (status === "complete" && result.info === "OK") {
                if (result && result.regeocode) {
                  self.address = result.regeocode.formattedAddress;
                  self.searchKey = result.regeocode.formattedAddress;
                  self.$emit("updateLocation", lng, lat, self.searchKey);
                  self.$nextTick();
                }
              }
            });
          }
        },
        // 一些工具插件
        plugin: [
          {
            // 定位
            pName: "Geolocation",
            events: {
              init(o) {
                // 关闭定位中心阴影
                o.options.showCircle = false 
                // 关闭定位中心标记
                o.options.showMarker = false
                // o是高德地图定位插件实例
                o.getCurrentPosition((status, result) => {
                  if (result && result.position) {
                    if (self.isEdit) {
                      // 设置经度
                      self.lng = self.longitude;
                      // 设置维度
                      self.lat = self.latitude;
                      // 设置坐标
                      self.center = [self.longitude, self.latitude];
                      // self.markers.push([self.longitude, self.latitude]);
                    } else {
                      // 设置经度
                      self.lng = result.position.lng;
                      // 设置维度
                      self.lat = result.position.lat;
                      // 设置坐标
                      self.center = [self.lng, self.lat];
                      // self.markers.push([self.lng, self.lat]);
                    }
                    // load
                    self.loaded = false;
                    let geocoder = new AMap.Geocoder({
                      radius: 1000,
                      extensions: "all"
                    });
                    geocoder.getAddress([self.lng, self.lat], function(status, result) {
                      if (status === "complete" && result.info === "OK") {
                        if (result && result.regeocode) {
                          self.address = result.regeocode.formattedAddress;
                          self.searchKey = result.regeocode.formattedAddress;
                          self.$emit("updateLocation", self.lng, self.lat, self.searchKey);
                          self.$nextTick();
                        }
                      }
                    });
                    console.log(self.lng, self.lat, self.searchKey,'o是高德地图定位插件实例o是高德地图定位插件实例o是高德地图定位插件实例o是高德地图定位插件实例o是高德地图定位插件实例');
                    // 页面渲染好后
                    self.$nextTick();
                  }
                });
              }
            }
          }
        ]
      };
    },
    created() {
      if (this.value) {
        this.searchKey = this.value;
        this.address = this.value;
      }
      if (this.longitude && this.latitude) {
        this.lng = this.longitude;
        this.lat = this.latitude;
        this.center = [this.longitude, this.latitude];
        this.markers.push([this.longitude, this.latitude]);
      }
    },
    methods: {
      searchChange(){
        this.$emit("updateLocation", '', '','');
      },
      // 选择地址后自动定位到当前地址附近
      updateAddress(value, longitude, latitude) {
        this.searchKey = value;
        this.address = value;
        this.lng = longitude;
        this.lat = latitude;
        this.center = [longitude, latitude];
        this.markers.push([longitude, latitude]);
      },
      initSearch() {
        let vm = this;
        let map = this.amapManager.getMap();
        AMapUI.loadUI(["misc/PoiPicker"], function(PoiPicker) {
          let poiPicker = new PoiPicker({
            input: "search",
            placeSearchOptions: {
              map: map,
              pageSize: 10
            },
            suggestContainer: "searchTip",
            searchResultsContainer: "searchTip"
          });
          vm.poiPicker = poiPicker;
          // 监听poi选中信息
          poiPicker.on("poiPicked", function(poiResult) {
            let source = poiResult.source;
            let poi = poiResult.item;
            if (source !== "search") {
              poiPicker.searchByKeyword(poi.name);
            } else {
              poiPicker.clearSearchResults();
              vm.markers = [];
              let lng = poi.location.lng;
              let lat = poi.location.lat;
              let address = poi.name; // poi.cityname + poi.adname + poi.name
              vm.center = [lng, lat];
              vm.markers.push([lng, lat]);
              vm.lng = lng;
              vm.lat = lat;
              vm.address = address;
              vm.searchKey = address;
              vm.$emit("updateLocation", lng, lat, vm.searchKey);
            }
          });
        });
      },
      searchByHand() {
        if (this.searchKey !== "" && this.poiPicker) {
          this.poiPicker.searchByKeyword(this.searchKey);
        }
      },
    }
  };
  </script>
  <style lang="scss" scoped>
  .search-box {
    margin-top: 6px;
    width: 100%;
  }
  .search-box input {
    padding: 0 15px;
    width: 100%;
    height: 32px;
    line-height: 32px;
    color: #606266;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
  }
  .search-box input:focus {
    border-color: #409eff;
    outline: 0;
  }
  .search-box input::-webkit-input-placeholder {
    color: #c0c4cc;
  }
  .tip-box {
    width: 100%;
    max-height:280px;
    position: absolute;
    top: 72px;
    z-index: 10000;
    overflow-y: auto;
    background-color: #fff;
  }
  </style>
  <style>
  .amap-ui-poi-picker-sugg,
  .amap_lib_placeSearch {
    border: 1px solid #eee;
    border-radius: 4px;
  }
  .amap-box {
    height: 200px;
  }
  </style>

标签:vueAMap,项目,self,latitude,let,result,vue2,lat,lng
From: https://www.cnblogs.com/cwl1025/p/17819353.html

相关文章

  • 大屏展示技术栈:vue2+echarts+dataV
    1.大屏搭建使用的是dataV组件http://datav.jiaminghi.com/guide,使用dataV组件有如下注意点:a.修改配置项config中data的值,需要重新赋值configb.修改dataV某些内置样式,他有个固定的类2.登录界面动态背景,使用的是vanta.jsVanta.js-Animated3DBackgroundsForYourWebsite......
  • Go语言实战开发一个WEB项目博客系统
    Go语言实战开发一个WEB项目博客系统beego个人博客系统功能介绍首页分页展示博客博客详情评论文章专栏分类导航资源分享时光轴点点滴滴关于本站后台管理登录系统设置分类添加修改删除管理博文添加修改删除管理基于Go语言和beego框架前端使用layui布局开发的个......
  • 备份下启动springboot项目脚本文件 start.sh
    1.将jar包放到和该脚本同级目录2.记得chmod777./start.sh赋予执行权限3.执行./start.sh文件即可4.重启也可以直接执行此文件5.如果没有打包配置文件可以在此脚本同级目录创建config目录将yml/properties配置文件放进去. #!/bin/bash#进入脚本所在路径cd`dirname$......
  • 【Microsoft Azure 的1024种玩法】七十四.五分钟在Azure Virtual Machines中快速部署
    【简介】ApacheMaven由Apache软件基金会所提供的一个软件项目管理及自动构建工具,Maven为开发者提供了一套完整的构建生命周期框架。开发团队几乎不用花多少时间就能够自动完成工程的基础构建配置,因为Maven使用了一个标准的目录结构和一个默认的构建生命周期,Maven能够在很短......
  • vue2实现动态侧边导航栏
    router文件下index.js 来源http://blog.itpub.net/69978258/viewspace-2909200///index.tsimportVuefrom'vue';importVueRouterfrom'vue-router';importLoginfrom'@/views/login/index.vue';importLayoutfrom'@/layout/ind......
  • CMake多个CMakeLists.txt共同合作编译一个C++项目
    一、概述在C++项目比较大或者要根据不同的规则生成不同的执行文件或者动态库/静态库的时候。单独的CMakeLists.txt会变的比较复杂,此时可以利用CMakeLists.txt的父子关系分目录分模块的进行编译及输出。就相当于项目模块化编译参考博客:【大丙课堂】二、具体实现......
  • vue 项目使用element ui 中tree组件 check-strictly 用法
    属性check-strictly:   在显示复选框的情况下,是否严格遵循父子互相关联的做法,默c认为false。   默认false,父子关联。      点击父节点,其下子节点全部统一跟随父节点变化,点击子节点,子节点部分勾选时,父节点处于半选状态。   设置为true,严格遵循......
  • Hadoop作业实时智能监控项目实践
    1.背景对于Hadoop集群监控,有基于Linux的硬件告警,比如磁盘,内存,网络带宽告警;有基于组件的告警,例如OOM报警、RPC告警。这些告警能反应个体机器的运行状况,不能反映整个集群的运行状况;同时,这些告警都是在已知的故障指标,但是对于未知的指标,可能已经发生并且对系统产生较大影响,由于没有......
  • 项目管理之项目时间箱内容补充
    本章节内容为前述文章“项目管理之如何召开项目时间箱启动会议”的补充内容,请结合阅读。时间箱管理包括时间箱启动会、时间箱执行与控制、时间箱回顾会三个部分。时间箱执行与控制包括探索、精进、巩固三个部分,每个部分使用迭代开发技术。迭代开发技术包括识别、计划、发展......
  • 2023版全新高质量商业级小程序全栈项目实战22章完结
    点击下崽:2023版全新高质量商业级小程序全栈项目实战22章完结  提取码:fxso小程序全栈项目是一种将前端、后端和数据库等技术结合起来,实现一个完整的小程序应用的项目。全栈项目可以让开发者更加高效地开发小程序,同时也可以提高小程序的性能和稳定性。本文将介绍如何构建一个小程......