简体   繁体   English

openlayers编译多点到线串

[英]openlayers compile multi point to linestring

I have a list of points load via GEOJSON like: 我有一个通过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
        ]
      }
    }
  ]
}

How can i merge these point become a linestring. 我如何合并这些点成为线串。 i would like to make a link for each of couple point I have my code: 我想为一对夫妇的每一个链接我有我的代码:

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

and i don't have any error but, linestring does not work. 而且我没有任何错误,但是,线串不起作用。

Copy the individual points into an array and use that to make a new LineString: 将单个点复制到数组中,并使用该数组创建新的LineString:

rough 3am code: 凌晨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));

You can check if each feature is a point: 您可以检查每个特征是否都是点:

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

There is another way, using specialized layer: 还有另一种方法,使用专用层:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM