高德地图显示圆形区域并在区域边上标注半径

image.png

  • bug:循环创建三个圆形区域 ,数组设置为[{raduis:500,color:“#FF0000”}],然后循环取颜色会莫名其妙报错
  • 修改为 strokeColor: [“#FF0000”, “#1EE3C2”, “#3772E9”][i]即可
 initAMap() {
      AMapLoader.load({
        key: "130cca3be68a2ff0fd5ebb6de25e4eac", // 申请好的Web端开发者Key,首次调用 load 时必填
        // version: "v1.4.15", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        // plugins: [
        //   "AMap.ControlBar",
        //   "AMap.ToolBar",
        //   "AMap.Weather",
        //   "AMap.CitySearch",
        //   "AMap.Marker",

        //   "AMap.MouseTool",
        //   "AMap.PolyEditor",
        //   "AMap.Driving",
        //   "AMap.Polyline",
        //   "AMap.Geolocation",
        //   "AMap.GraspRoad",
        //   "AMap.Geocoder",
        //   "AMap.GeometryUtil.ringArea",
        //   "AMap.DistrictSearch",
        //   "AMap.MoveAnimation",
        // ], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      })
        .then((AMap) => {
          this.map = new AMap.Map("containerMap", {
            // 设置地图容器id
            rotateEnable: true,
            pitchEnable: true,
            zoom: 13,
            pitch: 50,
            rotation: -15,
            viewMode: "3D", //开启3D视图,默认为关闭
            zooms: [2, 20],
            center: [119.419251, 32.400703],
            mapStyle: "amap://styles/blue", //设置地图的显示样式
          });

          // 工具条
          // var controlBar = new AMap.ControlBar({
          //   position: {
          //     right: "10px",
          //     top: "10px",
          //   },
          // });
          // this.map.addControl(controlBar);

          // var toolBar = new AMap.ToolBar({
          //   position: {
          //     right: '40px',
          //     top: '110px'
          //   }
          // });
          // this.map.addControl(toolBar)

          this.map.on("complete", () => {
            console.log("地图加载完成");

            // -------展示警情中心点 this.position --------
            if (this.position) {
              // 添加中心点标记
              var marker = new AMap.Marker({
                position: new AMap.LngLat(...this.position),
                icon: new AMap.Icon({
                  image: fireIcon,
                  imageSize: new AMap.Size(26, 34),
                }),
              });
              this.map.add(marker);
              
              //设置圆形位置
              var center = new AMap.LngLat(...this.position);
              //设置圆的半径大小
              var radius = [500, 1000, 1500];
              radius.map((item, i) => {
                //创建圆形 Circle 实例
                var circle = new AMap.Circle({
                  center: center, //圆心
                  radius: item, //半径
                  borderWeight: 3, //描边的宽度
                  strokeColor: ["#FF0000", "#1EE3C2", "#3772E9"][i], //轮廓线颜色
                  strokeOpacity: 1, //轮廓线透明度
                  strokeWeight: 2, //轮廓线宽度
                  fillOpacity: 0.1, //圆形填充透明度
                  // strokeStyle: "dashed", //轮廓线样式
                  // strokeDasharray: [10, 10],
                  fillColor: ["#FF0000", "#1EE3C2", "#3772E9"][i], //圆形填充颜色
                  zIndex: 50, //圆形的叠加顺序
                });
                //圆形 Circle 对象添加到 Map
                this.map.add(circle);
                //根据覆盖物范围调整视野
                this.map.setFitView([circle]);

                // 获取圆心坐标为center,半径为radius米,最右侧bearing边线上的点
                function getEdgePointOfCircle(center, radius, bearing) {
                  // 中心经纬度
                  var lat = center[1];
                  var lon = center[0];
                  // 将角度转换为弧度
                  var brngRad = (bearing * Math.PI) / 180;
                  // 地球平均半径,单位米,用于近似计算
                  var R = 6371000;
                  // 计算新坐标的纬度
                  var lat2 = Math.asin(
                    Math.sin((lat * Math.PI) / 180) * Math.cos(radius / R) 
                      Math.cos((lat * Math.PI) / 180) *
                        Math.sin(radius / R) *
                        Math.cos(brngRad)
                  );
                  // 计算新坐标的经度
                  var lon2 =
                    (lon * Math.PI) / 180 +
                    Math.atan2(
                      Math.sin(brngRad) *
                        Math.sin(radius / R) *
                        Math.cos((lat * Math.PI) / 180),
                      Math.cos(radius / R) -
                        Math.sin((lat * Math.PI) / 180) * Math.sin(lat2)
                    );
                  // 转换回度数并返回结果
                  return [(lon2 * 180) / Math.PI, (lat2 * 180) / Math.PI];
                }

                // 在圆的边缘添加半径标注
                let addRadiusLabel = () => {
                  // 计算标注位置,这里简单地将标注放在圆心右侧,根据实际情况调
                  var labelPosition = getEdgePointOfCircle(
                    this.position,
                    item,
                    90
                  );

                  // 创建文本标注
                  var text = new AMap.Text({
                    text: item + "米", //标记显示的文本内容
                    anchor: "center", //设置文本标记锚点位置
                    draggable: false, //是否可拖拽
                    cursor: "pointer", //指定鼠标悬停时的鼠标样式。
                    angle: 0, //点标记的旋转角度
                    style: {
                      //设置文本样式,Object 同 css 样式表
                      padding: ".1rem 0.2rem",
                      "border-radius": ".1rem",
                      "background-color": "transparent",
                      border: "1px solid rgba(0, 0, 0, 0.5)",
                      "border-color": ["#FF0000", "#1EE3C2", "#3772E9"][i],
                      "text-align": "center",
                      "font-size": ".1875rem",
                      color: "white",
                    },
                    position: labelPosition, //点标记在地图上显示的位置
                  });
                  text.setMap(this.map); //将文本标记设置到地图上
                };

                // 调用函数添加半径标注
                addRadiusLabel();
              });
            }
          });
        })
        .catch((e) => {
          console.log(e);
        });
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue是一款流行的JavaScript框架,而高德API是一组非常强大的地图API。将二者结合起来使用,可以实现许多有趣的地图功能,比如地图圆形矩形编辑半径搜索等。 想要实现地图圆形矩形编辑半径搜索,需要先引入高德地图API,并在Vue组件中进行相关的配置和操作。 首先,需要在Vue组件中引入高德地图API,并在对应的生命周期方法中初始化地图。可以通过在mounted方法中引入高德地图JSAPI,并在初始化方法中配置地图相关的选项来完成地图的初始化。 接下来,需要在地图中添加圆形或矩形图元来实现编辑。可以通过调用高德地图API的circle和rectangle方法来添加圆形和矩形图元,同时可以绑定相关的事件监听器来实现交互操作。例如,可以添加鼠标点击事件监听器,在点击图元时弹出相应的信息窗口。 为了实现半径搜索,需要在地图中添加相应的搜索功能。可以利用高德地图JSAPI的search方法来进行搜索操作,并将搜索结果展示在地图上。例如,在搜索框中输入关键词后,可以调用高德地图API的search方法进行搜索,并将搜索结果展示在地图上,同时高亮显示圆形或矩形图元内的相关地点。 总之,利用Vue和高德地图API可以很方便地实现地图圆形矩形编辑半径搜索等有趣的功能。只需要在Vue组件中进行相应的配置和操作即可。同时,还可以结合其他工具和技术来进一步实现更复杂的地图应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曲曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值