位置: IT常识 - 正文

vue项目中使用高德地图(vue中使用gojs)

编辑:rootadmin
vue项目中使用高德地图

推荐整理分享vue项目中使用高德地图(vue中使用gojs),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue 使用,vue 使用,vue高级技巧,vue在项目中怎么用的,vue高级技巧,vue在项目中怎么用的,vue 高级用法,vue 高级用法,内容如对您有帮助,希望把文章链接给更多的朋友!

最近做的项目中有个地图选择的功能,如下图所示:

所以在此记录下使用方法,望各位大神指导

我的应用 | 高德控制台第一步:去高德官网去创建一个属于自己的地图应用 (得到key和秘钥)我的应用 | 高德控制台

 这是添加的方式:

vue项目中使用高德地图(vue中使用gojs)

准备-入门-教程-地图 JS API | 高德地图API

第二步:获取到属于自己的key和秘钥后,下载地图高德:npm install vue-amap --save 

第三步:在main.js中导入 

VueAMap.initAMapApiLoader({ key: '你申请的key', plugin: [ 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PlaceSearch', 'AMap.Geolocation', 'AMap.Geocoder', 'AMap.DrivingPolicy', 'AMap.Driving', "AMap.Autocomplete", "AMap.PolyEditor", "AMap.CircleEditor", ], v: '1.4.4', uiVersion: '1.0',})

这里的key是你申请的

第四步:在index.html页面头部 添加秘钥

​​​​​​​​​​​​​​

<script type="text/javascript"> window._AMapSecurityConfig = { securityJsCode: "你申请的秘钥", } </script>

 以上步骤完成后,就可以去你对应的页面中使用了

因为我是写的一个组件,所以大家根据自己的情况 

我的组件完整代码:

<template> <div class="container"> <div class="search-box"> <el-row> <el-col :span="16"> <el-input v-model="searchKey" id="search" placeholder="请输入地址信息"></el-input> </el-col> <el-col :span="6"> <el-button type="success" plain @click="searchByHand" style="margin-left:20px">搜索位置</el-button> </el-col> </el-row> <div class="tip-box" id="searchTip"></div> </div> <el-amap class="amap-box" :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></template><script>import { AMapManager, lazyAMapApiLoaderInstance } from 'vue-amap'let amapManager = new AMapManager()export default { data() { let self = this return { address: null, searchKey: '', amapManager, markers: [], searchOption: { city: '全国', citylimit: true }, center: [108.872249, 34.33328], zoom: 10, lng: 0, lat: 0, loaded: false, markerEvent: { click(e) { // console.log(e); } }, mapInfo: { lng: '', lat: '', mapText: '' }, 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) { if (result.regeocode.crosses.length > 0) { self.mapInfo.lng = result.regeocode.crosses[0].location.lng self.mapInfo.lat = result.regeocode.crosses[0].location.lat } if (result.regeocode.crosses.length > 0) { self.mapInfo.lng = result.regeocode.pois[0].location.lng self.mapInfo.lat = result.regeocode.pois[0].location.lat } self.mapInfo.mapText = result.regeocode.formattedAddress // console.log('地图组件中点击选中的地址为',self.mapInfo) if (self.mapInfo.lng !== '' && self.mapInfo.lat !== '') { self.$store.state.addersInfo = JSON.stringify(self.mapInfo) } console.log(self.lng, self.lat, "33333333333333333") self.address = result.regeocode.formattedAddress self.searchKey = result.regeocode.formattedAddress self.$nextTick() } } }) } }, // 一些工具插件 plugin: [ { // 定位 pName: 'Geolocation', events: { init(o) { // o是高德地图定位插件实例 o.getCurrentPosition((status, result) => { if (result && result.position) { // 设置经度 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 = true // 页面渲染好后 self.$nextTick() } }) }, click(e) { // console.log(e); } } }, { // 工具栏 pName: 'ToolBar', events: { init(instance) { // console.log(instance); } } }, { // 鹰眼 pName: 'OverView', events: { init(instance) { // console.log(instance); } } }, { // 地图类型 pName: 'MapType', defaultType: 0, events: { init(instance) { // console.log(instance); } } }, { // 搜索 pName: 'PlaceSearch', events: { init(instance) { // console.log(instance) } } } ] } }, created() { this.searchKey = JSON.parse(this.$store.state.addersInfo).mapText; }, watch: { "$store.state.addersInfo": { handler: function (newVal, oldVal) { this.searchKey = JSON.parse(newVal).mapText; }, deep : true }, searchKey(addersText) { if (addersText == '') { this.$store.state.addersInfo = '' } } }, methods: { // submitAddress() {//确定事件 // console.log('经纬度', this.center) // console.log('地址', this.address) // this.mapInfo.lng = this.center[0] // this.mapInfo.lat = this.center[1] // this.mapInfo.mapText = this.address // // console.log(this.mapInfo) // this.$store.state.address = JSON.stringify(this.mapInfo) // }, initSearch() { let vm = this let map = this.amapManager.getMap() AMapUI.loadUI(['misc/PoiPicker'], function (PoiPicker) { var poiPicker = new PoiPicker({ input: 'search', placeSearchOptions: { map: map, pageSize: 10 }, suggestContainer: 'searchTip', searchResultsContainer: 'searchTip' }) console.log(poiPicker, "***********") 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.cityname + poi.adname + poi.name vm.center = [lng, lat] // console.log(vm.center) vm.markers.push([lng, lat]) vm.lng = lng vm.lat = lat vm.address = address vm.searchKey = address // console.log(vm.address) vm.mapInfo.lng = lng vm.mapInfo.lat = lat vm.mapInfo.mapText = vm.address // console.log(vm.mapInfo) vm.$store.state.addersInfo = JSON.stringify(vm.mapInfo) } }) }) }, searchByHand() {//点击搜索事件 if (this.searchKey !== '') { this.searchKey = JSON.parse(this.$store.state.addersInfo).mapText; this.poiPicker.searchByKeyword(this.searchKey) } } },}</script><style lang="scss" scoped>.container { width: 100%; height: 500px;}.search-box { position: absolute; z-index: 5; width: 100%; left: 13%; top: 10px; height: 30px; border: none !important;}.tip-box { width: 73%; max-height: 260px; position: absolute; top: 42px; overflow-y: auto; background-color: #fff;}</style>

里面都有对应的注释

本文链接地址:https://www.jiuchutong.com/zhishi/283340.html 转载请保留说明!

上一篇:pavupg.exe是什么进程 pavupg进程查询(pnaico.exe是什么软件)

下一篇:ReadTimeoutError: HTTPSConnectionPool(host=‘cdn-lfs.huggingface.co‘, port=443)

  • 苹果13画中画怎么用(苹果13画中画怎么操作)

  • 荣耀30pro3.5mm的耳机接口在哪(荣耀30pro是几寸的)

  • 快手怎么反名(快手反名教程)

  • 苹果屏幕使用时间不准(苹果屏幕使用时间准确吗)

  • 电脑正在更新怎么取消(电脑正在更新怎么取消让他开机)

  • 华为电子三包日期比激活日期早(华为电子三包日期是怎样计算的)

  • 855比845强多少(845比855差多少)

  • 小米10充电要多久(小米10充电多少安)

  • 如何更改工作簿的名称(如何更改工作簿名称颜色)

  • 华为高对比文字有啥用(华为高比度文字)

  • oppo手机老是黑屏死机怎么办(oppo手机老是黑屏一闪一闪)

  • ufs3.1手机有哪些(ufs 3.1的手机)

  • oppok5跑分

  • ios13三指撤销怎么关闭(iphone三指轻点撤销)

  • 大王卡怎么激活应用宝(腾讯大王卡怎么激活)

  • 华为mate30pro可以用5g吗(华为mate30pro可以用120w充电器吗)

  • 苹果手表电量能用多久(苹果手表电量能在手机上看吗)

  • 苹果8p短信特效怎么弄(iphone8短信特效设置)

  • 消防控制室的布置要求(消防控制室的布置有哪些要求?)

  • 虎扑要达到什么等级才可以发帖(虎扑干啥用的)

  • 陌陌怎么不能视频聊天了(陌陌怎么不能视频显示时间太短)

  • 苹果手机26键怎么换行(苹果手机26键怎么换行打字)

  • 充电宝充一会就发烫(充电宝充一会就不充了是怎么回事)

  • 苹果6手机反应慢的解决方法(苹果6手机反应迟钝了怎么办)

  • Python 删除List元素的三种方法remove、pop、del(list删除某个元素 python)

  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设 电脑维修 湖南楚通运网络