1.0.72016 • Published 2 years ago

leprechaun-amap v1.0.72016

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

leprechaun-amap

vue2 高德地图组件

地图加载

参数描述必填默认可选
key申请好的 Web 端开发者 Key
plugins需要使用的的插件列表"AMap.ToolBar"见地图插件

地图插件

已支持插件(有些插件需要 addControl(),官方文档没有明确哪些需要)

参数描述
AMap.HawkEye鹰眼,显示缩略图
AMap.ToolBar工具条,控制地图的缩放、平移等
AMap.MapType图层切换,用于几个常用图层切换显示
AMap.HeatMap热力图插件
AMap.GeoJSONgeoJSON 插件
AMap.PolylineEditor折线编辑插件
AMap.PolygonEditor多边形编辑插件
AMap.CircleEditor圆形编辑插件

使用示例

<AMap
  ref="testMap"
  :load="{
        key: 'xxxxxxxxxxxxxxxxxxxxxx',
        plugins: [
          'AMap.ToolBar',
          'AMap.MapType',
          'AMap.HeatMap',
          'AMap.GeoJSON',
        ],
      }"
  @complete="initMap()"
/>

创建地图对象

mapObj 参数描述必填默认可选
mapStyle底图样式amap://styles/lighthttps://lbs.amap.com/api/jsapi-v2/guide/map/map-style
center初始中心经纬度116.419447, 25.049867number,number
zoom地图显示的缩放级别10.52-30 可以设置为浮点数
zooms地图显示的缩放级别范围默认为 [10, 182-30 可以设置为浮点数取值范围 2 ~ 30
viewMode地图视图模式默认为‘2D’可选’3D’
pitch俯仰角度默认 0,最大值根据地图当前 zoom 级别不断增大,2D 地图下无效 。可选’3D’
features设置地图上显示的元素种类'bg','point','road','building''bg'(地图背景)、'point'(POI 点)、'road'(道路)、'building'(建筑物)

使用示例

<AMap
  ref="testMap"
  :load="{
        key: 'xxxxxxxxxxxxxxxxxxxxxx',
        plugins: [
          'AMap.ToolBar',
          'AMap.MapType',
          'AMap.HeatMap',
          'AMap.GeoJSON',
        ],
      }"
  :mapObj="{
        mapStyle: 'amap://styles/fresh',
        zoom: 10,
        zooms: [8, 18],
        center: [116.419447, 25.049867],
        viewMode: '3D',
        pitch: 60,
      }"
  @complete="initMap()"
/>

限定可视范围

props描述必填默认可选
limitBounds取 zoom 为可视范围falseBoolean

热力图层

参数描述必填默认可选
radius热力点的半径30number
data数据列表{longitude:number,latitude:number,count:number}
crs数据列表的坐标系gcj-02bd09、wgs84
value_max数据最大值数据列表的最大值number

使用示例

this.$refs.testMap.initHeatMap({
  data: res.data.map((item) => {
    return { longitude: +item.jd, latitude: +item.wd, count: 1 };
  }),
  crs: "wgs84",
  value_max: 1,
});

添加 Marker

参数描述必填默认可选
data数据列表markerData
crs数据列表的坐标系gcj-02bd09、wgs84
iconmarker 的 iconicon

使用示例

this.$refs.testMap.addMarker({
  data: cz_markerData,
  crs: "bd09",
  icon: {
    image: require("../assets/cz.png"),
    width: 24,
    height: 24,
  },
});

markerData

参数描述必填默认可选
longitude经度number
latitude纬度number
titlemarker 标题string
introduction点击 marker 传递给信息窗的内容string

icon

参数描述必填默认可选
imageicon 图片例:require("../assets/cz.png")
width图片宽度number
height图片高度number

信息窗 infoWindow

props描述必填默认可选
infoWindowHeight信息框最大高度400number
infoWindowWidth信息框最大宽度800number

添加覆盖物

参数描述必填默认可选
data数据列表
crs坐标系gcj-02bd09、wgs84
borderWeight线条宽度20number
strokeColor线条颜色"red"color
editor是否可以编辑falseBoolean

使用示例

this.$refs.testMap.addPolyline({
  data: [
    { longitude: 117.017362, latitude: 25.075884 },
    { longitude: 116.98303, latitude: 25.090809 },
    { longitude: 116.943033, latitude: 25.104645 },
  ],
});
参数描述必填默认可选
data数据列表
crs坐标系gcj-02bd09、wgs84
radius圆半径200number
fillColor多边形填充颜色"fff"color
borderWeight线条宽度,20number
strokeColor线条颜色"blue"color
editor是否可以编辑falseBoolean

使用示例

this.$refs.testMap.addPolyline({
  data: [
    { longitude: 117.017362, latitude: 25.075884 },
    { longitude: 116.98303, latitude: 25.090809 },
    { longitude: 116.943033, latitude: 25.104645 },
  ],
});
参数描述必填默认可选
longitude中心点经度number
latitude中心纬度number
crs坐标系gcj-02bd09、wgs84
radius圆半径200number
fillColor圆形填充颜色"red"color
strokeColor描边颜色"#fff"color
strokeWeight描边宽度2number
editor是否可以编辑falseBoolean

使用示例

this.$refs.testMap.addCircle({
  longitude: 117.017362,
  latitude: 25.075884,
});

GeoJSON

使用示例

this.$refs.testMap.addGeoJson({
  geoJSON: GeoJson_shangHang,
});

事件

事件描述参数
complete地图图块加载完成后触发
mapClick地图点击事件{ target, lnglat, pixel, type }
markerClickmarker 点击事件target
polylineClickpolyline 点击事件e
polygonClickpolygon 点击事件e
circleClickcircle 点击事件e
geoJsonClickgeoJson 点击事件e
polylineChange折线编辑事件target
polygonChange多边形编辑事件target
circleChange圆形编辑事件target

完整使用示例

<!--
 * @Author: ChenWei
 * @Date: 2022-07-13 14:03:43
 * @LastEditors: ChenWei
 * @LastEditTime: 2022-07-19 10:39:11
 * @FilePath: \amap\test\src\components\MapContainer.vue
 * @Description:
 * WeChat:ChenWei990128
 * Copyright (c) 2022 by 福建龙腾大数据信息技术有限公司, All Rights Reserved.
-->
<template>
  <div style="width: 100vw; height: 100vh">
    <AMap
      ref="testMap"
      :load="{
        key: 'xxxxxxxxxxxxxxxxxxxxxxx',
        plugins: [
          'AMap.ToolBar',
          'AMap.MapType',
          'AMap.HeatMap',
          'AMap.GeoJSON',
        ],
      }"
      :mapObj="{
        mapStyle: 'amap://styles/fresh',
        zoom: 10,
        zooms: [8, 18],
        center: [116.419447, 25.049867],
        viewMode: '3D',
        pitch: 60,
      }"
      limitBounds
      @complete="initMap()"
    />
  </div>
</template>

<script>
// 替换成npm包
import AMap from "./AMap.vue";
// 引用geoJson对象
import GeoJson_shangHang from "../assets/shanghang";
export default {
  props: {},
  data() {
    return {
      map: null,
    };
  },
  computed: {},
  created() {},
  mounted() {},
  watch: {},
  methods: {
    // 参考手册 https://lbs.amap.com/api/jsapi-v2/documentation
    async initMap() {
      // 热力图层初始化
      this.initHeatMap();

      // geojson初始化
      this.initGeoJson();

      // 标记点初始化
      this.initMarker();

      // 线面覆盖物初始化
      this.initCover();
    },
    /**
     * @desc 初始化热力图
     */
    initHeatMap() {
      fetch("http://10.56.120.84:9089/jsons/469")
        .then((res) => {
          return res.json();
        })
        .then((res) => {
          this.$refs.testMap.initHeatMap({
            data: res.data.map((item) => {
              return { longitude: +item.jd, latitude: +item.wd, count: 1 };
            }),
            crs: "wgs84",
            value_max: 1,
          });
        });
    },
    /**
     * @desc 初始化标记点
     */
    initMarker() {
      fetch("http://10.56.120.84:9089/jsons/449")
        .then((res) => {
          return res.json();
        })
        .then((res) => {
          // 村 社区
          const cz_markerData = res.data
            .filter((item) => item.administrative_level != "镇")
            .map((item) => {
              return {
                longitude: item.longitude,
                latitude: item.latitude,
                title: item.name,
                introduction: item.introduction,
              };
            });
          this.$refs.testMap.addMarker({
            data: cz_markerData,
            crs: "bd09",
            icon: {
              image: require("../assets/cz.png"),
              width: 24,
              height: 24,
            },
          });
          // 乡镇
          const xz_markerData = res.data
            .filter((item) => item.administrative_level == "镇")
            .map((item) => {
              return {
                longitude: item.longitude,
                latitude: item.latitude,
                title: item.name,
                introduction: item.introduction,
              };
            });
          this.$refs.testMap.addMarker({
            data: xz_markerData,
            crs: "bd09",
            icon: {
              image: require("../assets/xz.png"),
              width: 36,
              height: 36,
            },
          });
        });
    },

    /**
     * @desc 初始化覆盖物
     */
    initCover() {
      this.$refs.testMap.addPolyline({
        data: [
          { longitude: 117.017362, latitude: 25.075884 },
          { longitude: 116.98303, latitude: 25.090809 },
          { longitude: 116.943033, latitude: 25.104645 },
        ],
      });
      this.$refs.testMap.addCircle({
        longitude: 117.017362,
        latitude: 25.075884,
      });
    },
    /**
     * @desc 初始化geojson数据
     */
    initGeoJson() {
      this.$refs.testMap.addGeoJson({
        geoJSON: GeoJson_shangHang,
      });
    },
  },
  components: { AMap },
};
</script>

<style scoped>
#container {
  padding: 0px;
  margin: 0px;
  width: 100%;
  height: 45vh;
  background-color: #f3f4f5;
}
</style>