Dyl*_*lan 3 leaflet typescript angular ngx-leaflet
我曾尝试了一段时间,现在实现这个小册子教程使用ngx-leaflet
。
在遵循本教程时,绝对没有关于如何实现自定义控件或图例的明确文档。
var info = L.control();
info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
this.update();
return this._div;
};
// method that we will use to update the control based on feature properties passed
info.update = function (props) {
this._div.innerHTML = '<h4>US Population Density</h4>' + (props ?
'<b>' + props.name + '</b><br />' + props.density + ' people / mi<sup>2</sup>'
: 'Hover over a state');
};
info.addTo(map);
Run Code Online (Sandbox Code Playgroud)
创造传奇也是如此。
任何人都可以指出我在正确的方向上尝试使用 ngx-leaflet lib 在 Angular 7 中实现这一点吗?
import { control, featureGroup, geoJson, icon, latLng, LatLngExpression, Map, Marker, marker, popup, tileLayer } from 'leaflet';
onMapReady(map: Map) {
this.map = map;
// create info control
let info = control(
{
onAdd: map => {
}
}
)
info.addTo(map);
Run Code Online (Sandbox Code Playgroud)
我得到你需要做一些像这样,但我不希望添加一个圆形或形状,但自定义控制在上面的截图,以及一个传奇。
要使此示例工作,您唯一需要做的就是侦听 onMapReady 事件并将本教程的所有代码放在其中。地图引用是您想要的,它是此函数的参数。
具体来说:
<div
style="height: 100vh"
leaflet
[leafletOptions]="options"
(leafletMapReady)="onMapReady($event)"
></div>
Run Code Online (Sandbox Code Playgroud)
ts:
onMapReady(map) {
// place all the tutorial code inside here
...
// control that shows state info on hover
let info = L.control();
// here you want the reference to be info, therefore this = info
// so do not use es6 to access the the class instance
info.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info');
this.update();
return this._div;
};
// also here you want the reference to be info, therefore this = info
// so do not use es6 to access the class instance
info.update = function (props) {
this._div.innerHTML = '<h4>US Population Density</h4>' + (props ?
'<b>' + props.name + '</b><br />' + props.density + ' people / mi<sup>2</sup>'
: 'Hover over a state');
};
info.addTo(map);
...
const legend = L.control({ position: 'bottomright' });
legend.onAdd = map => {
let div = L.DomUtil.create('div', 'info legend'),
grades = [0, 10, 20, 50, 100, 200, 500, 1000],
labels = [],
from, to;
for (var i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
labels.push(
'<i style="background:' + getColor(from + 1) + '"></i> ' +
from + (to ? '–' + to : '+'));
}
div.innerHTML = labels.join('<br>');
return div;
};
legend.addTo(map);
}
Run Code Online (Sandbox Code Playgroud)
使用 ngx-leaflet检查Angular 7 中完整示例的演示