I'm using Leafletjs to display a map on my website. I'm now starting to use Vuejs in it, but I'm getting errors because suddenly this
refers to the Vuejs instance.
Consider the following function which works great outside of Vuejs (taken from here):
var marker_a = new L.Marker(a, {draggable: true}).addTo(map);
marker_a.on('dragstart', dragStartHandler);
function dragStartHandler (e) {
var latlngs = polyline.getLatLngs(),
var latlng = this.getLatLng();
for (var i = 0; i < latlngs.length; i++) {
if (latlng.equals(latlngs[i])) {
this.polylineLatlng = i;
}
}
}
But when I use it in Vuejs:
methods: {
dragStartHandler: function (e) {
var latlngs = this.polyline.getLatLngs();
var latlng = this.getLatLng();latlng
for (var i = 0; i < latlngs.length; i++) {
if (latlng.equals(latlngs[i])) {
this.polylineLatlng = i;
}
}
}
}
You can see that instead of polyline.getLatLngs()
I now use this.polyline.getLatLngs()
because I now load polyline
from the Vuejs context. That works great. Unfortunately this.getLatLng()
gives me an error saying this.getLatLng is not a function
. That makes sense because I never defined getLatLng()
in Vuejs. I'm not sure how to point it to the Leaflet context though.
Could anybody point me in the right direction?