mpetazzoni / leaflet-gpx

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

Error when gpx contains Waypoints #107

Closed ypcyc closed 3 years ago

ypcyc commented 3 years ago

When I use gpx with route, everything works fine. when I add at least one waypoint I get error and maps is not shown even with route:

Uncaught TypeError: Cannot read property 'length' of null at i._parse_gpx_data (VM463 gpx.min.js:formatted:366) at i (VM463 gpx.min.js:formatted:293) at XMLHttpRequest.o.onreadystatechange (VM463 gpx.min.js:formatted:286)

I used this, maybe there are some more mandatory parameters that are missing?

`

Name

`

Just check some other waypoints from the Issues, it turned out that <desc> is needed, but according to gpx specification it is not mandatory https://www.topografix.com/gpx_manual.asp

mpetazzoni commented 3 years ago

Hmm, that's weird, it should already correctly handle this. This is how waypoints are parsed:

el = xml.getElementsByTagName('wpt');
for (i = 0; i < el.length; i++) {
  var ll = new L.LatLng(
      el[i].getAttribute('lat'),
      el[i].getAttribute('lon'));

  var nameEl = el[i].getElementsByTagName('name');
  var name = nameEl.length > 0 ? nameEl[0].textContent : null;

  var descEl = el[i].getElementsByTagName('desc');
  var desc = descEl.length > 0 ? descEl[0].textContent : null;

  var symEl = el[i].getElementsByTagName('sym');
  var symKey = symEl.length > 0 ? symEl[0].textContent : null;

  var typeEl = el[i].getElementsByTagName('type');
  var typeKey = typeEl.length > 0 ? typeEl[0].textContent : null;

  // ...
}

As you can see, I just call getElementsByTagName('desc') on each <wpt> element, which returns an empty collection if the sub-element <desc> does not exist; descEl should never be null.

Can you confirm you are using the latest version of leaflet-gpx, and share your code snipped and source GPX file so I can reproduce?

jengalas commented 3 years ago

I had the same problem using v1.5.1 and a .gpx file that includes a waypoint (but no <desc> tag for the waypoint). I added a <desc> tag and found that it works correctly now.

This was not a problem with v1.5.0 - even without the <desc> tag, everything worked as it should in that version.

I can try to work up an example if that would help.

jengalas commented 3 years ago

It is working if I change null in line 367 to the empty string, making it similar to how you coded this part in v1.5.0.

mpetazzoni commented 3 years ago

@jengalas Thanks! The Cannot read property 'length' of null was actually coming from further down in that function because name and desc could now be null instead of empty strings. v1.5.2 contains the fix.