以下是一个用微信小程序编写的程序示例,实现了在地图中心上方放置一个红色正方形边框,并且可以通过滑块调节边框的大小,最后输出边框左上角和右下角对应的经纬度。

在 wxml 文件中,添加以下代码:

<view class="container">
  <map id="map" longitude="{{longitude}}" latitude="{{latitude}}" bindregionchange="regionChange"></map>
  <view class="slider-container">
    <slider class="slider" value="{{size}}" min="10" max="100" bindchange="sliderChange"></slider>
  </view>
  <view class="info">
    <text>左上角经度:{{topLeftLongitude}}</text>
    <text>左上角纬度:{{topLeftLatitude}}</text>
    <text>右下角经度:{{bottomRightLongitude}}</text>
    <text>右下角纬度:{{bottomRightLatitude}}</text>
  </view>
</view>

在 wxss 文件中,添加以下代码:

.container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.map {
  width: 100%;
  height: 80%;
}

.slider-container {
  width: 100%;
  height: 10%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.slider {
  width: 80%;
}

.info {
  width: 100%;
  height: 10%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

在 js 文件中,添加以下代码:

Page({
  data: {
    longitude: 0, // 地图中心经度
    latitude: 0, // 地图中心纬度
    size: 50, // 边框大小,默认为50
    topLeftLongitude: 0, // 边框左上角经度
    topLeftLatitude: 0, // 边框左上角纬度
    bottomRightLongitude: 0, // 边框右下角经度
    bottomRightLatitude: 0, // 边框右下角纬度
  },

  onReady: function() {
    // 获取当前位置
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        this.setData({
          longitude: res.longitude,
          latitude: res.latitude,
          topLeftLongitude: res.longitude - this.data.size / 2 * 0.00001,
          topLeftLatitude: res.latitude + this.data.size / 2 * 0.00001,
          bottomRightLongitude: res.longitude + this.data.size / 2 * 0.00001,
          bottomRightLatitude: res.latitude - this.data.size / 2 * 0.00001,
        });
      }
    });
  },

  sliderChange: function(e) {
    const size = e.detail.value;
    this.setData({
      size: size,
      topLeftLongitude: this.data.longitude - size / 2 * 0.00001,
      topLeftLatitude: this.data.latitude + size / 2 * 0.00001,
      bottomRightLongitude: this.data.longitude + size / 2 * 0.00001,
      bottomRightLatitude: this.data.latitude - size / 2 * 0.00001,
    });
  },

  regionChange: function(e) {
    if (e.type === 'end') {
      // 获取地图中心位置
      const mapContext = wx.createMapContext('map');
      mapContext.getCenterLocation({
        success: (res) => {
          this.setData({
            longitude: res.longitude,
            latitude: res.latitude,
            topLeftLongitude: res.longitude - this.data.size / 2 * 0.00001,
            topLeftLatitude: res.latitude + this.data.size / 2 * 0.00001,
            bottomRightLongitude: res.longitude + this.data.size / 2 * 0.00001,
            bottomRightLatitude: res.latitude - this.data.size / 2 * 0.00001,
          });
        }
      });
    }
  }
})

这个程序会在地图中心上方放置一个红色正方形边框,滑块可以调节边框的大小。初始时,边框大小为50,位置为当前位置。当地图位置发生变化时,边框也会相应更新。最后,页面会显示边框左上角和右下角对应的经纬度。

注意:需要在小程序管理后台开启地图功能,并在小程序代码中引入地图组件库


原文地址: http://www.cveoy.top/t/topic/iUwO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录