0

new information begins I was able to make a little headway by changing one line in the "existing" code (that is, in the code I am borrowing), replacing responseXML with responseText. That fixed a key problem in that the variable details now contains the desired data.

I also noticed while looking at "working example" link containing util.js that an additional function is being employed xmlParse(str). I don't see or understand how xmlParse(str) is used, but that likely is my naiveté with respect to javascript.

function xmlParse(str) {
  if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') {
    var doc = new ActiveXObject('Microsoft.XMLDOM');
    doc.loadXML(str);
    return doc;
  }

So is the new version of Google Maps (V3) using XML when it did not before, or what? Anyhow, I will likely need to open a new question about the next error I already am experiencing in main.js, whatever that is.

new information ends

a complete rewrite begins

This first script is available here. I have added some console.log() results to show intermediate results that confirm that the information is contained in request.responseText.

/**
* Returns an XMLHttp instance to use for asynchronous
* downloading. This method will never throw an exception, but will
* return NULL if the browser does not support XmlHttp for any reason.
* @return {XMLHttpRequest|Null}
*/
function createXmlHttpRequest() {
 try {
   if (typeof ActiveXObject != 'undefined') {
     return new ActiveXObject('Microsoft.XMLHTTP');
   } else if (window["XMLHttpRequest"]) {
     return new XMLHttpRequest();
   }
 } catch (e) {
   changeStatus(e);
 }
 return null;
};

/**
* This functions wraps XMLHttpRequest open/send function.
* It lets you specify a URL and will call the callback if
* it gets a status code of 200.
* @param {String} url The URL to retrieve
* @param {Function} callback The function to call once retrieved.
*/
function downloadUrl(url, callback) {
 var status = -1;
 var request = createXmlHttpRequest();
 if (!request) {
   return false;
 }

 request.onreadystatechange = function() {
   if (request.readyState == 4) {
     try {
       status = request.status;
     } catch (e) {
       // Usually indicates request timed out in FF.
     }
     if (status == 200) {
     console.log(request.responseText);
     console.log(request.responseXML);
     console.log(request.status);
       callback(request.responseXML, request.status);
       request.onreadystatechange = function() {};
     }
   }
 }
 request.open('GET', url, true);
 try {
   request.send(null);
 } catch (e) {
   changeStatus(e);
 }
};

The key part of my script is as follows, and the problem is that details is always "null" as can be seen from the readout of the log further below. But I can also see from the console.log(request.responseText) that the data I need is really available.

  downloadUrl("details.txt?place=Playground",function(details) {
  console.log('details: '+details);
  if (details) {
      var lines = details.split("\n");
      }
  }

Below is the console log. The first line is the information I hope to capture in the variable details and is the value of request.responseText because it represents a map pin.

How do I alter my code, or even the existing code that was provided, to hook up my variable details with the result of request.responseText in the existing code?

ahdkZXZ-c2ltcGxpZnljb25uZWN0aW9uc3IlCxIFR3JvdXAiClBsYXlncm91bmQMCxIDUGluGICAgICAgIAKDA  Thu 06 12 2014 1444 29.1109258712   -81.4114379883  Dad 0       0
 util.js:42
null util.js:43
200 util.js:44
details: null ?place=Playground:114

The working example of downloadUrl() is here. (Part of my confusion about understanding how to adapt the working example is that it uses XML, which is not involved in my usage.)

a complete rewrite ends

zerowords
  • 2,915
  • 4
  • 29
  • 44
  • 1
    You need to _pass_ the function, not call it. – SLaks Jun 09 '14 at 21:17
  • Can you say more, please? I don't know the difference. – zerowords Jun 09 '14 at 21:34
  • 1
    `downloadUrl(url,readData())` calls the function read data, uses the value it returns as a function pointer to execute when the data arrives from the server. `downloadUrl(url,readData);` executes the readData function when the data arrives from the server, passing in the appropriate arguments. – geocodezip Jun 09 '14 at 23:55
  • So, I enter just `downloadUrl("details.txt?place={{place}}",readData);` and I get the following error. `ReferenceError: doc is not defined`. So can you imagine another error I am making? Like, is there a way to leave `doc` out of `readData` but still access the incoming data? – zerowords Jun 10 '14 at 00:03
  • If I enter the code `console.log('doc: '+doc);` as the first line of `readData()`, it produces: "doc: null". Does that necessarily mean that `doc` is defined but has received no values, or could it mean that doc was never defined, or what? How do I diagnose this problem, please? – zerowords Jun 10 '14 at 08:36
  • Please see the "update" I put into the question. – zerowords Jun 10 '14 at 16:10
  • I have added a bounty. Please answer this question. – zerowords Jun 12 '14 at 01:19
  • Ok, now please see that I have rewritten the whole question. I really need some help here. What am I doing wrong, and how can I fix it, please? – zerowords Jun 12 '14 at 20:28
  • 1
    What does your XML look like? Can you provide a [fiddle](http://jsfiddle.net/) that demonstrates the issue? – geocodezip Jun 12 '14 at 21:04
  • I don't have any XML, afaik. I said that the example code written by Pamela Fox at Google claims that her demo uses XML. I gave the example's link in the rewrite, but I could not find the `data.xml` file mentioned in the example at that link. **However** with your prompting, while looking at the link I noticed there is another function in that code's downloadUrl() that has been added: `function xmlParse(str)` and I also tried a new experiment: changed `callback(request.responseXML, request.status);` to `callback(request.responseText, request.status);`. New error. I'll alter the question.Thanks – zerowords Jun 12 '14 at 21:34

1 Answers1

0

The short answer to the question is that the callback function inside the utility util.js needed a new argument -- request.responseText -- that was not provided and not mentioned. Unwittingly, I was focusing on the argument for the the call function in my script, not in the utility.

I was also confused by the fact that the second parameter of the calling function was the callback function, but in the callback function itself, it was the first parameter that needed adjustment. Silly me.

zerowords
  • 2,915
  • 4
  • 29
  • 44