-2

I'm sorry to ask this but I've looked everywhere, and cant follow this.
I'm trying to re-work a v2 google maps example to make it go in v3. I have a set of 'end points' within a driving distance of a central point I want to draw a polygon around. Tese are written to an array (there are three in the code that should work: driveMarkersArray - the current one, or drivePolyPoints or markersArray) Within the function process1direction(from,to) that does this they are certainly there, (although interestingly the firebug console reports them at the end, after all the processing is done?) BUT when then processed in a different function draw_DrivePolygon() to draw the polygon, array appears empty. Is this just declarations (I've checked a a lot) or something to do with the asynchronous query to find the driving directions?

  • 3
    could you please reduce the code to the relevant bit – Asad Saeeduddin Jan 01 '13 at 18:48
  • 2
    You just copy pasted your whole program there. You will get better answers if you produce a *minimal* example. In fact, you might even solve it yourself by doing that. – hugomg Jan 01 '13 at 18:49
  • 4
    I went thoroughly through your code, and in line 324, there is... no just kidding, I didn't go through your code. `:P` – Šime Vidas Jan 01 '13 at 18:51
  • @Charlie neither is a full listing of a program. –  Jan 01 '13 at 18:57
  • @tonygoodwin: What others mean is, you need to actually produce an example which demonstrates the problem using minimum amount of code for the purpose. EDIT: And also, it stands to reason that you will better understand the problem by doing this, and perhaps even solve it on your own. –  Jan 01 '13 at 19:37
  • @bvukelic, and everyone else, thanks for the feedback. I have been reducing the code myself, but you've rightly pushed me to reduce to the core question. I've done that, and posted the full remaining code in place as I still don't know whether this is to do with declaring variables or another root. I assure you I have looked at the other answer, and have checked that the arrays in question are declared globally. thanks for your encouragement. – tony goodwin Jan 01 '13 at 20:15
  • Ok, so there are so many problems with the code it's scary to even begin discussing them. First of all, so many global variables it's not even funny. So many stateful functions it's even less funny. You should try to make functions more functional: depend more on their input value, and return usable values, instead of depending on setting global variables. That should make debugging much much more easier. –  Jan 01 '13 at 20:24
  • One obvious problem is that call to `process1direction()` will always return undefined because it is an asynchronous function, and yet you set a variable to its return value (which is, again, always undefined). –  Jan 01 '13 at 20:26
  • I appreciate your time. endPoint = process1direction(center,point); I was trying to get a result from that before and failed, and should have removed it when tidying up so should read: process1direction(center,point);. My essential problem is that even though there are values in each of var markersArray, drivePolyPoints, driveMarkersArray while in the function they are not available in the draw_DrivePolygon(). How do I pass them please? – tony goodwin Jan 01 '13 at 20:36
  • Answer found. Not to do with variable declaration, but was due to asynchronous call. Answer#2 in [link](http://stackoverflow.com/questions/12467690/why-is-this-variable-undefined-or-not-returned) was the clearest explanation of what to do. I did find the 'tough love' above and the confirmation that asynchronous was relevant very helpful from @bvukelic . Would post the solution but its been closed. – tony goodwin Jan 02 '13 at 19:25
  • @tonygoodwin Yeah, some folks on this site are simply too trigger-happy. :) Glad you solved it, though. –  Jan 02 '13 at 22:04

1 Answers1

2

In JavaScript Variable has its scope function level. Not block level.

Have a look here for variable scope in JavaScript

javascript-variable-scope

Variable scope and the var keyword.

Community
  • 1
  • 1
Raghvendra Parashar
  • 3,883
  • 1
  • 23
  • 36
  • Thank you, could you help me by indicating how to change the variable to block level? I've looked at the examples, and think I've declared them globally. – tony goodwin Jan 01 '13 at 20:18