Skip to content

使用 div 自定义 icon 的样式

使用 DivIcon 可以更加灵活的自定义 icon 样式。

但大批量使用 DivIcon 可能会导致性能问题,所以需要在使用时注意。

示例

代码实现

vue
<script setup>
import { ref, defineAsyncComponent } from 'vue';

const InitMap = defineAsyncComponent(() =>
  import('../../components/InitMapTianditu.vue')
);

const mapObj = ref();

// 单个 marker 创建
const marker = ref();
const createMarker = () => {
  if (marker.value) {
    mapObj.value.removeLayer(marker.value);
    marker.value = null;
  }

  // 创建一个 marker
  const customIcon = L.divIcon({
    className: 'custom-marker-style',
    html: `<div class="name">自定义 Icon</div><div class="icon"></div>`,
    iconSize: [141, 102.6]
  });

  marker.value = L.marker([32.0237855, 118.8075675], {
    riseOnHover: true,
    icon: customIcon
  }).addTo(mapObj.value);

  // 为 marker 绑定 popup
  marker.value.bindPopup('<b>Hello world!</b><br />这是默认 marker 绑定的 Popup', {offset: [0, -40]});

  // 为 marker 绑定点击事件
  marker.value.on('click', (e) => {
    console.log('默认 marker 触发点击事件', e);
  });
};

const mapLoad = (map) => {
  mapObj.value = map;

  createMarker();
};
</script>

<template>
  <init-map @map-load="mapLoad"></init-map>
</template>

<style lang="scss">
.custom-marker-style {
  display: flex !important;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  .name {
    width: 141px;
    height: 52.5px;
    line-height:40px;
    font-size: 18px;
    font-weight: 500;
    color: #fff;
    text-align: center;
    background: url('../../public/markerCustom/top-bg.png') no-repeat center;
    background-size: 100% 100%;
  }

  .icon {
    width: 38px;
    height: 49.13px;
    background: url('../../public/markerCustom/icon.png') no-repeat center;
    background-size: 100% 100%;
  }
}
</style>
vue
<script setup>
import { onMounted, onUnmounted, ref } from 'vue';
// todo 项目使用请放开 leaflet 引入
// import L from 'leaflet';

const props = defineProps({
  center: {
    type: Array,
    default: [32.0237855, 118.8075675]
  }
})

const emit = defineEmits(['mapLoad']);

const mapRef = ref();

const initMap = () => {
  const map = L.map(mapRef.value, {
    center: props.center,
    zoom: 11,
    minZoom: 6,
    maxZoom: 20
  });

  const mapType = 'vec';
  L.tileLayer(
    'https://t{s}.tianditu.gov.cn/' +
      mapType +
      '_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=' +
      mapType +
      '&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={x}&TILEROW={y}&TILEMATRIX={z}&tk=b72aa81ac2b3cae941d1eb213499e15e',
    {
      subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
      attribution:
        '&copy; <a href="http://lbs.tianditu.gov.cn/home.html">天地图 GS(2022)3124号 - 甲测资字1100471</a>'
    }
  ).addTo(map);

  const mapLabelType = 'cva';
  L.tileLayer(
    'https://t{s}.tianditu.gov.cn/' +
      mapLabelType +
      '_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=' +
      mapLabelType +
      '&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL={x}&TILEROW={y}&TILEMATRIX={z}&tk=b72aa81ac2b3cae941d1eb213499e15e',
    {
      subdomains: ['0', '1', '2', '3', '4', '5', '6', '7']
    }
  ).addTo(map);

  // 地图初始化完成发送事件
  emit('mapLoad', map);

  return map;
};

const mapObj = ref();

// 在 onMounted 中初始化地图
onMounted(() => {
  mapObj.value = initMap();
});

const removeMap = () => {
  if (mapObj.value) {
    mapObj.value.remove();
  }
};

// 在组件卸载时删除地图
onUnmounted(() => {
  removeMap();
});
</script>

<template>
  <div ref="mapRef" class="map"></div>
</template>

<style scoped>
.map {
  height: 40vh;
  z-index: 0;
}
</style>

配置参考

贡献者

Released under the MIT License.