[英]openlayers compile multi point to linestring
我有一個通過GEOJSON加載的點列表,例如:
{
"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)));
而且我沒有任何錯誤,但是,線串不起作用。
將單個點復制到數組中,並使用該數組創建新的LineString:
凌晨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"
還有另一種方法,使用專用層:
var line = new OpenLayers.Layer.PointTrack ('Line');
line.addNodes (vectors.features);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.