openlayers将多点编译为线串

时间:2014-04-16 07:44:07

标签: javascript geometry openlayers geojson

我有一个通过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)));

我没有任何错误但是,线串不起作用。

2 个答案:

答案 0 :(得分:0)

将各个点复制到一个数组中并使用它来创建一个新的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"

答案 1 :(得分:0)

还有另一种方法,使用专门的层:

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