mpetazzoni / leaflet-gpx

A GPX track plugin for Leaflet.js
http://mpetazzoni.github.io/leaflet-gpx
BSD 2-Clause "Simplified" License
545 stars 118 forks source link

cannot read property fitBounds of undefined #98

Closed davidof1977 closed 4 years ago

davidof1977 commented 4 years ago

Hi,

I alway get these error when i try to acces any property inside the loaded event

map;
ngOnInit(): void {
    this.map = L.map('map').setView([40.4929200, -3.8737100], 14);

    L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
    }).addTo(this.map);
    this.addGpx();
  }

 addGpx(){
    const gpx = '../assets/LasRozas-Majadahonda-Pozuelo.gpx';
new L.GPX(gpx, {async: true}).on('loaded', function(e) {
  map.fitBounds(e.target.getBounds());
}).addTo(this.map);
  }

The track shows correctly in the map. But i always get that error. And if i tried to acces any attribute of the gpx, like the distance, i can get it, but i cannot send it to the page, because i can not get acces to any atributte outside the gpx. I apologice for my english.

ibrierley commented 4 years ago

Formatting is a bit messed up, so hard to read, but wondering should it be this.map instead of map ? Maybe post more of the code with fixed formatting will help.

mpetazzoni commented 4 years ago

This is most likely an error in your code as pointed out by @ibrierley. Try with this.map.fitBounds(...).