0

I get data from Google Maps in XML format. All functions are working, but I have little problem. It only shows me the last data. Why is that, and how can I solve it?

This is my code (jsFiddle)

function getline() {


downloadUrl("line.php", function(doc) {



        var g = google.maps;

        var xmlDoc = xmlParse(doc);

        bounds = new google.maps.LatLngBounds();

      // ========= Now process the polylines ===========
      var lines = xmlDoc.documentElement.getElementsByTagName("line");

      // read each line
      for (var a = 0; a < lines.length; a++) {
        // get any line attributes
        var colour = lines[a].getAttribute("colour");
        var width  = parseFloat(lines[a].getAttribute("width"));
        var diameter = lines[a].getAttribute("diameter");
        var project = lines[a].getAttribute("projectid");
        var contract = lines[a].getAttribute("contract");
        var comp = lines[a].getAttribute("complated");
        var id = lines[a].getAttribute("id_line");
        // read each point on that line
        var points = lines[a].getElementsByTagName("point");
        var pts = [];
        var length = 0;
        var point = null;

        for (var i = 0; i < points.length; i++) {
           pts[i] = new g.LatLng(parseFloat(points[i].getAttribute("lng")),
                                parseFloat(points[i].getAttribute("lat")));
           if (i > 0) {
             length += pts[i-1].distanceFrom(pts[i]);
             if (isNaN(length)) { alert("["+i+"] length="+length+" segment="+pts[i-1].distanceFrom(pts[i])) };
           }
           bounds.extend(pts[i]);
           point = pts[parseInt(i/2)];

                var info = "<b> Contract: </b>" + contract + " <br/> <b>Project: </b>" + project +"<br/>  <b>Diameter: </b>" + diameter + " <br/> <b>Complated: </b>" + comp + "  <hr><br/><a class=\"inline-link-1\" href=\"#\">Change Data</a> <a class=\"inline-link-1\" href=\"#\">Edit</a> >" + id +"" ;

        }
        // length *= 0.000621371192; // miles/meter 

 if (comp < 1) { 
 colorr = '#FA0505' }

 if (comp > 0 && comp < 25 ) { 
 colorr = '#FFA640' }

 if (comp > 24 && comp < 50) { 
 colorr = '#FFFD91' }

 if (comp > 49 && comp < 75) { 
 colorr = '#E8E400' }

 if (comp > 74 && comp < 100) { 
 colorr = '#BFFFAD' }

 if (comp == 100) { 
 colorr = '#0F8500' }


        var polyline = new g.Polyline({
                          map:map,
                          path:pts,
                          strokeColor:colorr,
                          strokeWeight:width,
                          clickable: true
                          });
       //createClickablePolyline(polyloine, html, label, point, length);
     //  map.addOverlay(polyline);


  google.maps.event.addListener(polyline,'mouseover', function() {


        this.setOptions({strokeColor: '#690469' });
           this.setOptions({strokeOpacity: 1.0 });
           this.setOptions({strokeWeight: 4 });
  });

     google.maps.event.addListener(polyline,'mouseout', function() {


        this.setOptions({strokeColor: colorr });
           this.setOptions({strokeOpacity: 1.0 });
           this.setOptions({strokeWeight: 4 });
  });


  var  mpenc = new google.maps.InfoWindow();
  google.maps.event.addListener(polyline,'click', function(event) {
    mpenc.setContent(info, this);
    mpenc.setPosition(event.latLng, this);
    mpenc.open(map);
  });

      }
      map.fitBounds(bounds);




    });



}
Maehler
  • 6,111
  • 1
  • 41
  • 46
brio
  • 175
  • 2
  • 5
  • 16
  • You mean all elements generated by the loop have the value of the points.length element? you are probably not closing over the loop variable correctly, have a look at this [so about it](http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example) – kieran Mar 29 '13 at 05:24
  • @brio Please! make sure you should always post code here,don't just drop the link only – swiftBoy Mar 29 '13 at 05:51
  • thanks for answer.. Can your explain where is my error? i cant find it yet.. I can show your my xml if need. – brio Mar 29 '13 at 05:52
  • possible duplicate of [Google Maps API V3 infoWindow - All infoWindows displaying same content](http://stackoverflow.com/questions/4897316/google-maps-api-v3-infowindow-all-infowindows-displaying-same-content) – Dr.Molle Mar 29 '13 at 06:49
  • @Dr.Molle I know i read all it.. But cant understand. – brio Mar 29 '13 at 07:43
  • You should leave your code (in addition to the link to jsfiddle...). Can you provide a link to the XML for the data you are having trouble parsing (or a snippet of XML that exhibits the problem)? – geocodezip Mar 30 '13 at 01:35
  • yes of course. i think its not a problem with xml.. http://nn-gis.com/map/line.php – brio Mar 30 '13 at 20:21

1 Answers1

0

It looks like you copied some code and changed it. Why is this commented out?

//createClickablePolyline(polyline, html, label, point, length);

That function is what gets closure on the "data", polyline and infowindow, so they are associated with each other (at least in my versions of this code).

working v3 version of my code

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • yes. its your code. i edited it.. but now i cant correct it to show data..it show me only last data – brio Mar 29 '13 at 13:20
  • Compare your version to the working version. Add the createClickablePolyline function back in. – geocodezip Mar 29 '13 at 15:06
  • i have markers and polylines. Markers working perfect. I call line when i need it with function getline(). when i add this function (creatpoly...) line not working without this func lines working but show only last data – brio Mar 29 '13 at 19:50