未捕获的类型错误:无法读取未定义的 VueJs 和 Leaflet 的属性“_leaflet_id”
2018-02-20
11395
我正在制作一个 vue 项目,我想在我的组件中使用 leaflet。我得到了地图显示,我可以添加标记,但是当我尝试删除标记时遇到错误。我得到
Uncaught TypeError: Cannot read property '_leaflet_id' of undefined at n (leaflet.js:5) at e.removeLayer (leaflet.js:5) at HTMLInputElement.eval (VM119323 App.vue:74) at HTMLInputElement.dispatch (jquery.js:3058) at HTMLInputElement.eventHandle (jquery.js:2676)
<template>
<div id="app" class="container-fluid">
<div class="row">
<div class="col-md-9">
<div id="map" class="map" style="height: 781px;"></div>
</div>
<div class="col-md-3">
</div>
</div>
<router-view/>
</div>
</template>
<script>
export default {
name: "App",
data() {
return {
map: null,
markers: [],
mapSW: [0, 4096],
mapNE: [4096, 0],
tileLayer: null
};
},
mounted() {
this.initMap();
this.initLayers();
this.onClick();
this.onPopupOpen();
},
computed: {
popupContent: function() {
return "<input type='button' value='Delete' class='marker-delete-button' /> <br> <input type='button' value='Add Event' class='add-event'/>";
}
},
methods: {
initMap() {
this.map = L.map("map").setView([0, 0], 1);
this.tileLayer = L.tileLayer("/static/map/{z}/{x}/{y}.png", {
maxZoom: 4,
minZoom: 3,
continuousWorld: false,
noWrap: true,
crs: L.CRS.Simple
});
this.tileLayer.addTo(this.map);
this.map.on("click", this.onClick, this);
this.map.setMaxBounds(
L.LatLngBounds(L.latLng(this.mapSW), L.latLng(this.mapNW))
);
},
initLayers() {},
onClick(e) {
var newMarker = L.marker(e.latlng, {
draggable: true
})
.addTo(this.map)
.bindPopup(this.popupContent);
this.markers.push(newMarker);
newMarker.on("popupopen", this.onPopupOpen, this);
},
onPopupOpen(index) {
$(".marker-delete-button:visible").click(() => {
this.map.removeLayer(this.newMarker);
});
}
}
};
</script>
2个回答
newMarker
变量的作用域应属于组件,以便您以后能够将其删除。目前,它仅存在于
onClick
方法内。您可以在
此处
阅读有关变量作用域的更多信息。为了解决您的问题,您需要将
newMarker
添加到
data()
函数:
// ...
data() {
return {
// ...
tileLayer: null,
newMarker: null
};
},
// ...
onClick(e) {
this.newMarker = L
.marker(e.latlng, { draggable: true })
.addTo(this.map)
.bindPopup(this.popupContent);
this.markers.push(this.newMarker);
this.newMarker.on("popupopen", this.onPopupOpen, this);
},
onPopupOpen(index) {
$(".marker-delete-button:visible").click(() => {
this.map.removeLayer(this.newMarker);
});
}
您的完整代码将类似于:
<template>
<div id="app" class="container-fluid">
<div class="row">
<div class="col-md-9">
<div id="map" class="map" style="height: 781px;"></div>
</div>
<div class="col-md-3">
</div>
</div>
<router-view/>
</div>
</template>
<script>
export default {
name: "App",
data() {
return {
map: null,
markers: [],
mapSW: [0, 4096],
mapNE: [4096, 0],
tileLayer: null,
newMarker: null
};
},
mounted() {
this.initMap();
this.initLayers();
this.onClick();
this.onPopupOpen();
},
computed: {
popupContent: function() {
return "<input type='button' value='Delete' class='marker-delete-button' /> <br> <input type='button' value='Add Event' class='add-event'/>";
}
},
methods: {
initMap() {
this.map = L.map("map").setView([0, 0], 1);
this.tileLayer = L.tileLayer("/static/map/{z}/{x}/{y}.png", {
maxZoom: 4,
minZoom: 3,
continuousWorld: false,
noWrap: true,
crs: L.CRS.Simple
});
this.tileLayer.addTo(this.map);
this.map.on("click", this.onClick, this);
this.map.setMaxBounds(
L.LatLngBounds(L.latLng(this.mapSW), L.latLng(this.mapNW))
);
},
initLayers() {},
onClick(e) {
this.newMarker = L
.marker(e.latlng, { draggable: true })
.addTo(this.map)
.bindPopup(this.popupContent);
this.markers.push(this.newMarker);
this.newMarker.on("popupopen", this.onPopupOpen, this);
},
onPopupOpen(index) {
$(".marker-delete-button:visible").click(() => {
this.map.removeLayer(this.newMarker);
});
}
}
};
</script>
daniloisr
2018-02-20
您还可以做的是在删除标记之前验证它是否确实在地图上,使用 hasLayer 方法,如下所示:
if (this.map.hasLayer(this.newMarker)) this.map.removeLayer(this.newMarker);
hnrd
2020-09-29