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

Leaflet根据坐标画圆形区域

在做地图应用时,有时需要根据指定的坐标来画一个圆形区域,比如签到打卡类的应用,此时我们可以使用 leaflet.Circle 来在在指定坐标上创建一个圆并添加到的地图上,其中可以通过 radius 属性可以指定区域半径,比如:

const circle = leaflet.circle([lat, lng], {color: 'red',fillColor: 'red',fillOpacity: 0.5,radius: 100,
}).addTo(this.map);

完整例子如下

import { Component, OnInit, AfterViewInit } from "@angular/core";
import * as leaflet from "leaflet";@Component({selector: "app-map-circle",templateUrl: "./map-circle.component.html",styleUrls: ["./map-circle.component.css"],
})
export class MapCircleComponent 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], 16);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);let marker: leaflet.Marker | null = null;let circle: leaflet.Circle | null = null;this.map.on("click", (e) => {console.log(e.latlng);if (marker) {this.map.removeLayer(marker);}const icon = leaflet.icon({iconUrl: "media/marker-icon.png",iconSize: [25, 41],iconAnchor: [12, 41]});marker = leaflet.marker([e.latlng.lat, e.latlng.lng], {icon: icon}).addTo(this.map);if (circle) {this.map.removeLayer(circle);}circle = this.createCircle(e.latlng.lat, e.latlng.lng, "red").addTo(this.map);});}private createCircle(lat: number, lng: number, color: string): leaflet.Circle {return leaflet.circle([lat, lng], {color: color,fillColor: color,fillOpacity: 0.5,radius: 100,});}
}

相关文章:

  • RK3588 DRM 介绍及调试记录
  • 力扣刷题Day 53:和为 K 的子数组(560)
  • 消息队列在异步推理任务中的作用
  • 86. Java 数字和字符串 - 数字
  • java刷题(6)
  • 部署n8n
  • 树莓派开箱上手教程(无需显示器版)
  • 战略-2.1 -战略分析(PEST/五力模型/成功关键因素)
  • 基于蒙特卡罗 AWGN信道调制信号互信息(信道容量)
  • Flink运行架构及并行度设置
  • P8943 Deception Point
  • python语法学习
  • C++ STL
  • DVWA通关笔记-靶场安装教程
  • [爬虫知识] IP代理
  • 从神经生物学到社会心理学:游戏沉迷机制的深度解构
  • 148. 排序链表
  • 大般涅槃经卷第三十五
  • 基于 ColBERT 框架的后交互 (late interaction) 模型速递:Reason-ModernColBERT
  • 车载中央域控制器测试【BCM模块介绍-外灯3】
  • 福田做棋牌网站建设多少钱/最全的百度网盘搜索引擎
  • 企业局域网组建与网站建设/seo运营做什么
  • 网站友情链接怎么做/谷歌在线浏览入口
  • 网站在建设中无法访问/武汉网站seo推广公司
  • 网站建设要什么知识/淘宝seo排名优化软件
  • 网站建设进度图/产品seo基础优化