当前位置: 首页 > news >正文

高德地图绘图,点标记,并计算中心点

 

效果图

代码如下

/ 地图初始化
const map: any = ref(null)
const marker: any = ref(null)
const polyEditor: any = ref(null)
const view: any = ref(false)
const squareVertices: any = ref([])
const init = () => {workSpacesCurrent(workspaceId, {}).then((res) => {console.log('地图呢', res.data)map.value = new AMap.Map('container6', {center: [res.data.longitude, res.data.latitude],// center://   ['118.622184', '32.096262'],layers: [// 卫星new AMap.TileLayer.Satellite(),// 路网new AMap.TileLayer.RoadNet(),],zoom: 17, // 地图显示的缩放级别zooms: [0, 19], // 设置缩放级别范围为3至16})pointMark()})
}// 点标记
function pointMark () {if (cameraId.value !== 0) {console.log('你是多少', cameraId.value)console.log('cameraLngInpt', cameraLngInpt.value)console.log('cameraLatInpt', cameraLatInpt.value)setTimeout(() => {marker.value = new AMap.Marker({icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png',position: [cameraLngInpt.value, cameraLatInpt.value],})map.value.add(marker.value)}, 5000)} else {if (squareVertices.value.length === 0) {map.value.on('click', (e) => {if (view.value) {polyEditor.value.close()polyEditor.value = nullmap.value.clearMap()}console.log('地图点击进来啦吗')console.log('e', e)marker.value = new AMap.Marker({icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png',position: [e.lnglat.lng, e.lnglat.lat],})map.value.add(marker.value)// marker.value.setMap(map.value)console.log('地图点击点标记', marker.value)const cameralng = e.lnglat.lngconst cameralat = e.lnglat.latcameraLngInpt.value = cameralngcameraLatInpt.value = cameralat// 绘图drawMark(e)})} else {// centerPointFn()}}
}// 绘图
function drawMark (e: any) {squareVertices.value = calculateSquareVertices(e.lnglat.lat,e.lnglat.lng,1000)console.log('squareVertices.value', squareVertices.value)markerQure()
}// 监听绘图变化
const markerQure = () => {console.log('进来了没有地图渲染')console.log('squareVertices.value', squareVertices.value)if (view.value) {// polyEditor.value.close()// polyEditor.value = null// map.value.clearMap()}const path: any = []if (cameraId.value !== 0) {squareVertices.value.forEach((i) => {path.push([i[0], i[1]])})} else {squareVertices.value.forEach((i) => {path.push([i.lng, i.lat])})}console.log('path', path)const polygon = new AMap.Polygon({path: path,strokeColor: '#FF33FF',strokeWeight: 6,strokeOpacity: 0.2,fillOpacity: 0.4,fillColor: '#1791fc',zIndex: 50,bubble: true,})console.log('polygon', polygon)map.value.add([polygon])map.value.setFitView()centerPointFn()polyEditor.value = new AMap.PolygonEditor(map.value, polygon)polyEditor.value.addAdsorbPolygons([polygon])polyEditor.value.open()// 监听坐标点的变化polyEditor.value.on('addnode', function (event) {squareVertices.value = []event.target.getPath().forEach((i) => {squareVertices.value.push({lng: i.lng,lat: i.lat,})})console.log('这里有吗', squareVertices.value)clearMarker()setTimeout(() => {pointMark()}, 500)centerPointFn()})view.value = true// 监听拖动变化polyEditor.value.on('adjust', function (event) {squareVertices.value = []event.target.getPath().forEach((i) => {squareVertices.value.push({lng: i.lng,lat: i.lat,})})console.log('坐标参数', squareVertices.value)clearMarker()pointMark()centerPointFn()})polyEditor.value.on('removenode', function (event) {squareVertices.value = []event.target.getPath().forEach((i) => {squareVertices.value.push({lng: i.lng,lat: i.lat,})})console.log('坐标参数2', squareVertices.value)clearMarker()pointMark()centerPointFn()})
}// 画图各个点标记
const calculateSquareVertices = (centerLat, centerLng, sideLength) => {// 将长度从米转换为经度差值const deltaLat = (sideLength / 111300) * 1.1const deltaLng =(sideLength / (111300 * Math.cos(centerLat * (Math.PI / 180)))) * 1.1const vertices = [{ lat: centerLat + deltaLat, lng: centerLng - deltaLng }, // 右上角{ lat: centerLat - deltaLat, lng: centerLng - deltaLng }, // 右下角{ lat: centerLat - deltaLat, lng: centerLng + deltaLng }, // 左下角{ lat: centerLat + deltaLat, lng: centerLng + deltaLng }, // 左上角]return vertices
}// 中心点坐标
function centerPointFn () {let sumLng = 0let sumLat = 0if (cameraId.value !== 0) {console.log('是否进来')for (let i = 0; i < squareVertices.value.length; i++) {sumLng += squareVertices.value[i][0]sumLat += squareVertices.value[i][1]}} else {for (let i = 0; i < squareVertices.value.length; i++) {sumLng += squareVertices.value[i].lngsumLat += squareVertices.value[i].lat}}const centerLng = sumLng / squareVertices.value.lengthconst centerLat = sumLat / squareVertices.value.length// 创建多边形的中心点坐标对象const centerPoint = new AMap.LngLat(centerLng, centerLat)const centerPointInpp = [centerLng, centerLat]centerPointInpt.value = centerPointInpp.toString()addMarker(centerLng, centerLat)console.log('参数3', centerPointInpt.value)console.log('多边形的中心点经度:' +centerPoint.getLng() +',纬度:' +centerPoint.getLat())// const marker = new AMap.Marker({//   icon: '/src/assets/center.png',//   position: [centerLng, centerLat],//   offset: new AMap.Pixel(-13, -30),// })// map.value.add(marker)
}// 添加标记点
function addMarker (lng, lat) {if (marker.value) {console.log('添加点坐标更新')marker.value.setPosition([lng, lat]) // 更新点坐标} else {marker.value = new AMap.Marker({// icon: '/src/assets/center.png',map: map.value,position: [lng, lat],offset: new AMap.Pixel(-13, -30),})}
}// 清除标记点
function clearMarker () {console.log('清除打点位置', marker.value)if (marker.value) {marker.value.setMap(null)marker.value = null}
}

相关文章:

  • Leetcode面试经典150题-141.环形链表
  • 官宣:Zilliz 在亚马逊云科技中国区正式开服!
  • EA橘子平台Origin离线安装包获取
  • 树莓派安装 OpenCV 教程
  • 腾讯云使用
  • 企业采用电子招投标的原因及系统推荐
  • 50ETF期权可以当天买卖吗?
  • 观众登记2025中国(深圳)国际智能手机供应链展览会
  • 解算方案—二连杆与三连杆解算
  • 学习Vue3的第五天
  • 2024年全国大学生软件测试大赛赛项安排(一)
  • Docker快速部署Apache Guacamole
  • 移动订货小程序哪个好 批发订货系统源码哪个好
  • Mac电脑IDEA2024安装后打不开问题解决
  • Ubuntu IDEA 卡死问题如何解决
  • 自己简单写的 事件订阅机制
  • Debian下无root权限使用Python访问Oracle
  • eclipse的离线汉化
  • iOS筛选菜单、分段选择器、导航栏、悬浮窗、转场动画、启动视频等源码
  • Java 11 发布计划来了,已确定 3个 新特性!!
  • Javascript基础之Array数组API
  • Java-详解HashMap
  • java中的hashCode
  • Rancher-k8s加速安装文档
  • Vim 折腾记
  • 百度地图API标注+时间轴组件
  • 第13期 DApp 榜单 :来,吃我这波安利
  • 聊一聊前端的监控
  • 普通函数和构造函数的区别
  • 如何利用MongoDB打造TOP榜小程序
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • 2017年360最后一道编程题
  • 交换综合实验一
  • 微龛半导体获数千万Pre-A轮融资,投资方为国中创投 ...
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • (12)Hive调优——count distinct去重优化
  • (Redis使用系列) Springboot 整合Redisson 实现分布式锁 七
  • (二)fiber的基本认识
  • (二)Kafka离线安装 - Zookeeper下载及安装
  • (四)Controller接口控制器详解(三)
  • (游戏设计草稿) 《外卖员模拟器》 (3D 科幻 角色扮演 开放世界 AI VR)
  • (转)nsfocus-绿盟科技笔试题目
  • (转)甲方乙方——赵民谈找工作
  • (转载)在C#用WM_COPYDATA消息来实现两个进程之间传递数据
  • ***php进行支付宝开发中return_url和notify_url的区别分析
  • *ST京蓝入股力合节能 着力绿色智慧城市服务
  • *算法训练(leetcode)第四十天 | 647. 回文子串、516. 最长回文子序列
  • .helper勒索病毒的最新威胁:如何恢复您的数据?
  • .NET 8 编写 LiteDB vs SQLite 数据库 CRUD 接口性能测试(准备篇)
  • .NET Compact Framework 多线程环境下的UI异步刷新
  • .net framework profiles /.net framework 配置
  • .net redis定时_一场由fork引发的超时,让我们重新探讨了Redis的抖动问题
  • .Net 代码性能 - (1)
  • .NET 跨平台图形库 SkiaSharp 基础应用
  • .NET 快速重构概要1