0

I successfully added point like this:

var iconFeature = new ol.Feature({
  geometry: new ol.geom.Point([8637791.36, 1456487.82]),
  name: 'Police Station',
  population: 4000,
  rainfall: 500
});

var vectorSource = new ol.source.Vector({
  features: [iconFeature]
});

var vectorLayer = new ol.layer.Vector({
  source: vectorSource,
  title: 'point_layer'
});

But failing to add line feature like this and assigning various properties like that I assigned to point feature, Polygon feature was added by below code:

var ring = [[77.4700927734375, 12.906190219892437],[77.4707794189453, 12.987162237749473],
  [77.5188446044922, 12.989838549012532],[77.51060485839844, 12.961066692801282],
  [77.51747131347656, 12.953705916047227],[77.52708435058594, 12.94500653581774],
  [77.51815795898438, 12.936306851970144],[77.50030517578125, 12.92426063498786],
  [77.486572265625, 12.912883118595701],[77.4700927734375, 12.906190219892437]];

var polygon = new ol.geom.Polygon([ring]);
polygon.transform('EPSG:4326', 'EPSG:3857');

var polygonfeature = new ol.Feature(polygon);
var property = { "name": "Boundary"};
polygonfeature.setProperties(property);

var vectorSource1 = new ol.source.Vector();
vectorSource1.addFeature(polygonfeature);

var vectorLayer1 = new ol.layer.Vector({
  source: vectorSource1,
  title: 'polygon_layer'
});

Please suggest to add line feature with some properties so that those can be displayed on mouse click.

map.addLayer(vectorLayer1); 
  • Linestring is similar to polygon but one fewer pairs of brackets wiki.geojson.org/GeoJSON_draft_version_6#LineString – Mike Oct 8 '18 at 20:07
  • Why are you trying to make a polygon if you want to make a line? Have you tried new ol.geom.LineString instead? – Jelle Oct 8 '18 at 20:13
  • Actually, I want all i.e point , line and polygon with layer switcher button to switch among them. – Vishal Oct 9 '18 at 4:48
  • Ok, what code have you tried to add the line? – Jelle Oct 9 '18 at 8:06
  • var coordinates = [[8623931.28, 1449016.75],[8637791.36, 1456487.82]]; var layerLines = new ol.layer.Vector({ source: new ol.source.Vector({ features: [new ol.Feature({ geometry: new ol.geom.LineString(coordinates), name: 'Line', })] }), }); map.addLayer(layerLines); THANKS FOR CONCERN SIR, I GOT LINE BY THE ABOVE CODE. – Vishal Oct 9 '18 at 9:22

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.