在传单中创建标记的问题
Issue in creating marker in leaflet
我正在尝试使用传单创建标记 angular 8.The 以下示例工作正常。
但是在 addMarker 方法中,当我更改纬度和经度(有效纬度)的值时,不再创建标记。
有人可以解释一下吗?
LAYER_OSM = tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 18, attribution: 'Open Street Map' });
// Values to bind to Leaflet Directive
options = {
layers: [ this.LAYER_OSM ],
zoom: 10,
center: latLng(46.879966, -121.726909)
};
markers: Layer[] = [];
addMarker() {
const newMarker = marker(
[ 46.879966 + 0.1 * (Math.random() - 0.5), -121.726909 + 0.1 * (Math.random() - 0.5) ],
{
icon: icon({
iconSize: [ 25, 41 ],
iconAnchor: [ 13, 41 ],
iconUrl: 'leaflet/marker-icon.png',
shadowUrl: 'leaflet/marker-shadow.png'
})
}
);
this.markers.push(newMarker);
}
removeMarker() {
this.markers.pop();
}
}
我不确定你的问题出在哪里。检查这个演示。无论您放置哪个标记值,它都有效
<div
style="height: 90vh;"
leaflet
[leafletOptions]="options"
(leafletMapReady)="onMapReady($event)"
></div>
<button (click)="addMarker()">
add marker
</button>
组件:
map;
markers: L.Layer[] = [];
popupText = "Some popup text";
markerIcon = {
icon: L.icon({
iconSize: [25, 41],
iconAnchor: [10, 41],
popupAnchor: [2, -40],
// specify the path here
iconUrl: "https://unpkg.com/leaflet@1.4.0/dist/images/marker-icon.png",
shadowUrl: "https://unpkg.com/leaflet@1.4.0/dist/images/marker-shadow.png"
})
};
options = {
layers: [
L.tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
maxZoom: 18,
attribution: ""
})
],
zoom: 10,
center: L.latLng(46.879966, -121.726909)
};
addMarker() {
// const newMarker = L.marker(
// [
// 46.879966 + 0.1 * (Math.random() - 0.5),
// -121.726909 + 0.1 * (Math.random() - 0.5)
// ],
// this.markerIcon
// );
const newMarker = L.marker([46.879966, -121.726909], this.markerIcon);
this.markers.push(newMarker);
newMarker.addTo(this.map);
}
onMapReady(map: L.Map) {
this.map = map;
// Do stuff with map
}
我正在尝试使用传单创建标记 angular 8.The 以下示例工作正常。
但是在 addMarker 方法中,当我更改纬度和经度(有效纬度)的值时,不再创建标记。 有人可以解释一下吗?
LAYER_OSM = tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 18, attribution: 'Open Street Map' });
// Values to bind to Leaflet Directive
options = {
layers: [ this.LAYER_OSM ],
zoom: 10,
center: latLng(46.879966, -121.726909)
};
markers: Layer[] = [];
addMarker() {
const newMarker = marker(
[ 46.879966 + 0.1 * (Math.random() - 0.5), -121.726909 + 0.1 * (Math.random() - 0.5) ],
{
icon: icon({
iconSize: [ 25, 41 ],
iconAnchor: [ 13, 41 ],
iconUrl: 'leaflet/marker-icon.png',
shadowUrl: 'leaflet/marker-shadow.png'
})
}
);
this.markers.push(newMarker);
}
removeMarker() {
this.markers.pop();
}
}
我不确定你的问题出在哪里。检查这个演示。无论您放置哪个标记值,它都有效
<div
style="height: 90vh;"
leaflet
[leafletOptions]="options"
(leafletMapReady)="onMapReady($event)"
></div>
<button (click)="addMarker()">
add marker
</button>
组件:
map;
markers: L.Layer[] = [];
popupText = "Some popup text";
markerIcon = {
icon: L.icon({
iconSize: [25, 41],
iconAnchor: [10, 41],
popupAnchor: [2, -40],
// specify the path here
iconUrl: "https://unpkg.com/leaflet@1.4.0/dist/images/marker-icon.png",
shadowUrl: "https://unpkg.com/leaflet@1.4.0/dist/images/marker-shadow.png"
})
};
options = {
layers: [
L.tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
maxZoom: 18,
attribution: ""
})
],
zoom: 10,
center: L.latLng(46.879966, -121.726909)
};
addMarker() {
// const newMarker = L.marker(
// [
// 46.879966 + 0.1 * (Math.random() - 0.5),
// -121.726909 + 0.1 * (Math.random() - 0.5)
// ],
// this.markerIcon
// );
const newMarker = L.marker([46.879966, -121.726909], this.markerIcon);
this.markers.push(newMarker);
newMarker.addTo(this.map);
}
onMapReady(map: L.Map) {
this.map = map;
// Do stuff with map
}