Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 3.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-30-2010, 02:50 AM   PM User | #16
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,542
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Do you not have *SOME* debugger???

Just running your page on Firefox using the Firebug debugger immediately pinpoints the problem:
Code:
 infowindow.open( map, which );
"map is not defined".

And indeed it isn't.

You do
Code:
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
in your initialize() function so the variable map is *NOT* available outside that function (except when you pass it as an argument to the setMarkers() function).

So...

Simply move the *declaration* of the variable to global scope:
Code:
var map = null; 
function initialize() {
  var myOptions = {
    zoom: 10,
    center: new google.maps.LatLng(32.75542,-117.13433),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  // notice that the var keyword is removed!
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
 
  setMarkers(map, MarkersArray);
}
Will this be the last problem? I dunno. But if you would use Firebug you'd be better equipped to find out for yourself. Please?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
flashdave (06-30-2010)
Old 06-30-2010, 08:41 AM   PM User | #17
flashdave
New to the CF scene

 
Join Date: Jun 2010
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
flashdave is an unknown quantity at this point
I got it!!

thank you soo much!

I had to make a few edits, but it is working now.

small new problem, but I guess I will deal with it.

thanks again!
flashdave is offline   Reply With Quote
Old 09-13-2011, 07:21 AM   PM User | #18
syslogic
New to the CF scene

 
Join Date: Sep 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
syslogic is an unknown quantity at this point
I once posted a detailed explanation to my blog:
Google Maps API v3 Multiple InfoWindows

This should give you a pretty good clue how it works.
syslogic is offline   Reply With Quote
Old 05-12-2012, 03:18 AM   PM User | #19
owltech
New to the CF scene

 
Join Date: May 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
owltech is an unknown quantity at this point
Try this

I have had and resolved your exact problem. The issue is that in Javascript for loops, the real variables are not used, but rather only copies of them. So, if you want to change the value of an external or passed variable, you have to make a function call OUTSIDE the loop to do the job. Here's my code:

Code:
function setMarkers() {
  // Add markers to the map
  for (var i = 0; i < _offices.length; i++) {
    var office = _offices[i];
    var myLatLng = new google.maps.LatLng(office[1], office[2]);
    var contentString = 
        '<div class="infoWindow">'+
        '<h3 class="heading">'+office[0]+'</h3>'+
        '<p>'+office[4]+'</p>'+
        '</div>';
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: _map,
        title: office[0],
        zIndex: office[3],
        clickable: true
    });
    addInfoWindow(marker, contentString);
    _markers.push(marker);
  }
}
function addInfoWindow(marker, message) {
  var info = message;
  var infoWindow = new google.maps.InfoWindow({
      content: message,
      size: new google.maps.Size(200,50)
  });
  _infoWindows.push(infoWindow);
  google.maps.event.addListener(marker, 'click', function () {
      closeAllMarkers();
      infoWindow.open(_map, this);
  });
}
Note the call to addInfoWindow inside the for loop. Believe it or not, putting the code in the called function INSIDE the for loop will not work. Only the last item referenced, according to the length of the variable "offices" will be left to use. Putting the code for addInfoWindow OUTSIDE the for loop will produce the results you want. I'm definitely not an expert on this, but I have battered my brains out over it and appear to have won.

Last edited by owltech; 05-12-2012 at 03:22 AM..
owltech is offline   Reply With Quote
Old 05-12-2012, 03:51 AM   PM User | #20
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,542
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You have described the phenomenon correctly but you don't understand the cause.

Look into JavaScript closures for the reason your solution worked.

Quote:
Only the last item referenced, according to the length of the variable "offices" will be left to use.
And even it wouldn't be available if you didn't have a global variable that held a reference to it.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 05-12-2012 at 05:42 AM..
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
api, geocode, google, infowindow, maps

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:40 AM.


Advertisement
Log in to turn off these ads.