高德地图矢量图形点击事件生成

官方文档https://lbs.amap.com/demo/javascript-api-v2/example/overlay-editor/polylineeditor

生成矢量图形如上,代码如下

const markerAddof = ref(false)
// 绘图
function drawMark () {
  if (!markerAddof.value) {//限制矢量图形点击只显示一个
    if (cameraId.value !== 0) {
      markerQure()
    } else {
      squareVertices.value = calculateSquareVertices(
        cameraLatInpt.value,//中心坐标
        cameraLngInpt.value,//中心坐标
        1000
      )
      setTimeout(() => {
        markerQure()
      }, 1000)

      console.log('squareVertices.value', squareVertices.value)
    }
    markerAddof.value = true
  }
}

对地图进行渲染, 如果点击后只显示一个矢量图形,可以加上限制判断,不需要的不用加。必须添加中心点,需要根据中心点进行渲染

// 画图各个点标记
const calculateSquareVertices = (centerLat, centerLng, sideLength) => {
  // 将长度从米转换为经度差值
  const deltaLat = (sideLength / 111300) * 1.1
  const deltaLng =
    (sideLength / (111300 * Math.cos(centerLat * (Math.PI / 180)))) * 1.1
  // console.log('deltaLat', deltaLat)
  // console.log('deltaLng', deltaLng)
  const vertices = [
    { lat: Number(centerLat) + deltaLat, lng: Number(centerLng) - deltaLng }, // 右上角
    { lat: Number(centerLat) - deltaLat, lng: Number(centerLng) - deltaLng }, // 右下角
    { lat: Number(centerLat) - deltaLat, lng: Number(centerLng) + deltaLng }, // 左下角
    { lat: Number(centerLat) + deltaLat, lng: Number(centerLng) + deltaLng }, // 左上角
  ]
  console.log(vertices)
  return vertices
}

 通过计算给四周的点进行标记,可以直接使用该算法

// 绘图变化
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 {
    console.log(' squareVertices.value2', squareVertices.value)
    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)
    // 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)
    // 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)
    // centerPointFn()
  })
}

通过改变点的位置,对矢量图形重新绘画并重新获取各个点的位置进行渲染

Vue 高德地图 API 提供了 `AMap.MouseTool` 类,可以用来绘制矢量图。下面是一个简单的示例: ``` <template> <div id="map-container" style="height: 500px;"></div> </template> <script> export default { data() { return { map: null, mouseTool: null }; }, mounted() { // 加载地图 this.map = new AMap.Map('map-container', { zoom: 13, // 缩放级别 center: [116.397428, 39.90923] // 地图中心点 }); // 创建 MouseTool 实例 this.mouseTool = new AMap.MouseTool(this.map); // 监听绘制事件 AMap.event.addListener(this.mouseTool, 'draw', e => { // 获取绘制的矢量图对象 const shape = e.obj; // 在地图上显示矢量图 this.map.add(shape); }); }, methods: { // 开始绘制矢量图 startDraw(type) { this.mouseTool[type](); } } }; </script> ``` 在上面的示例中,我们首先创建了一个地图实例并将其渲染到页面上。然后,我们创建了一个 MouseTool 实例,并在绘制事件中获取绘制的矢量图对象并将其添加到地图上。最后,我们提供了一个 `startDraw` 方法,用于开始绘制不同类型的矢量图。 可以通过调用 `startDraw` 方法来开始绘制矢量图。例如,要绘制一个圆,可以在模板中添加一个按钮并将其与 `startDraw` 方法关联: ``` <template> <div> <div id="map-container" style="height: 500px;"></div> <button @click="startDraw('circle')">绘制圆</button> </div> </template> ``` 这将在地图上启动圆绘制工具。其他可用的绘制工具包括 `rectangle`(矩)、`polygon`(多边)和 `polyline`(折线)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值