tofuninjah / geoxml3

Automatically exported from code.google.com/p/geoxml3
0 stars 0 forks source link

Issue when toggling hide/show for multiple docs loaded with parseKmlString #114

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Initialize map:

myParser = new geoXML3.parser({map: map});

2. Load KML string for each file and create a Show/Hide toggle button for each 
file:

function handleFileSelectDrop(evt) {
    evt.stopPropagation();
    evt.preventDefault();

    var files = evt.dataTransfer.files; // FileList object.
    // files is a FileList of File objects. List some properties.
    //var output = [];
    for (var i = 0, f; f = files[i]; i++) {
      //output.push('<li><strong>', escape(f.name), '</strong>', '</li>');
      var fr = new FileReader();
      fr.onload = function () {
            myParser.parseKmlString(this.result);
        };
      fr.readAsText(files[i]);
      //myParser.parse('http://localhost/Uploads/chicago.kml');

      var button = document.createElement('button');
      button.id = 'kmlShowHide' + ((+i) + (+loadedKmlLayers));
      button.innerHTML = 'Hide: ' + escape(f.name);
      button.onclick = kmlHideShowToggle;
      document.getElementById('kmlLayers').appendChild(button);
      var br = document.createElement("br");
      document.getElementById('kmlLayers').appendChild(br);
      loadedKmlLayers += 1;
    }
    //document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
  }

3. Click the Show/Hide button and execute the following:

  function kmlHideShowToggle(){
    var buttonId = this.id;
    var kmlIndex = buttonId.substring(11,12);
    alert(kmlIndex);
    alert(myParser.docs[0].placemarks.length);

    var buttonName = this.innerHTML;
    var buttonNameArray = buttonName.split(': ');
    var hideShowText = buttonNameArray[0];
    var buttonFileName = buttonNameArray[1];

    if (hideShowText == 'Hide') {
      myParser.hideDocument(myParser.docs[kmlIndex]);
      document.getElementById(this.id).innerHTML = 'Show: ' + buttonFileName;
    } else {
      myParser.showDocument(myParser.docs[kmlIndex]);
      document.getElementById(this.id).innerHTML = 'Hide: ' + buttonFileName;
    }

    return false;
  }

4. Observe that hideDocument is executed and the setMap property for the 
gpolylines for the doc is set to null.

With alert statements, I verified this.

5. Observe that (!docsByUrl[doc.baseUrl]) in line 932 of geoxml3.js evaluates 
to true for the first parseKmlString call, but evaluates to false for 
subsequent calls:

if (!doc.internals.parseOnly) {
      // geoXML3 is not being used only as a real-time parser, so keep the processed documents around
      if (!docsByUrl[doc.baseUrl]) {
        docs.push(doc);
        docsByUrl[doc.baseUrl] = doc;
      }
      else {
        // internal replacement, which keeps the same memory ref loc in docs and docsByUrl
        for (var i in docsByUrl[doc.baseUrl]) {
          docsByUrl[doc.baseUrl][i] = doc[i];
        }
      }
    }

6. The button that toggles hiding/showing the first KML document doesn't work.  
The buttons for the other documents may or may not work.

What is the expected output? 
Each button should toggle it's corresponding KML doc.

Proposed resolution:
In my code, I had to change this line:
            myParser.parseKmlString(this.result);
to this:
            myParser.parseKmlString(this.result, myParser.docs);

therefore, should the ParserReference doc here:
            http://code.google.com/p/geoxml3/wiki/ParserReference
should be changed from this:
            parseKmlString(string: String)
to this?
            parseKmlString(string: String, docSet: Object)

Line 124 has this option:
      docSet: docSet || [],
Does that mean that docSet is optional and that if it's not passed in, then to 
use an empty array?  Do we ever want to do that?  Another way to implement the 
same fix above is to change this:
  var parseKmlString = function (kmlString, docSet) {
    // Internal values for the set of documents as a whole
    var internals = {
      parser: this,
      docSet: docSet || [],

to this:

  var parseKmlString = function (kmlString) {
    // Internal values for the set of documents as a whole
    var internals = {
      parser: this,
      docSet: docs,
maybe that's better?

For line 932 in geoxml3.js:
      if (!docsByUrl[doc.baseUrl]) {
we are actually not concerned with docsByUrl being updated here because we 
called parseKmlString and didn't provide a URL.  Also, when we make subsequent 
calls to parseKmlString, we don't provide a docsByUrl array parameter (such as 
when we provide the docSet parameter), so it's not preserved, anyways.

The problem is that we don't ever want to call line 934:
        docs.push(doc);
as long as we're passing in a docSet variable with parseKmlString.

For now, I commented out the whole if statement block on line 930, but I'm ONLY 
calling parseKmlString.

I hope this helps!  Thanks!

What version of the product are you using? 
r127 in the kmz branch.

On what operating system?
Win7 w/ Chrome

Original issue reported on code.google.com by PJSimon4...@gmail.com on 1 Jul 2015 at 8:49

Attachments:

GoogleCodeExporter commented 8 years ago
Can you provide a sample HTML/javascript file to reproduce the issue?
(thank you for the KML files) 

Original comment by geocodezip on 2 Jul 2015 at 2:57

GoogleCodeExporter commented 8 years ago
Here you go!  I had to clear out my value for Inherits in line 1 of the aspx 
file.  Just put in your own value there.

Original comment by PJSimon4...@gmail.com on 2 Jul 2015 at 10:27

Attachments:

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
This issue was closed by revision r128.

Original comment by geocodezip on 3 Jul 2015 at 4:58

GoogleCodeExporter commented 8 years ago
fixed revision 128
handle case when doc.baseUrl not set

test page:
http://www.geocodezip.com/geoxml3_test/v3_geoxml3_test_parseKmlString.html

Original comment by geocodezip on 3 Jul 2015 at 5:22

GoogleCodeExporter commented 8 years ago
test case with additional KML loaded via URL:

http://www.geocodezip.com/geoxml3_test/v3_geoxml3_test_parseKmlString_linkto.htm
l?filename=http://www.geocodezip.com/geoxml3_test/kml/Illinois.kml

Original comment by geocodezip on 9 Jul 2015 at 6:15