使用高德地图JS API 开发——区划浏览/下钻功能(4)

使用高德地图JS API 开发之行政区划浏览(4):

官方API 👉传送门 官方示例 👉传送门

关于如何引入高德地图JS API方式 请移步前面文章👉使用高德地图JS API 开发一些常见使用方法(急救包)

上一篇👉: 使用高德地图JS API 开发——海量点标记LabelMarker

高德行政区划浏览DistrictExplorer实例集成了地图下钻功能 亲测好用不伤脑👍

DistrictExplorer(行政区划浏览) 提供了全国范围内到区县一级的行政区划数据(含边界),同时提供一些辅助功能,比如区划面绘制、事件监听,以及快速判断经纬度所属的子级区划等。

但是官方对DistrictExplorer做了目的性的取舍,重点针对两级浏览,
在这里插入图片描述
行政区划浏览 DistrictExplorer是以传入区划编码(adcode) 渲染区域面,有点击、移入、移出操作 优化需求效果可对此下手
在这里插入图片描述

另外 DistrictExplorer只能下钻渲染到区县区域,若想要继续下钻到街镇可以使用 👉AMap.GeoJSON
通过渲染json绘制区域,在加载区县区域时把json效果叠在上面 后面也能做下钻展示街镇的区域效果
提示:街镇json需要付费购买。

1.先引入UI组件库
<script src="//webapi.amap.com/ui/1.1/main.js?v=1.1.1"></script>
2.加载DistrictExplorer(模块名:ui/geo/DistrictExplorer)
//加载DistrictExplorer,loadUI的路径参数为模块名中 'ui/' 之后的部分 
AMapUI.loadUI(['geo/DistrictExplorer'], function(DistrictExplorer) {
    // 这里执行你的操作
});
全部代码下👇 (从官方示例扒了下来直接可用,但没有切换过渡效果)

在这里插入图片描述

<!-- Vue 2 Code -->
<template>
  <!-- 行政区划浏览示例-->
  <div id="index" ref="appRef">
    <!-- 地图 -->
    <div id="container" ref="mapRef"></div>
  </div>
</template>
<script>
var map; // 地图加载
var districtExplorer; // 行政区域实例
var currentAreaNode = null;
export default {
  name: "componentsMap",
  data() {
    return {
      timing: null,
      adcode: 100000,
    };
  },
  created() {},
  mounted() {
    this.$nextTick(() => this.initMap());
  },
  beforeDestroy() {
    map?.destroy();
    map = null;
  },
  methods: {
    initMap() {
      map = new window.AMap.Map("container", {
        terrain: false, //关闭地形图
        showBuildingBlock: false, // 不显示3D楼块
        showLabel: false, // 取消地图层标注
        scrollWheel: true, // 开启缩放
        dragEnable: true, // 开启鼠标拖动
        doubleClickZoom: false, // 禁止双击
      });
      this.setMapUI();
    },
    // 加载行政区域组件
    setMapUI() {
      window.AMapUI.load(
        ["ui/geo/DistrictExplorer", "lib/$"],
        (DistrictExplorer, $) => {
          //创建行政区划实例
          districtExplorer = window.districtExplorer = new DistrictExplorer({
            eventSupport: true, //打开事件支持
            map: map,
          });
          // 加载当前聚焦行政区域
          this.switch2AreaNode(this.adcode);

          //鼠标hover提示内容
          var $tipMarkerContent = $('<div class="tipMarker top"></div>');

          var tipMarker = new window.AMap.Marker({
            content: $tipMarkerContent.get(0),
            offset: new window.AMap.Pixel(0, 0),
            bubble: true,
          });
          //根据Hover状态设置相关样式
          function toggleHoverFeature(feature, isHover, position) {
            tipMarker.setMap(isHover ? map : null);

            if (!feature) {
              return;
            }

            var props = feature.properties;

            if (isHover) {
              //更新提示内容
              $tipMarkerContent.html(props.adcode + ": " + props.name);
              //更新位置
              tipMarker.setPosition(position || props.center);
            }

            //更新相关多边形的样式
            var polys = districtExplorer.findFeaturePolygonsByAdcode(
              props.adcode
            );
            for (var i = 0, len = polys.length; i < len; i++) {
              polys[i].setOptions({
                fillOpacity: isHover ? 0.5 : 0.2,
              });
            }
          }
          //监听feature的hover事件
          districtExplorer.on(
            "featureMouseout featureMouseover",
            function (e, feature) {
              toggleHoverFeature(
                feature,
                e.type === "featureMouseover",
                e.originalEvent ? e.originalEvent.lnglat : null
              );
            }
          );
          //监听鼠标在feature上滑动
          districtExplorer.on("featureMousemove", function (e, feature) {
            //更新提示位置
            tipMarker.setPosition(e.originalEvent.lnglat);
          });

          //feature被点击
          districtExplorer.on("featureClick", (e, feature) => {
            var props = feature.properties;
            //如果存在子节点
            // if (props.childrenNum > 0) {
            //切换聚焦区域
            this.switch2AreaNode(props.adcode);
            // }
          });

          //外部区域被点击
          districtExplorer.on("outsideClick", (e) => {
            districtExplorer.locatePosition(
              e.originalEvent.lnglat,
              (error, routeFeatures) => {
                if (routeFeatures && routeFeatures.length > 1) {
                  //切换到省级区域
                  this.switch2AreaNode(routeFeatures[1].properties.adcode);
                } else {
                  //切换到全国
                  this.switch2AreaNode(100000);
                }
              },
              {
                levelLimit: 2,
              }
            );
          });
        }
      );
    },
    //绘制某个区域的边界
    renderAreaPolygons(areaNode) {
      var colors = [
        "#3366cc",
        "#dc3912",
        "#ff9900",
        "#109618",
        "#990099",
        "#0099c6",
        "#dd4477",
        "#66aa00",
        "#b82e2e",
        "#316395",
        "#994499",
        "#22aa99",
        "#aaaa11",
        "#6633cc",
        "#e67300",
        "#8b0707",
        "#651067",
        "#329262",
        "#5574a6",
        "#3b3eac",
      ];
      //更新地图视野
      map.setBounds(areaNode.getBounds(), null, null, true);
      //清除已有的绘制内容
      districtExplorer.clearFeaturePolygons();
      //绘制子区域
      districtExplorer.renderSubFeatures(areaNode, function (feature, i) {
        var fillColor = colors[i % colors.length];
        var strokeColor = colors[colors.length - 1 - (i % colors.length)];

        return {
          cursor: "default",
          bubble: true,
          strokeColor: strokeColor, //线颜色
          strokeOpacity: 1, //线透明度
          strokeWeight: 1, //线宽
          fillColor: fillColor, //填充色
          fillOpacity: 0.35, //填充透明度
        };
      });

      //绘制父区域
      districtExplorer.renderParentFeature(areaNode, {
        cursor: "default",
        bubble: true,
        strokeColor: "black", //线颜色
        strokeOpacity: 1, //线透明度
        strokeWeight: 1, //线宽
        fillColor: areaNode.getSubFeatures().length ? null : colors[0], //填充色
        fillOpacity: 0.35, //填充透明度
      });
    },
    //切换区域
    switch2AreaNode(adcode, callback) {
      let that = this;
      if (currentAreaNode && "" + currentAreaNode.getAdcode() === "" + adcode) {
        return;
      }
      that.loadAreaNode(adcode, function (error, areaNode) {
        if (error) {
          if (callback) {
            callback(error);
          }
          return;
        }
        currentAreaNode = window.currentAreaNode = areaNode;
        //设置当前使用的定位用节点
        districtExplorer.setAreaNodesForLocating([currentAreaNode]);
        //切换区域后刷新显示内容
        districtExplorer.setHoverFeature(null);
        that.renderAreaPolygons(areaNode);
        if (callback) {
          callback(null, areaNode);
        }
      });
    },
    //加载区域
    loadAreaNode(adcode, callback) {
      districtExplorer.loadAreaNode(adcode, function (error, areaNode) {
        if (error) {
          if (callback) {
            callback(error);
          }
          return;
        }
        if (callback) {
          callback(null, areaNode);
        }
      });
    },
  },
};
</script>
<style lang="scss"  scoped>
#index {
  width: 1920px;
  height: 1080px;
  color: #ffffff;
  overflow: hidden;
  #container {
    width: 1920px;
    height: 1080px;
    z-index: 100;
  }
}
/* 地图移入tipMarker样式 */
::v-deep .tipMarker {
  color: #555;
  background-color: rgba(255, 254, 239, 0.8);
  border: 1px solid #7e7e7e;
  padding: 2px 6px;
  font-size: 12px;
  white-space: nowrap;
  display: inline-block;
}
.tipMarker:before,
.tipMarker:after {
  content: "";
  display: block;
  position: absolute;
  margin: auto;
  width: 0;
  height: 0;
  border: solid transparent;
  border-width: 5px 5px;
}
.tipMarker.top {
  transform: translate(-50%, -110%);
}
.tipMarker.top:before,
.tipMarker.top:after {
  bottom: -9px;
  left: 0;
  right: 0;
  border-top-color: rgba(255, 254, 239, 0.8);
}
.tipMarker.top:before {
  bottom: -10px;
  border-top-color: #7e7e7e;
}
</style>
    
    

直接下载(需要积分,没积分下载私我私发)👉行政区浏览-行政区划浏览-官方示例中心-JS API UI 组件示例 | 高德地图API

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
高德地图API是一款功能强大的地图服务,它提供了多种功能和接口供开发使用使用高德地图API需要经过以下几个步骤: 1. 首先,你需要申请一个高德地图开发者账号,并创建一个应用。你可以访问高德地图开放平台的官方网站,在那里你可以找到相关的注册和创建应用的步骤。 2. 在创建应用后,你会得到一个API Key,这是你使用高德地图API的权限凭证。在使用API时,你需要在请求中携带这个API Key。 3. 接下来,你可以根据你的需求选择合适的API接口进行开发高德地图API提供了地图显示、地理编码、逆地理编码、路径规划、导航等一系列功能的接口。 4. 根据你选择的接口,你需要按照API文档中的要求构造API请求,并将API Key携带在请求中。 5. 发送请求后,你会收到API的响应数据。根据响应数据的格式和内容,你可以进行相应的处理和展示。 以下是一些常用的高德地图API接口和功能: - 地理编码:将地址转换为经纬度坐标,可以使用 Geocoding API 实现。 - 逆地理编码:将经纬度坐标转换为具体地址,可以使用 Regeocoding API 实现。 - 路径规划:根据起点和终点计算出最优路径,可以使用 Directions API 实现。 - 导航:提供步行、驾车、骑行等导航功能,可以使用 Navigation API 实现。 - 地图显示:在网页或移动应用中显示地图,可以使用 JavaScript API 或 Android/iOS SDK 实现。 希望以上信息能帮助你初步了解如何使用高德地图API。如果你有其他相关问题,请随时提问。 相关问题: 1. 高德地图API有哪些常用的功能? 2. 如何在网页中使用高德地图? 3. 高德地图API的请求参数有哪些?

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

水煮白菜王

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

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

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

打赏作者

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

抵扣说明:

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

余额充值