0

I want to draw a polygon by connecting markers in googlemap.Time is associated with each marker.So i want connect each points based on the time.How can i do this.Currently code is impleneted like this.Where i need to change.

       var marker = new Array();
       var points = new Array();
     for(var i=0;i<value.length;i++)
          {
               var tempar=value[i].split(',');
               var center = new GLatLng(tempar[0], tempar[1]);
               var mar = new GMarker(center, icon);
               var imgpth=tempar[3]; 
               var tme=tempar[2];
               marker.push(mar);   
               marker[i].time = tempar[2];
               points.push(marker[i].getLatLng());
               drawMarker(mar,imgpth,tme);                   
        }
          for(i=;i<marker.length;i++)
         {
            map.addOverlay(marker[i]);
          }
           var polyline = new GPolygon(points, "#f33f00", 2, 1, "#ff0000", 0.2);
           map.addOverlay(polyline);
user922834
  • 195
  • 2
  • 6
  • 18

1 Answers1

1

The easiest way to do this is to sort marker array by time. Assuming that the time property has some sensible values (not strings):

marker.sort(function(a,b){return a.time-b.time});

If the time is a string then:

marker.sort(function(a,b) {
 var date1 = new Date(a.time);
 var date2 = new Date(b.time);
 return date1.getTime() - date2.getTime();

});
WojtekT
  • 4,735
  • 25
  • 37
  • time property is just a string.how can i able to sort this time string – user922834 May 24 '12 at 13:21
  • after sorting again we need to push it in "Points" array...is it correct? – user922834 May 24 '12 at 13:22
  • one more thing date time string doesnot contain Date value.String will be like this..."19:23:58" – user922834 May 24 '12 at 13:42
  • I can't fix all your problems. I gave you an example, you can figure it out. – WojtekT May 24 '12 at 13:44
  • function SortByName(a, b){ return timeToSeconds(a.time) - timeToSeconds(b.time); } function timeToSeconds(time) { time = time.split(/:/); return time[0] * 3600 + time[1] * 60 + time[2]; } //sorting will be like this.. – user922834 May 25 '12 at 05:51