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

在Angular中使用Leaflet构建地图应用

Leaflet是一个用于创建地图的JavaScript库,它包含许多功能,并且非常适用于移动设备。

准备

nodejs: v20.15.0
npm: 10.7.0
angular: 19.2.10

创建一个地图应用工程

npx @angular/cli new my-leaflet-app --style=css --routing=false --skip-tests

提示 “Do you want to enable Server-Side Rendering (SSR) and Static Site Generation (SSG/Prerendering)?” 的时候选择 No

创建完成后,启动一下,验证工程可以正常运行。

cd my-leaflet-app
npm start

然后访问 http://localhost:4200/ 验证。

安装leaflet

npm install leaflet @types/leaflet

创建地图组件

npx @angular/cli generate component map --skip-tests

编辑 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,minZoom: 3,},);tiles.addTo(this.map);}
}

编辑 map.component.html 文件

<div class="map-container"><div class="map-frame"><div id="map"></div></div>
</div>

编辑 map.component.css 文件

.map-container {position: absolute;top: 0;left: 0;right: 0;bottom: 0;margin: 30px;
}.map-frame {border: 2px solid black;height: 300px;width: 500px;
}#map {height: 100%;
}

使用地图组件

编辑 app.component.ts 文件,导入地图组建

import { Component } from "@angular/core";
import { MapComponent } from "./map/map.component";@Component({selector: "app-root",imports: [MapComponent],templateUrl: "./app.component.html",styleUrl: "./app.component.css",
})
export class AppComponent {title = "my-leaflet-app";
}

编辑 app.component.html 文件,在视图中添加地图组件

<app-map></app-map>

最后修改 angular.json 文件,在编译选项中添加 “./node_modules/leaflet/dist/leaflet.css”,如下:

{"projects": {"my-leaflet-app": {"architect": {"build": {"options": {"styles": ["./node_modules/leaflet/dist/leaflet.css","src/styles.css"],},},}}}
}

验证

启动服务 npm start,然后访问 http://localhost:4200/

http://www.xdnf.cn/news/456985.html

相关文章:

  • 【实战教程】从零实现DeepSeek AI多专家协作系统 - Spring Boot+React打造AI专家团队协作平台
  • 服务器连接多客户端
  • 8.ADC
  • 常用Playwright代码片段-Part4
  • π0: A Vision-Language-Action Flow Model for General Robot Control
  • PowerBI链接EXCEL实现自动化报表
  • 【Linux系统】从 C 语言文件操作到系统调用的核心原理
  • vscode c++编译onnxruntime cuda 出现的问题
  • VScode各文件转化为PDF的方法
  • 赛博放生:用数字技术重构心灵仪式
  • 各个历史版本mysql/tomcat/Redis/Jdk/Apache下载地址
  • 【深度学习之四】知识蒸馏综述提炼
  • Golang基础知识—cond
  • 51c~C语言~合集5
  • Python Bug 修复案例分析:asyncio 事件循环异常引发的程序崩溃 两种修复方法
  • 深度解析 IDEA 集成 Continue 插件:提升开发效率的全流程指南
  • 2025长三角杯数学建模A题:智能手机产品设计优化与定价问题,赛题发布与思路分析
  • 2025.05.14华为机考笔试题-第一题-100分
  • 边缘计算模块
  • 解密企业级大模型智能体Agentic AI 关键技术:MCP、A2A、Reasoning LLMs-docker MCP解析
  • 开源GPU架构RISC-V VCIX的深度学习潜力测试:从RTL仿真到MNIST实战
  • 1、数据结构与算法(Python版-啃书)-绪论
  • CodeEdit:macOS上一款可以让Xcode退休的IDE
  • React 第四十一节Router 中 useActionData 使用方法案例以及注意事项
  • SQL笔记一
  • C#.NET 或 VB.NET Windows 窗体中的 DataGridView – 技巧、窍门和常见问题
  • 资产管理系统评测:功能、易用性、性价比全面对比
  • [C++面试] lambda面试点
  • 使用gitbook 工具编写接口文档或博客
  • AWS EC2 微服务 金丝雀发布(Canary Release)方案