当前位置: 首页 > ds >正文

Leaflet使用SVG创建动态Legend

接前一篇文章,前一篇文章我们使用 SVG 创建了带有动态文字的图标,今天再看看怎样在地图上根据动态图标生成相关的legend,当然这里也还是使用了 SVG 来生成相关颜色的 legend。

看下面的代码,生成了一个 svg 节点,其中包含了一个带有颜色的圆形图标和一个文字说明。

private generateLegend(name: string, color: string): string {return `<svgversion="1.2"baseProfile="tiny"xmlns="http://www.w3.org/2000/svg"width="16"height="16"viewBox="0 0 30 30"><circle cx="20" cy="20" r="10" fill="${color}" /></svg><span style="margin-left: 4px;">${name}</span>`;
}

完整的 map.component.ts 文件如下,其它代码参考前一篇文章。

import { Component, OnInit, AfterViewInit } from "@angular/core";
import * as leaflet from "leaflet";@Component({selector: "app-map",templateUrl: "./map.component.html",styleUrls: ["./map.component.css"],
})
export class MapComponent implements OnInit, AfterViewInit {map!: leaflet.Map;constructor() {}ngOnInit(): void {}ngAfterViewInit(): void {this.initMap();}private initMap(): void {this.map = leaflet.map("map").setView([51.5, -0.09], 13);const tiles = leaflet.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png",{maxZoom: 19,attribution:'&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',},);tiles.addTo(this.map);const clients = [{ name: "Client A", lat: 51.5, lng: -0.09, value: 7, color: "red" },{ name: "Client B", lat: 51.5, lng: -0.07, value: 7, color: "blue" },{ name: "Client C", lat: 51.5, lng: -0.11, value: 7, color: "green" },];clients.forEach((client) => {this.generateMarker(client.lat, client.lng, client.value, client.color);});const generateLegend = this.generateLegend;const legend = leaflet.control.scale({ position: "bottomleft" });legend.onAdd = function () {const div = leaflet.DomUtil.create("div", "info");let html = `<div style="width: 80px; height: 80px; background-color: lightgray;">`;html += `<strong>Categories</strong><br/>`;clients.forEach((client) => {html += generateLegend(client.name, client.color) + "<br/>";});html += `</div>`;div.innerHTML = html;return div;};legend.addTo(this.map);}private generateMarker(lat: number,lng: number,value: number,color: string,) {const circleSVGHtml = `<svg version="1.2" baseProfile="tiny" xmlns="http://www.w3.org/2000/svg" width="250" height="250"><circle cx="125" cy="125" r="100" fill="${color}"/><text x="50%" y="50%" text-anchor="middle" fill="white" font-size="100px" font-family="Arial" dy=".2em">${value}</text></svg>`;const iconURL = "data:image/svg+xml," + encodeURIComponent(circleSVGHtml);const circleIcon = leaflet.icon({iconUrl: iconURL,iconSize: [30, 30],});const marker = leaflet.marker([lat, lng], { icon: circleIcon }).addTo(this.map);return marker;}private generateLegend(name: string, color: string): string {return `<svgversion="1.2"baseProfile="tiny"xmlns="http://www.w3.org/2000/svg"width="16"height="16"viewBox="0 0 30 30"><circle cx="20" cy="20" r="10" fill="${color}" /></svg><span style="margin-left: 4px;">${name}</span>`;}
}
http://www.xdnf.cn/news/7056.html

相关文章:

  • 文件读取漏洞路径与防御总结
  • AI日报 - 2024年5月17日
  • PyTorch实现三元组损失Triplet Loss
  • 风控域——风控决策引擎系统设计
  • 考研数学微分学(第三,四,五,六,七讲)
  • 【前端基础】HTML元素隐藏的四个方法(display设置为none、visibikity设置为hidden、rgba设置颜色、opacity设置透明度)
  • 软件设计师教程—— 第二章 程序设计语言基础知识(上)
  • Spatial Transformer Layer
  • Vue3学习(组合式API——ref模版引用与defineExpose编译宏函数)
  • 信贷域——互联网金融业务
  • 低空经济发展现状与前景
  • 聚集索引 vs. 非聚集索引
  • 恒大歌舞团全集
  • Android 14 解决打开app出现不兼容弹窗的问题
  • 参考工具/网站
  • scss additionalData Can‘t find stylesheet to import
  • 强化学习入门:马尔科夫奖励过程二
  • 什么是API接口?API接口的核心价值
  • 网关GateWay——连接不同网络的关键设备
  • STM32IIC实战-OLED模板
  • TC3xx学习笔记-UCB BMHD使用详解(二)
  • 使用NVM管理node版本
  • GO语言学习(二)
  • CSS 浮动与定位以及定位中z-index的堆叠问题
  • 设计练习 - Movie Review Aggregator System
  • 探秘Transformer系列之(33)--- DeepSeek MTP
  • 【爬虫】DrissionPage-6
  • MapReduce 原理深度剖析:从任务执行到参数配置
  • AI编码代理的崛起 - AlphaEvolve与Codex的对比分析引言
  • 61. 旋转链表