0

I'm using Vue and Leaflet for displaying polygons (zones) on a map. Further I want appropriate information (messages) about the specific polygons to be displayed after clicking on them on the map with the polygon.on function. But it seems that I can not call the getMessages function at that point, I always get the message "Cannot read property 'call' of undefined". Someone has an idea how I can make this work?

var map = new Vue ({
    el: '#messagearea',
    data: {
        function() {
        return {
            map: false
        };
    },
        zones: [],
        messages:[],
    },
    ready: function() {
    this.map = L.map('map').setView([51.959, 7.623], 14);
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(this.map);
        this.$http.get('/api/zones', function(data) {
            this.$set('zones', data);
            for (var i = 0; i < this.zones['Zones'].length; i++) {
                    polygon = L.polygon(
                    this.zones['Zones'][i]['Geometry']['Coordinates']).addTo(this.map);
                    polygon.bindPopup(this.zones['Zones'][i]['Name']);
                    polygon.on('click', this.getMessages(this.zones['Zones'][i]['Zone-id']));                     
                }
        });
    },
    methods:
    {
        getMessages: function(id) {
        this.$http.get('/api/messages?zone='+id, function(data) {
            this.$set('messages', data['Messages']);
        });
    }
    }
})
Flugmango
  • 113
  • 2
  • 11

1 Answers1

1

You're confusing the concepts of function references and function calls. Read Leaflet marker event fires at wrong time - even if the question and setup is different, the solution is the same.

IvanSanchez
  • 18,272
  • 3
  • 30
  • 45