Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-13-2013, 03:32 PM   PM User | #1
michael_cassio
New to the CF scene

 
Join Date: Feb 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
michael_cassio is an unknown quantity at this point
infowindow GMaps no iteration over text

Hello, I wrote a GMaps JS Script which allows one to toggle different markers I fetch the data via php from a mySQL-Database:

In order to open so-called infowindows I use G-Maps Event-Listeners. In the following script everything works just fine: the toggle function works. It' possible to open an info-windows on each marker.
Howerver, I always get the the Infowindow-text of the last marker. So, there is no iteration over the text. I know that I always overwrite the content of the infowindow (until I have only the last item), so I dont' know how to fix it...
I would be very grateful for just a hint. Thank you!!

Code:
 var deutschland = new google.maps.LatLng(51,9);
  

     function initialize(){

      var mapOptions = { zoom: 6,
                         center: deutschland,
                         streetViewControl: true,
                         scaleControl: true,
                         mapTypeControl: true,
                         mapTypeId: google.maps.MapTypeId.ROADMAP };


   var map = new google.maps.Map(document.getElementById("map-container"),mapOptions);
  

   lat_data = [

['mid',50.0010400,9.0730700,'BluVisio','marker.png','infowindow text 1'],
['mid',50.2010057,8.2592738,'Alco GmbH','marker.png','infowindow text 2'],
['West',50.7430120,7.1264100,'Computer Wunsch Systems OHG','marker_green.png','infowindow 3 text'] ....

];



   markers = [];
   var i, newMarker;

   for (i = 0; i < lat_data.length; i++) {

       var shadow = new google.maps.MarkerImage('http://maps.google.com/mapfiles/shadow50.png',
       new google.maps.Size(37,32),
       new google.maps.Point(0,0),
       new google.maps.Point(0,32));

    var image = new google.maps.MarkerImage('http://maps.google.com/mapfiles/'+lat_data[i][4],
       new google.maps.Size(20,32),
       new google.maps.Point(0,0),
       new google.maps.Point(0,32));

     newMarker = new google.maps.Marker({
       position: new google.maps.LatLng(lat_data[i][1], lat_data[i][2]),
       map: map,
       title: lat_data[i][3],
       icon: image,
       shadow: shadow
     });

     newMarker.category = lat_data[i][0];
     newMarker.setVisible(true);

     var infowindow = new google.maps.InfoWindow({
           content: lat_data[i][5]
       });

       google.maps.event.addListener(newMarker,'click',function(){
         infowindow.open(map,this);

      });
      google.maps.event.addListener(map,'click',function(){
          infowindow.close(map,this);
      });

     markers.push(newMarker);
   }

   }

    function displayMarkers(category) {
      var i;

     for (i = 0; i < markers.length; i++) {
       if (markers[i].category == category) {
         if (markers[i].getVisible()) {
              markers[i].setVisible(false); }

        else { markers[i].setVisible(true); } } }

   }
michael_cassio is offline   Reply With Quote
Old 02-13-2013, 06:53 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
if you just want a hint, you should google "function closure" - there's plenty of stuff out there about getting closure for event listeners when using loops - it's not a gmaps problem, just a general javascript problem
xelawho is offline   Reply With Quote
Old 02-13-2013, 09:43 PM   PM User | #3
michael_cassio
New to the CF scene

 
Join Date: Feb 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
michael_cassio is an unknown quantity at this point
I have been searching for a similar problem for the whole day.
As I said, I think, that I overwrite infowindow.content over and over again.
Is this true or false?

edit: o.k., you are right, I just start to read the definition about closures ones again!!

Last edited by michael_cassio; 02-13-2013 at 09:55 PM..
michael_cassio is offline   Reply With Quote
Old 02-15-2013, 09:13 PM   PM User | #4
michael_cassio
New to the CF scene

 
Join Date: Feb 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
michael_cassio is an unknown quantity at this point
Yo, I got it!!

Code:
....
 iw = new google.maps.InfoWindow({
           content: lat_data[i][5]
       });

       function make_open_event_callback(iw){
           return function(){
              iw.open(map,this);
           };
       }
        function make_close_event_callback(iw){
           return function(){
              iw.close(map,this);
           };
       }

       google.maps.event.addListener(newMarker,'click',make_open_event_callback(iw));
      google.maps.event.addListener(map,'click',make_close_event_callback(iw));

     markers.push(newMarker);

....
michael_cassio is offline   Reply With Quote
Reply

Bookmarks

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:37 AM.


Advertisement
Log in to turn off these ads.