首页 > 编程知识 正文

小程序地图添加GroundOverlay并将其southwest设为中心

时间:2023-11-21 19:07:07 阅读:293560 作者:VMKV

本文旨在介绍如何在小程序中添加GroundOverlay覆盖物,并将其southwest设为地图中心。

一、GroundOverlay概述

GroundOverlay是百度地图API中的一种覆盖物,可以将图片或者矩形区域覆盖在地图上。GroundOverlay的实例化需要地理位置信息,其中包括南西、北东两个点的经纬度坐标。

二、小程序地图添加GroundOverlay

下面是添加GroundOverlay的示例代码:

// 在地图上添加GroundOverlay
addGroundOverlay() {
  let that = this
  that.mapCtx.getCenterLocation({
    success(res) {
      that.mapCtx.addGroundOverlay({
        'url': 'https://i.enlanhao.com/pic/night.jpg',
        'alpha': 100,
        'southwest': {
          longitude: res.longitude - 0.01,
          latitude: res.latitude - 0.01
        },
        'northeast': {
          longitude: res.longitude + 0.01,
          latitude: res.latitude + 0.01
        },
        success: function (res) {
          console.log('addGroundOverlay success')
        },
        fail: function (res) {
          console.log('addGroundOverlay fail')
        }
      })
    }
  })
}

其中,url指定了待覆盖图片的url地址;alpha指定了覆盖图片的透明度,取值范围为0-100;southwest和northeast分别指定了待覆盖区域的南西和北东两个点的经纬度坐标。以上代码会在地图中心添加一个南西偏南0.01度,南西偏西0.01度,北东偏北0.01度,北东偏东0.01度的矩形覆盖物。

三、将GroundOverlay的southwest设为中心

通过调用地图相应接口,我们可以将GroundOverlay的southwest点设为地图中心。如下所示:

// 将GroundOverlay的southwest设为地图中心
setGroundOverlayCenter() {
  let that = this
  that.mapCtx.getCenterLocation({
    success(res) {
      // 获取地图中心点经纬度坐标
      let centerLongitude = res.longitude
      let centerLatitude = res.latitude
      
      // 获取GroundOverlay南西点经纬度坐标
      let southwestLongitude = centerLongitude - 0.01
      let southwestLatitude = centerLatitude - 0.01
      
      // 设置地图中心点为GroundOverlay南西点
      that.mapCtx.moveToLocation({
        longitude: southwestLongitude,
        latitude: southwestLatitude
      })
    }
  })
}

以上代码中,我们首先通过mapCtx.getCenterLocation()接口获取地图中心点的经纬度坐标,然后计算出GroundOverlay的南西点经纬度坐标,最后通过mapCtx.moveToLocation()接口将地图中心点移动到GroundOverlay的南西点,即将GroundOverlay的southwest设置为地图中心。

小结

通过以上代码示例,我们可以在小程序地图中添加GroundOverlay覆盖物,并将其southwest设为地图中心。这样能够使得地图的焦点更加准确地锁定在待覆盖区域上,提升地图应用的用户体验。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。