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

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 11-05-2012, 11:15 AM   PM User | #1
franrtorres77
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
franrtorres77 is an unknown quantity at this point
closure issue, infowindows in google maps when reload a xml file

Hi there. I am working in this code and I am having some problems when I want to display the updated infowindow when i reload a xml file.

The position of the markers works well, and they move everytime that the xml is loaded with a setinterval(), but the problem is that infowindow just shows up the last element of the array in any marker.

Does anyone knows how to show the right updated infowindow for every marker?

Thank you very much.


PHP Code:
//<![CDATA[
      // this variable will collect the html which will eventually be placed in the side_bar 
      
var side_bar_html ""
    
      
// arrays to hold copies of the markers and html used by the side_bar 
      // because the function closure trick doesnt work there 
      
var gmarkers = []; 
    

     
// global "map" variable
      
var map null;
      var 
markerclusterer null;

// A function to create the marker and set up the event window function 



 
// This function picks up the click and opens the corresponding info window
function myclick(i) {
  
google.maps.event.trigger(gmarkers[i], "click");
  
map.setZoom(map.getZoom()+2); // cuando se selecciona un vehiculo del listado le aplicamos un zoom
  
LoadRoute();
}



function 
initialize() {
  
// create the map
  
var myOptions = {
    
zoom12,
    
disableDefaultUItrue,
    
center: new google.maps.LatLng(37.169619,-3.756981),
    
mapTypeControltrue,
    
mapTypeControlOptions: {stylegoogle.maps.MapTypeControlStyle.DROPDOWN_MENU},
    
navigationControltrue,
    
mapTypeIdgoogle.maps.MapTypeId.ROADMAP
  
}
  
map = new google.maps.Map(document.getElementById("map_canvas"),
                                
myOptions);
}
function 
getMarkers() {
  
google.maps.event.addListener(map'click', function() {
        
infowindow.close();
        });
      
// Read the data from example.xml
      
      
downloadUrl("vehiculos.asp", function(doc) {
        var 
xmlDoc xmlParse(doc);
        var 
markers xmlDoc.documentElement.getElementsByTagName("marker");
        for (var 
0markers.lengthi++) {
          
// obtain the attribues of each marker
          
var lat parseFloat(markers[i].getAttribute("lat"));
          var 
lng parseFloat(markers[i].getAttribute("lng"));
          var 
point = new google.maps.LatLng(lat,lng);
          var 
imei markers[i].getAttribute("imei");
          var 
alias markers[i].getAttribute("alias");
          var 
speedmarkers[i].getAttribute("speed");
          var 
timestampmarkers[i].getAttribute("timestamp");
          var 
estadomarkers[i].getAttribute("estado");
          var 
conectadomarkers[i].getAttribute("conectado");
          var 
altitudemarkers[i].getAttribute("altitude");
          var 
anglemarkers[i].getAttribute("angle");
          var 
soloFechamarkers[i].getAttribute("soloFecha");
          var 
horamarkers[i].getAttribute("hora");
          var 
html="<b>"+alias+" a una velocidad de "+speed+" km/h <br/> ultima posicion  a laaaas: "+hora"<br/> Fecha:"+soloFecha;
          
         
         
          
// create the marker
          
var marker createMarker(point,alias+" "+imei,html,estado,aliasspeedtimestampaltitudeanglesoloFechahora);
        }    
      
markerCluster = new MarkerClusterer(mapgmarkers);
        
// put the assembled side_bar_html contents into the side_bar div
       // document.getElementById("side_bar").innerHTML = side_bar_html;
      
});
    }
 var 
infowindow = new google.maps.InfoWindow(
  { 
 
    
size: new google.maps.Size(150,60)
  });
  

  


  
//a continuación comienza el codigo para actualizar la posicion de los marcadores sin borrarlos
setInterval(function updateMarkers() {
google.maps.event.addListener(map'click', function() {
        
infowindow.close();
        
        });

downloadUrl("vehiculos.asp", function(doc) {
        var 
xmlDoc xmlParse(doc);
        var 
markers xmlDoc.documentElement.getElementsByTagName("marker");
        for (var 
0markers.lengthi++) {
        
    
          
// obtain the attribues of each marker
          
var lat parseFloat(markers[i].getAttribute("lat"));
          var 
lng parseFloat(markers[i].getAttribute("lng"));
          var 
point = new google.maps.LatLng(lat,lng);
          var 
imei markers[i].getAttribute("imei");
          var 
alias markers[i].getAttribute("alias");
          var 
speedmarkers[i].getAttribute("speed");
          var 
timestampmarkers[i].getAttribute("timestamp");
          var 
estadomarkers[i].getAttribute("estado");
          var 
conectadomarkers[i].getAttribute("conectado");
          var 
altitudemarkers[i].getAttribute("altitude");
          var 
anglemarkers[i].getAttribute("angle");
          var 
soloFechamarkers[i].getAttribute("soloFecha");
          var 
horamarkers[i].getAttribute("hora");
          
          
          var 
html="<b>"+alias+" a una velocidad de "+speed+" km/h <br/> ultima posicion  a laaaas: "+hora"<br/> Fecha:"+soloFecha;
         
         
// create the marker
          
var newLatLng gmarkers[i].setPosition(point,alias+" "+imei,html,estado,alias,speed,timestamp,altitude,anglesoloFechahora);
        
        
////tryouts for infowindow
                
infowindow.setContent("<b>"+alias+" a una Velocidad de "+speed+" km/h <br/> ultima posicion  a las: "+hora"<br/> Fecha:"+soloFecha); 
       
        
    
        
           
infowindow.open(map,newLatLng);
          


////ends tryouts for infowindow

        
}
        
       
markerCluster = new MarkerClusterer(mapgmarkers);     
      }
//end loop
      
);//end download
      
 

}//end function 
 
5000);//end code for refreshg


function createMarker(latlngimeihtmlestadoaliasspeedtimestampaltitudeanglesoloFechahora) {
if(
estado == 1)
  
image '/artworks/icons/truck_green3.png';
else
  
image '/artworks/icons/truck_red.png';

    var 
textoLabel"<b>"+alias+" a una velocidad de "+speed+" km/h <br/> Ultima posición  a las: "+hora"<br/> Fecha:"+soloFecha+"<br/> first html ";
    var 
contentString html;
    var 
newLatLng = new MarkerWithLabel({
        
positionlatlng,
        
iconimage,
         
// map: map,
        
draggabletrue,
        
        
flattrue,
        
labelContenttextoLabel,
       
labelAnchor: new google.maps.Point(400),
       
labelClass"labels"// the CSS class for the label
       
labelStyle: {opacity0.50},
        
zIndexMath.round(latlng.lat()*-100000)<<5
        
});

        
    
google.maps.event.addListener(newLatLng'click', function() {
        
infowindow.setContent(textoLabel); 
        
infowindow.open(map,newLatLng);
         
map.setZoom(map.getZoom()+2); // cuando se selecciona un vehiculo del listado le aplicamos un zoom
        
});
    
// save the info we need to use later for the side_bar
    
gmarkers.push(newLatLng);
    
// add a line to the side_bar html
   // side_bar_html += '<a href="javascript:myclick(' + (gmarkers.length-1) + ')">' + imei +'<\/a><br>';


Last edited by VIPStephan; 11-05-2012 at 12:22 PM.. Reason: added code BB tags
franrtorres77 is offline   Reply With Quote
Old 11-05-2012, 12:23 PM   PM User | #2
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,615
Thanks: 5
Thanked 865 Times in 842 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
If you post any code please put it in between [CODE][/CODE] tags. It makes scanning your posts much easier. You can do this by clicking the small ‘#’ icon above the reply field.
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Reply

Bookmarks

Tags
closure issue, google maps v3, infowindows, xml

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 01:43 AM.


Advertisement
Log in to turn off these ads.