Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when gpx contains Waypoints #107

Closed
ypcyc opened this issue Mar 15, 2021 · 4 comments
Closed

Error when gpx contains Waypoints #107

ypcyc opened this issue Mar 15, 2021 · 4 comments

Comments

@ypcyc
Copy link

ypcyc commented Mar 15, 2021

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?

<wpt lat="xxx" lon="yyy"> <name>Name</name> </wpt>

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
Copy link
Owner

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
Copy link

jengalas commented Mar 28, 2021

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
Copy link

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
Copy link
Owner

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants