前端点击地图上的位置获取当前经纬度

server/2024/10/19 9:31:49/

地图封装组件

<template><div id="container"></div>
</template>
<script>import AMapLoader from '@amap/amap-jsapi-loader';export default {name: "Mapview",props:{mapinfo:{type:Object,default:function(){return {}}},maptype:{type:String,default:''}},data() {return {map:null,isDraw:false,editpology:null,polyEditor:null,wz:null,loca:null}},watch:{mapinfo(newdata){if(newdata.data != null){console.log(newdata)}}},created() {},mounted() {this.initAMap();console.log(this.mapinfo)},methods: {initAMap() {AMapLoader.load({key: '16e7ac6ee499a277e372a63e39cbdee9',  //设置您的keyversion: "2.0",plugins: ['AMap.ToolBar', 'AMap.Driving',"AMap.MouseTool","AMap.PolygonEditor"," AMap.MapType","AMap.Polygon","AMap.Marker"],AMapUI: {version: "1.1",plugins: [],},Loca: {version: "2.0"},}).then((AMap) => {let _this = this;if(this.maptype){var satelliteLayer = new AMap.TileLayer.Satellite();this.map = new AMap.Map("container", {viewMode: "3D",zoom: 10,/* pitch:60, */skyColor :'#1682a9',layers:[satelliteLayer],zooms: [2, 22],});/* this.map.addControl(new AMap.MapType({defaultType:1 //0代表默认,1代表卫星})); *//* AMapUI.loadUI(['control/BasicControl'], function(BasicControl) {//图层切换控件_this.map.addControl(new BasicControl.LayerSwitcher({position: 'rt'}));}) */}else{this.map = new AMap.Map("container", {zoom: 10,pitch:60,zooms: [2, 22],});AMapUI.loadUI(['control/BasicControl'], function(BasicControl) {//添加一个缩放控件_this.map.addControl(new BasicControl.Zoom({position: 'lt'}));//缩放控件,显示Zoom值_this.map.addControl(new BasicControl.Zoom({position: 'lb',showZoomNum: true}));//图层切换控件_this.map.addControl(new BasicControl.LayerSwitcher({position: 'rt'}));});}this.loca = new Loca.Container({map: this.map});/* this.map.addControl(new AMap.MapType({defaultType:0})) */_this.addClickEvent()_this.map.on('complete', function() {if(_this.$parent.getmapinfo){_this.$parent.getmapinfo()}else{_this.$emit('initmap')let styleName = "amap://styles/fresh";_this.map.setMapStyle(styleName);}})}).catch(e => {console.log(e);})},addClickEvent(){let _this = thisvar clickHandler = function(e) {console.log(e.target)if(this.marker == null){this.marker =  new AMap.Marker({position: [e.lnglat.getLng(),e.lnglat.getLat()]});this.marker.setMap(_this.map);}else{this.marker.setPosition([e.lnglat.getLng(),e.lnglat.getLat()])}_this.wz = [e.lnglat.getLng(),e.lnglat.getLat()]_this.$emit("getmaplocl",[e.lnglat.getLng(),e.lnglat.getLat()])};// 绑定事件this.map.on('click', clickHandler);// 解绑事件//this.map.off('click', clickHandler);},addPolygon(data){data =JSON.parse(data)let polygon = new AMap.Polygon({path: data,/* fillColor: '#ccebc5',strokeOpacity: 1,fillOpacity: 0.5,strokeColor: '#2b8cbe',strokeWeight: 1,strokeStyle: 'dashed',strokeDasharray: [5, 5], */});this.map.add(polygon)polygon.on('click',()=>{alert(1)this.editpology = polygonthis.edit()})this.map.setFitView()},addPolygons(data,name,id){data =JSON.parse(data)let polygon = new AMap.Polygon({path: data,/* fillColor: '#ccebc5',strokeOpacity: 1,fillOpacity: 0.5,strokeColor: '#2b8cbe',strokeWeight: 1,strokeStyle: 'dashed',strokeDasharray: [5, 5], */});polygon.on('click',()=>{this.$emit('clickpoly',id)})var text = new AMap.Text({position: polygon.getBounds().getCenter(),anchor: 'bottom-center',text: name,style: {color:'white','background-color':'rgba(220,38,38,0)','border':'none'},});text.on('click',()=>{this.$emit('clickpoly',id)})text.addTo(this.map)this.map.add(polygon)},addmarks(data,id){let marker =  new AMap.Marker({position: data});marker.on('click',()=>{//alert(id)this.$emit('clickmarker',id)})marker.setMap(this.map);},fitview(){this.map.setFitView()},draw() {this.isDraw = true;this.mouseTool = new AMap.MouseTool(this.map);this.mouseTool.polygon({//fillColor: "#00b0ff",//strokeColor: "#80d8ff"//同Polygon的Option设置});this.mouseTool.on("draw", e => {// 监听画多边形结束console.log("draw");this.overlays = [];console.log(e.obj);console.log(e.obj.getArea())console.log(e.obj.getPath())console.log(e.obj.getOptions())console.log(e.obj)console.log(e.obj.getBounds().getCenter())// console.log(e.obj.getOptions().path)this.$emit("update:mapinfo",{area:e.obj.getArea(),data:e.obj.getOptions().path})var text = new AMap.Text({position: e.obj.getBounds().getCenter(),anchor: 'bottom-center',text: '文本标记',style: {'background-color':'rgba(220,38,38,0)','border':'none'},});text.addTo(this.map)this.overlays.push(e.obj);this.mouseTool.close();this.isDraw = false;});},edit() {//console.log("bianji");//let polygon = this.overlays[0];if(this.editpology == null){return}let polygon =  this.editpology;this.polyEditor = new AMap.PolygonEditor(this.map, polygon);// polyEditor.addAdsorbPolygons(polygon1)this.polyEditor.open();polygon.on("dblclick", () => {console.log("双击");this.polyEditor.close();});this.polyEditor.on("end", event => { // polyEditor  close关闭时执行console.log(event.target.getPath());this.$emit("update:mapinfo",{area:polygon.getArea(),data:polygon.getOptions().path})});},canceledit(){if(this.polyEditor){this.polyEditor.close()}},clearmap(){this.map.clearMap();},animate(){this.loca.animate.start();let speed = 1;let _this = this// 下钻this.loca.viewControl.addAnimates([{pitch: {value: 0,control: [[0, 20], [1, 0]],timing: [0, 0, 0.8, 1],duration: 3000 / speed,},zoom: {value: 15.9,control: [[0, 12.8], [1, 15.9]],timing: [0, 0, 0.8, 1],duration: 3000 / speed,},rotation: {value: -20,control: [[0, 20], [1, -20]],timing: [0, 0, 0.8, 1],duration: 2000 / speed,},}/* ,{pitch: {value: 0,control: [[0, 0], [1, 100]],timing: [0.1, 0, 0.7, 1],duration: 2000 / speed,},rotation: {value: 360,control: [[0, 260], [1, 361]],timing: [0.3, 0, 0.7, 1],duration: 2000 / speed,},zoom: {value: 10.8,control: [[0, 17.5], [1, 13.8]],timing: [0.3, 0, 0.7, 1],duration: 2500 / speed,},} */,{// 环绕pitch: {value: 0,control: [[0, 40], [1, 50]],timing: [0.3, 0, 1, 1],duration: 7000 / speed,},rotation: {value: 360,control: [[0, -80], [1, 260]],timing: [0, 0, 0.7, 1],duration: 7000 / speed,},zoom: {value: 11,control: [[0.3, 16], [1, 17]],timing: [0.3, 0, 0.9, 1],duration: 5000 / speed,},}], function () {});}}}
</script><style scoped>#container {padding: 0px;margin: 0px;width: 100%;height: 100%;position: absolute;}
</style>

在vue页面内引入
import gdmap from ‘组件路径’
父子组件传参
父组件内

<gdmap @getmaplocl="getmaplocal" />methods:{getmaplocal(data){console.log(data)this.form.equipmentLongitude = data.join(',')},
}

http://www.ppmy.cn/server/23497.html

相关文章

uniapp实现点击事件跳转页面

首先定义一个点击事件 这里采用的vue3的写法&#xff0c;然后写上触发事件后要跳转的路径 function jump() {uni.switchTab({url:/pages/bangong/index})} 到这里就简单的实现uniapp的点击跳转页面了

All in One mini主机搭建全屋主路由方案----自己实现自己的路由器,实现路由器自由!

1 接线 首先&#xff0c;需要保证家里当前状态是有网的状态&#xff08;路由器有网并正常工作&#xff09; 将鼠标键盘接在mini主机的USB口&#xff0c;HDMP/DP/VGA等接上显示器。从路由器的lan口接一根网线出来接在mini主机的ETH0上&#xff0c;接在mini主机上保证mini主机在…

面试经典150题——分发糖果

面试经典150题 day15 题目来源我的题解方法一 两次不同方向的遍历&#xff08;应该是贪心&#xff09;方法二 贪心 题目来源 力扣每日一题&#xff1b;题序&#xff1a;135 我的题解 方法一 两次不同方向的遍历&#xff08;应该是贪心&#xff09; 遍历两次&#xff08;左→…

Linux 基本指令(上)

目录 whoami 命令 pwd 命令 ls 命令 Linux的目录结构 cd 命令 文件操作 什么是文件 touch 命令 mkdir 命令 rmdir / rm 命令 rm 删除文件&#xff1a; rm 删除目录&#xff1a; whoami 命令 whoami &#xff1a;可以看出当前登录的用户名 pwd 命令 pwd 用于显示用户当…

第八章异常(是程序在执行过程中,出现的非正常的情况,如果不处理最终会导致JVM的非正常停止)

目录 2、如何对待异常 3、异常的抛出机制 8.1.2 Java异常体系 1、Throwable 2、Error和Exception 8.1.3 受检异常和非受检异常 演示常见的错误和异常 1、Error 2、运行时异常&#xfeff; 3、编译时异常 8.2 异常的处理 8.2.1 捕获异常&#xff1a;try…catch 1、…

《Fundamentals of Power Electronics》——三端电池的旋转、负载差分连接

以下是关于三端电池的旋转的相关知识点&#xff1a; Buck电路、Boost电路和Buck-Boost电路均包含一个与单刀单掷开关相连的电感。如下图所示。 将上图中的电感和开关网络视为一个标有a,b,c三端的基础电池。该电池在电源和负载之间有三种不同的连接方式。a-A b-B c-C连接方式组…

【MySQL】多表查询

1.多表查询分类讲解 1.1等值连接vs非等值连接 等值连接&#xff1a; SELECT student.name, class.name AS className FROM student, class WHERE student.class_id class.class_id; 非等值连接&#xff1a; SELECT a.name, a.salary, b.grade FROM teacher a, grades b WH…

安卓基础知识-跨进程通信IPC篇

目录 1. Intent简介和作用分类常见操作创建intent设置component目的组件&#xff08;显式intent&#xff09;设置action动作添加category类别设置data数据和type类型设置和读取扩展数据extras设置flags标志位常见用法 2. Messenger3. Binderbinder机制介绍binder面试题AIDL 4. …