2014-04-16 24 views
0

我通過GeoJSON的具有點負載的列表,如:的OpenLayers編譯多點到線串

{ 
    "type": "FeatureCollection", 
    "features": [ 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 5:15PM", 
     "action": "none", 
     "location": "Thien Phuoc" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.65454387664795, 
      10.777103545816248 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 5:20PM", 
     "action": "none", 
     "location": "Ly Thuong Kiet" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.65585279464722, 
      10.778505309541208 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 5:25PM", 
     "action": "none", 
     "location": "Ly Thuong Kiet" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.65600299835204, 
      10.777957252320723 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 5:30PM", 
     "action": "none", 
     "location": "Bac Hai" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.65636777877806, 
      10.777957252320723 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 5:35PM", 
     "action": "none", 
     "location": "Bac Hai" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.65884613990782, 
      10.780697528442683 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 5:40PM", 
     "action": "none", 
     "location": "Thanh Thai" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.66354537010193, 
      10.77642901056951 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 5:45PM", 
     "action": "none", 
     "location": "To Hien Thanh" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.66723608970642, 
      10.779285235403528 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 5:50PM", 
     "action": "none", 
     "location": "CMT8" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.67211771011351, 
      10.782910810169879 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 5:55PM", 
     "action": "none", 
     "location": "CMT8" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.67622685432433, 
      10.780686988966933 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 6:7PM", 
     "action": "none", 
     "location": "Cong Truong Dan Chu" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.68163418769836, 
      10.777535669164344 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 6:15PM", 
     "action": "none", 
     "location": "CMT8" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.68194532394409, 
      10.77761998584285 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 6:20PM", 
     "location": "CMT8", 
     "action": "none" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.68367266654968, 
      10.776660882231052 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 6:25PM", 
     "location": "Dien Bien Phu", 
     "action": "none" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.68651580810547, 
      10.779348472547028 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 6:30PM", 
     "location": "Dien Bien Phu", 
     "action": "none" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.6892945766449, 
      10.781972802282924 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 6:35PM", 
     "location": "Dien Bien Phu", 
     "action": "none" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.69196605682373, 
      10.7846076484671 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 6:40PM", 
     "location": "Dien Bien Phu", 
     "action": "none" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.69505596160889, 
      10.787874825681238 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "time": "2014/04/08 6:45PM", 
     "location": "Vong Xoay Dien Bien Phu", 
     "action": "none" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.69925093650816, 
      10.79221695465009 
     ] 
     } 
    }, 
    { 
     "type": "Feature", 
     "properties": { 
     "fromtime": "2014/04/08 7:10PM", 
     "location": "Mc' Donal", 
     "action": "stop" 
     }, 
     "geometry": { 
     "type": "Point", 
     "coordinates": [ 
      106.69911146163939, 
      10.792859837346588 
     ] 
     } 
    } 
    ] 
} 

如何合併這些點變成線串。我想作一個鏈接,每對夫婦點 我有我的代碼:

var points = new OpenLayers.Format.GeoJSON({ 
       'internalProjection': map.baseLayer.projection, 
       'externalProjection': new OpenLayers.Projection("EPSG:4326") 
      }).read(data); 
     vectors.addFeatures(points); 
     vectors.addFeatures(new OpenLayers.Feature.Vector(new OpenLayers.Geometry.LineString(points))); 

和我沒有任何錯誤,但是,線串不起作用。

回答

0

複製的各個點到一個數組中,並用它來創建一個新線段形式:

粗糙凌晨3點代碼:

var feats = new OpenLayers.Format.GeoJSON({ 
      'internalProjection': map.baseLayer.projection, 
      'externalProjection': new OpenLayers.Projection("EPSG:4326") 
     }).read(data); 

var linePoints = []; 

for(var i=0; i<feats.length; ++i) { 
    linePoints.push(feats[i].geometry); 
} 
var newGeo = new OpenLayers.Geometry.LineString(linePoints); 
vectors.addFeatures(new OpenLayers.Feature.Vector(newGeo)); 

可以檢查每個特徵點:

feats[i].geometry.CLASS_NAME == "OpenLayers.Geometry.Point" 
0

還有另一種方法,使用專門的圖層:

var line = new OpenLayers.Layer.PointTrack ('Line'); 
line.addNodes (vectors.features);