如何使用Open Layer 3在两个cliked点之间画线?

时间:2017-01-19 03:34:23

标签: openlayers-3

我可以在地图中显示 json lat lon ,但我想在两个选定点之间绘制线条。 像这样Here

在这里我可以点击Map中的所有地方,但我想要启用仅点击仅显示的点。

我使用此链接显示我的点Link2现在我想在两点之间画线

<script>  
    var flickrSource = new ol.source.Vector();
    function flickrStyle(feature) {
          var style = new ol.style.Style({
              image: new ol.style.Circle({
                      radius: 10,
                      stroke: new ol.style.Stroke({
                        color: 'white',
                        width: 2
                      }),

                    fill: new ol.style.Fill({
                        color: 'green'
                      }),
               }),
              text: new ol.style.Text({
                text: feature.getGeometryName(),
                fill: new ol.style.Fill({color: 'blue'}),
                stroke: new ol.style.Stroke({color: 'white', width: 1}),
                offsetX: 0,
                offsetY: 15
              }),       
          });

          return [style];
    }   
        var flickrLayer = new ol.layer.Vector({
          source: flickrSource,
          style: flickrStyle
        });

        var layer = new ol.layer.Tile({
          source: new ol.source.OSM()
        });

        var center = ol.proj.transform([-1.812, 52.443], 'EPSG:4326', 'EPSG:3857');

        var view = new ol.View({
          center: center,

          zoom: 3
        });

        var source = new ol.source.Vector({wrapX: false});
        var map = new ol.Map({

          target: 'map',
          layers: [layer, flickrLayer],
          view: view
        });

    function successHandler(data) {

      var transform = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
      data.items.forEach(function(item) {
        var feature = new ol.Feature(item);
        feature.setGeometryName(item.name);   
        var coordinate = transform([parseFloat(item.longitude), parseFloat(item.latitude)]);
        var geometry = new ol.geom.Point(coordinate);
        feature.setGeometry(geometry);
        flickrSource.addFeature(feature);

      });
    }
</script>

2 个答案:

答案 0 :(得分:0)

获取这两点的坐标并绘制LineString

var thing = new ol.geom.LineString(points);
var featurething = new ol.Feature({
  name: "Thing",
  geometry: thing
});
flickrSource.addFeature(featurething);

&#13;
&#13;
var flickrSource = new ol.source.Vector();

var data = {
  "items": [{
    name: 'geo1',
    longitude: "0.0",
    latitude: "0.0"
  }, {
    name: 'geo1',
    longitude: "5.0",
    latitude: "5.0"
  }]
};

function flickrStyle(feature) {
  var style = new ol.style.Style({
    image: new ol.style.Circle({
      radius: 10,
      stroke: new ol.style.Stroke({
        color: 'white',
        width: 2
      }),

      fill: new ol.style.Fill({
        color: 'green'
      }),
    }),
    text: new ol.style.Text({
      text: feature.getGeometryName(),
      fill: new ol.style.Fill({
        color: 'blue'
      }),
      stroke: new ol.style.Stroke({
        color: 'white',
        width: 1
      }),
      offsetX: 0,
      offsetY: 15
    }),
  });

  return [style];
}
var flickrLayer = new ol.layer.Vector({
  source: flickrSource
  //style: flickrStyle
});

var layer = new ol.layer.Tile({
  source: new ol.source.OSM()
});

var center = ol.proj.transform([0.0, 0.0], 'EPSG:4326', 'EPSG:3857');

var view = new ol.View({
  center: center,

  zoom: 5
});

var source = new ol.source.Vector({
  wrapX: false
});
var map = new ol.Map({

  target: 'map',
  layers: [layer, flickrLayer],
  view: view
});

function successHandler(data) {
  var points = [];
  data.items.forEach(function(item) {
    var point = ol.proj.transform([parseFloat(item.longitude), parseFloat(item.latitude)], 'EPSG:4326', 'EPSG:3857');
    points.push(point);
    var geometry = new ol.geom.Point(point);
    var feature = new ol.Feature({
      name: item.name,
      geometry: geometry
    });
    flickrSource.addFeature(feature);

    var thing = new ol.geom.LineString(points);
    var featurething = new ol.Feature({
      name: "Thing",
      geometry: thing
    });
    flickrSource.addFeature(featurething);
  });
}
successHandler(data);
&#13;
<link href="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.20.1/ol.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.20.1/ol.js"></script>
<div id="map"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这是代码......

以下是矢量来源,图层和地图

// Vector source of data points
var flickrSource = new ol.source.Vector();

// Style function for the data points
function flickrStyle(feature) {
    var style = new ol.style.Style({
        image: new ol.style.Circle({
            radius: 10,
                stroke: new ol.style.Stroke({
                color: 'white',
                width: 2
            }),
            fill: new ol.style.Fill({
                color: 'green'
            })
        }),
        text: new ol.style.Text({
           text: feature.getGeometryName(),
           fill: new ol.style.Fill({color: 'blue'}),
           stroke: new ol.style.Stroke({color: 'white', width: 1}),
           offsetX: 0,
           offsetY: 15
        })
    });

    return [style];
}

// Layers
var osmLayer = new ol.layer.Tile({ source: new ol.source.OSM() });
var flickrLayer = new ol.layer.Vector({
     source: flickrSource,
     style: flickrStyle
});  

// MAP
var map = new ol.Map({
     target: 'map',
     layers: [osmLayer, flickrLayer],
     view: new ol.View({
          center: ol.proj.transform([0, 20], 'EPSG:4326', 'EPSG:3857'),
          zoom: 3
     })
});

然后是数据点将它们放在地图上

// Data points
var data = {
     "items": [{
          name: 'p1',
          longitude: "0.0",
          latitude: "0.0"
     }, {
          name: 'p2',
          longitude: "50.0",
          latitude: "50.0"
     }, {
          name: 'p3',
          longitude: "50.0",
          latitude: "-50.0"
     }]
};

// Placing data points on the map
function placePoints(data) {
     var transform = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');

     data.items.forEach( function(item) {
          // for each item of data points we create feature geometry
          // with coords contained in data and add them to the source

          var feature = new ol.Feature(item);
          feature.setGeometryName(item.name);   

          var coordinate = transform(
               [parseFloat(item.longitude), parseFloat(item.latitude)]
          );

          var geometry = new ol.geom.Point(coordinate);
          feature.setGeometry(geometry);

          flickrSource.addFeature(feature);
    } );
}
placePoints(data); // do the stuff of placing points 

然后用交互绘制点之间的线:

// select interaction working on "click"
var mySelectInteraction = new ol.interaction.Select({
     condition: ol.events.condition.click,
     multi: false
});

// init coords of line to draw between points
var pointA = null;
var pointB = null;

// Interaction on points for drawing lines between
mySelectInteraction.on('select', function(e) {

    if (e.selected.length === 0) {
        // clicking nothing, so reset points coords
        pointA = null;
        pointB = null;
    }
    else {
        // Feature clicked and its coords
        var feature = e.target.getFeatures().item(0);
        var coords = feature.getGeometry().getCoordinates();

        // Definition of coords points
        if (pointA === null) { pointA = coords; }
        else { pointB = coords; }


        if ( pointA !== null && pointB !== null) {

            var LinesSource = new ol.source.Vector();
            var LinesLayer = new ol.layer.Vector({ source : LinesSource });
            map.addLayer( LinesLayer );

            // Line construction
            LinesSource.addFeature( new ol.Feature({
                geometry : new ol.geom.LineString( [pointA, pointB] )
            }) );

            // Reset points for next drawing
            pointA = null;
            pointB = null;
        }
    }
});

map.addInteraction(mySelectInteraction);

非常适合我!