模板 :
<div id="mapid" style="height: 500px"></div>
我已经安装了传单和传单的打字。当出现错误提示找不到地图容器时,我添加了它的导入。
控制器 :
import { Component, OnInit, EventEmitter, Output } from
'@angular/core';
import * as L from 'leaflet';
import { Map } from 'leaflet';
@Component({
selector: 'app-leafletmap',
templateUrl: './leafletmap.component.html',
styleUrls: ['./leafletmap.component.css']
})
export class LeafletmapComponent implements OnInit {
mymap = L.map('mapid').setView([29.6516, -82.3248], 13);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}',
{
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors,
<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18,
id: 'mapbox.streets',
accessToken: '*****************************'
}).addTo(mymap);
popup = L.popup();
marker: any;
onMapClick(e) {
if (marker != undefined)
mymap.removeLayer(marker)
marker = new L.Marker(e.latlng, { draggable: true });
mymap.addLayer(marker);
popup.setLatLng(e.latlng).setContent("You clicked the map at " + e.latlng.toString()).openOn(mymap);
}
mymap.on('zoomend', function() {
console.log(mymap.getZoom());
})
mymap.on('click', onMapClick);
constructor() { }
ngOnInit() {
}
}
我不确定这是否是在打字稿中传递访问令牌和初始化变量的方法,因为我是按照使用常规 Javascript 的教程编写此代码的。