You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

在Angular 15.1.6/TypeScript项目中集成Leaflet 1.9.4的配置咨询

在Angular 15.1.6/TypeScript项目中集成Leaflet 1.9.4的配置咨询

最近我打算在自己的Angular 15.1.6/TypeScript项目里集成Leaflet 1.9.4来实现地图功能,目前已经完成了以下几步配置,想请教下这样的配置是否正确,有没有遗漏或者需要优化的地方:

1. 引入Leaflet样式文件

在项目的index.html中添加了Leaflet的CDN样式链接,确保地图能正常显示样式:

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
 integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
 crossorigin=""/>

2. 添加地图容器

app.component.html里放置了地图的容器元素,后续Leaflet会将地图渲染到这个div中:

<div id="map"></div>

3. 编写地图初始化逻辑

app.component.ts中导入Leaflet库,并在组件初始化时创建地图实例,设置初始视图和瓦片图层:

import * as L from 'leaflet';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'LeafletExp';
  latlngs!: L.LatLngExpression[];
  line!: L.Polyline;
  map!: L.Map;

  ngOnInit(): void {
    this.map = L.map('map').setView([50.505, 30.09], 13);
    L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
      maxZoom: 19,
      attribution: '&copy; <a href="http://www.openstreetmap.org/copyri...'
    });
  }
}

备注:内容来源于stack exchange,提问作者mli

火山引擎 最新活动