Search code examples
c#javascriptgoogle-mapsmapsgpolygon

Draw Polygon in Googlemap based marker time


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);

Solution

  • 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();
    
    });