How to draw a line with OpenLayers by clicking two points?OpenLayers3 with google map local tiles serverOpenLayers3 change Layer source URL (or replace features loaded from another URL)Confusion in creating vector layerOpenlayers 3 connecting to a wms feed with a different projectionHow to get the geometry type of a WFS vector layer in OpenLayers 3?How to fit an OpenLayers view to an extent that spans the map extent (wrap) or IDL?Labeling function for GeoJSON Layers in Openlayers 3Multiple layers from one WMTS-capabitilies source in OpenlayersOpenLayers WMTS reprojectionopenlayers-automatically adding the layers on clicking dynamic checkboxes without actually specifying all the published layers attributes

Did 'Cinema Songs' exist during Hiranyakshipu's time?

Could neural networks be considered metaheuristics?

What's the meaning of "Sollensaussagen"?

Calculate the Mean mean of two numbers

Do Iron Man suits sport waste management systems?

How to prevent "they're falling in love" trope

Does the Idaho Potato Commission associate potato skins with healthy eating?

Does int main() need a declaration on C++?

Does marriage to a non-Numenorean disqualify a candidate for the crown of Gondor?

Blending or harmonizing

Notepad++ delete until colon for every line with replace all

How can a day be of 24 hours?

Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?

Why was the shrink from 8″ made only to 5.25″ and not smaller (4″ or less)

Is there a hemisphere-neutral way of specifying a season?

What historical events would have to change in order to make 19th century "steampunk" technology possible?

How badly should I try to prevent a user from XSSing themselves?

What Exploit Are These User Agents Trying to Use?

What exactly is ineptocracy?

How to stretch the corners of this image so that it looks like a perfect rectangle?

What do you call someone who asks many questions?

Mathematica command that allows it to read my intentions

Are British MPs missing the point, with these 'Indicative Votes'?

What does the same-ish mean?



How to draw a line with OpenLayers by clicking two points?


OpenLayers3 with google map local tiles serverOpenLayers3 change Layer source URL (or replace features loaded from another URL)Confusion in creating vector layerOpenlayers 3 connecting to a wms feed with a different projectionHow to get the geometry type of a WFS vector layer in OpenLayers 3?How to fit an OpenLayers view to an extent that spans the map extent (wrap) or IDL?Labeling function for GeoJSON Layers in Openlayers 3Multiple layers from one WMTS-capabitilies source in OpenlayersOpenLayers WMTS reprojectionopenlayers-automatically adding the layers on clicking dynamic checkboxes without actually specifying all the published layers attributes













1















I've got website with OpenLayers tile layer. I am trying to draw vector line on the map (only one segment between 2 points) on mouse click and to get coordinates of these two points. I read a lot on the Internet, but I could not handle it.



That's my code for adding OSM layer, layer switcher, view:



 var view = new ol.View(
projection: new ol.proj.Projection(
code: 'EPSG:3857',
units: 'm'
),
center: [1030534,5690437],
zoom: 12
)

var layers = [
new ol.layer.Tile(
source: new ol.source.OSM(),
title: 'Open Street Map'
)
];

var map = new ol.Map(
layers: layers,
target: 'map',
view: view,
controls: ol.control.defaults().extend([
new ol.control.OverviewMap()
])
);

var layerSwitcher = new ol.control.LayerSwitcher();
map.addControl(layerSwitcher);


I really have no idea how to make the line-part. I use this
example, but it's hard for me: https://openlayers.org/en/latest/examples/draw-features.html



I'd like to make something like LineString from the example above but only with one segment.



I tried with that, but it's obviously NOT right. As I said - I'm beginner.



 var source = new ol.source.Vector(wrapX: false);

var vector = new ol.layer.Vector(
title: 'Line',
source: source
);




var draw = new ol.Interaction.Draw(
source: source,
type: 'LineString'
);
map.addInteraction(draw);









share|improve this question









New contributor




BananaGrape is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Welcome to GIS SE. As a new user, please take the Tour. Coding questions here are expected to contain code. Please Edit the question to include what you have attempted, with a description of what behavior you are seeing.

    – Vince
    2 days ago











  • To limit the line to 2 points specify maxPoints: 2, as an option when you create the draw interaction

    – Mike
    2 days ago











  • Vince, Thanks! I will have it in mind the next time :)

    – BananaGrape
    2 days ago











  • What is missing is just map.addLayer(vector) at the end.

    – TomazicM
    2 days ago















1















I've got website with OpenLayers tile layer. I am trying to draw vector line on the map (only one segment between 2 points) on mouse click and to get coordinates of these two points. I read a lot on the Internet, but I could not handle it.



That's my code for adding OSM layer, layer switcher, view:



 var view = new ol.View(
projection: new ol.proj.Projection(
code: 'EPSG:3857',
units: 'm'
),
center: [1030534,5690437],
zoom: 12
)

var layers = [
new ol.layer.Tile(
source: new ol.source.OSM(),
title: 'Open Street Map'
)
];

var map = new ol.Map(
layers: layers,
target: 'map',
view: view,
controls: ol.control.defaults().extend([
new ol.control.OverviewMap()
])
);

var layerSwitcher = new ol.control.LayerSwitcher();
map.addControl(layerSwitcher);


I really have no idea how to make the line-part. I use this
example, but it's hard for me: https://openlayers.org/en/latest/examples/draw-features.html



I'd like to make something like LineString from the example above but only with one segment.



I tried with that, but it's obviously NOT right. As I said - I'm beginner.



 var source = new ol.source.Vector(wrapX: false);

var vector = new ol.layer.Vector(
title: 'Line',
source: source
);




var draw = new ol.Interaction.Draw(
source: source,
type: 'LineString'
);
map.addInteraction(draw);









share|improve this question









New contributor




BananaGrape is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Welcome to GIS SE. As a new user, please take the Tour. Coding questions here are expected to contain code. Please Edit the question to include what you have attempted, with a description of what behavior you are seeing.

    – Vince
    2 days ago











  • To limit the line to 2 points specify maxPoints: 2, as an option when you create the draw interaction

    – Mike
    2 days ago











  • Vince, Thanks! I will have it in mind the next time :)

    – BananaGrape
    2 days ago











  • What is missing is just map.addLayer(vector) at the end.

    – TomazicM
    2 days ago













1












1








1








I've got website with OpenLayers tile layer. I am trying to draw vector line on the map (only one segment between 2 points) on mouse click and to get coordinates of these two points. I read a lot on the Internet, but I could not handle it.



That's my code for adding OSM layer, layer switcher, view:



 var view = new ol.View(
projection: new ol.proj.Projection(
code: 'EPSG:3857',
units: 'm'
),
center: [1030534,5690437],
zoom: 12
)

var layers = [
new ol.layer.Tile(
source: new ol.source.OSM(),
title: 'Open Street Map'
)
];

var map = new ol.Map(
layers: layers,
target: 'map',
view: view,
controls: ol.control.defaults().extend([
new ol.control.OverviewMap()
])
);

var layerSwitcher = new ol.control.LayerSwitcher();
map.addControl(layerSwitcher);


I really have no idea how to make the line-part. I use this
example, but it's hard for me: https://openlayers.org/en/latest/examples/draw-features.html



I'd like to make something like LineString from the example above but only with one segment.



I tried with that, but it's obviously NOT right. As I said - I'm beginner.



 var source = new ol.source.Vector(wrapX: false);

var vector = new ol.layer.Vector(
title: 'Line',
source: source
);




var draw = new ol.Interaction.Draw(
source: source,
type: 'LineString'
);
map.addInteraction(draw);









share|improve this question









New contributor




BananaGrape is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I've got website with OpenLayers tile layer. I am trying to draw vector line on the map (only one segment between 2 points) on mouse click and to get coordinates of these two points. I read a lot on the Internet, but I could not handle it.



That's my code for adding OSM layer, layer switcher, view:



 var view = new ol.View(
projection: new ol.proj.Projection(
code: 'EPSG:3857',
units: 'm'
),
center: [1030534,5690437],
zoom: 12
)

var layers = [
new ol.layer.Tile(
source: new ol.source.OSM(),
title: 'Open Street Map'
)
];

var map = new ol.Map(
layers: layers,
target: 'map',
view: view,
controls: ol.control.defaults().extend([
new ol.control.OverviewMap()
])
);

var layerSwitcher = new ol.control.LayerSwitcher();
map.addControl(layerSwitcher);


I really have no idea how to make the line-part. I use this
example, but it's hard for me: https://openlayers.org/en/latest/examples/draw-features.html



I'd like to make something like LineString from the example above but only with one segment.



I tried with that, but it's obviously NOT right. As I said - I'm beginner.



 var source = new ol.source.Vector(wrapX: false);

var vector = new ol.layer.Vector(
title: 'Line',
source: source
);




var draw = new ol.Interaction.Draw(
source: source,
type: 'LineString'
);
map.addInteraction(draw);






openlayers line coordinates






share|improve this question









New contributor




BananaGrape is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




BananaGrape is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









Vince

14.8k32849




14.8k32849






New contributor




BananaGrape is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









BananaGrapeBananaGrape

83




83




New contributor




BananaGrape is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





BananaGrape is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






BananaGrape is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Welcome to GIS SE. As a new user, please take the Tour. Coding questions here are expected to contain code. Please Edit the question to include what you have attempted, with a description of what behavior you are seeing.

    – Vince
    2 days ago











  • To limit the line to 2 points specify maxPoints: 2, as an option when you create the draw interaction

    – Mike
    2 days ago











  • Vince, Thanks! I will have it in mind the next time :)

    – BananaGrape
    2 days ago











  • What is missing is just map.addLayer(vector) at the end.

    – TomazicM
    2 days ago

















  • Welcome to GIS SE. As a new user, please take the Tour. Coding questions here are expected to contain code. Please Edit the question to include what you have attempted, with a description of what behavior you are seeing.

    – Vince
    2 days ago











  • To limit the line to 2 points specify maxPoints: 2, as an option when you create the draw interaction

    – Mike
    2 days ago











  • Vince, Thanks! I will have it in mind the next time :)

    – BananaGrape
    2 days ago











  • What is missing is just map.addLayer(vector) at the end.

    – TomazicM
    2 days ago
















Welcome to GIS SE. As a new user, please take the Tour. Coding questions here are expected to contain code. Please Edit the question to include what you have attempted, with a description of what behavior you are seeing.

– Vince
2 days ago





Welcome to GIS SE. As a new user, please take the Tour. Coding questions here are expected to contain code. Please Edit the question to include what you have attempted, with a description of what behavior you are seeing.

– Vince
2 days ago













To limit the line to 2 points specify maxPoints: 2, as an option when you create the draw interaction

– Mike
2 days ago





To limit the line to 2 points specify maxPoints: 2, as an option when you create the draw interaction

– Mike
2 days ago













Vince, Thanks! I will have it in mind the next time :)

– BananaGrape
2 days ago





Vince, Thanks! I will have it in mind the next time :)

– BananaGrape
2 days ago













What is missing is just map.addLayer(vector) at the end.

– TomazicM
2 days ago





What is missing is just map.addLayer(vector) at the end.

– TomazicM
2 days ago










1 Answer
1






active

oldest

votes


















2














The vector layer needs to be included in the map and you will need to process information from your drawn lines somewhere, the drawend event would be the best place to do that. Views default to EPSG:3857 so you don't need to define it.



var view = new ol.View(
center: [1030534,5690437],
zoom: 12
)

var source = new ol.source.Vector();

var vector = new ol.layer.Vector(
title: 'Line',
source: source
);

var layers = [
new ol.layer.Tile(
source: new ol.source.OSM(),
title: 'Open Street Map'
),
vector
];

var map = new ol.Map(
layers: layers,
target: 'map',
view: view,
controls: ol.control.defaults().extend([
new ol.control.OverviewMap()
])
);

var draw = new ol.interaction.Draw(
source: source,
type: 'LineString',
maxPoints: 2
);

draw.on('drawend', function(evt)
// log the coordinates and lon/lat
var coordinates = evt.feature.getGeometry().getCoordinates();
var llCoordinates = evt.feature.getGeometry().clone().transform(view.getProjection(), 'EPSG:4326').getCoordinates();
console.log(coordinates);
alert(JSON.stringify(llCoordinates, null, 2));
);

map.addInteraction(draw);





share|improve this answer























  • Thanks a lot! i really appreciate your help :)

    – BananaGrape
    2 days ago











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "79"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






BananaGrape is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f317272%2fhow-to-draw-a-line-with-openlayers-by-clicking-two-points%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














The vector layer needs to be included in the map and you will need to process information from your drawn lines somewhere, the drawend event would be the best place to do that. Views default to EPSG:3857 so you don't need to define it.



var view = new ol.View(
center: [1030534,5690437],
zoom: 12
)

var source = new ol.source.Vector();

var vector = new ol.layer.Vector(
title: 'Line',
source: source
);

var layers = [
new ol.layer.Tile(
source: new ol.source.OSM(),
title: 'Open Street Map'
),
vector
];

var map = new ol.Map(
layers: layers,
target: 'map',
view: view,
controls: ol.control.defaults().extend([
new ol.control.OverviewMap()
])
);

var draw = new ol.interaction.Draw(
source: source,
type: 'LineString',
maxPoints: 2
);

draw.on('drawend', function(evt)
// log the coordinates and lon/lat
var coordinates = evt.feature.getGeometry().getCoordinates();
var llCoordinates = evt.feature.getGeometry().clone().transform(view.getProjection(), 'EPSG:4326').getCoordinates();
console.log(coordinates);
alert(JSON.stringify(llCoordinates, null, 2));
);

map.addInteraction(draw);





share|improve this answer























  • Thanks a lot! i really appreciate your help :)

    – BananaGrape
    2 days ago















2














The vector layer needs to be included in the map and you will need to process information from your drawn lines somewhere, the drawend event would be the best place to do that. Views default to EPSG:3857 so you don't need to define it.



var view = new ol.View(
center: [1030534,5690437],
zoom: 12
)

var source = new ol.source.Vector();

var vector = new ol.layer.Vector(
title: 'Line',
source: source
);

var layers = [
new ol.layer.Tile(
source: new ol.source.OSM(),
title: 'Open Street Map'
),
vector
];

var map = new ol.Map(
layers: layers,
target: 'map',
view: view,
controls: ol.control.defaults().extend([
new ol.control.OverviewMap()
])
);

var draw = new ol.interaction.Draw(
source: source,
type: 'LineString',
maxPoints: 2
);

draw.on('drawend', function(evt)
// log the coordinates and lon/lat
var coordinates = evt.feature.getGeometry().getCoordinates();
var llCoordinates = evt.feature.getGeometry().clone().transform(view.getProjection(), 'EPSG:4326').getCoordinates();
console.log(coordinates);
alert(JSON.stringify(llCoordinates, null, 2));
);

map.addInteraction(draw);





share|improve this answer























  • Thanks a lot! i really appreciate your help :)

    – BananaGrape
    2 days ago













2












2








2







The vector layer needs to be included in the map and you will need to process information from your drawn lines somewhere, the drawend event would be the best place to do that. Views default to EPSG:3857 so you don't need to define it.



var view = new ol.View(
center: [1030534,5690437],
zoom: 12
)

var source = new ol.source.Vector();

var vector = new ol.layer.Vector(
title: 'Line',
source: source
);

var layers = [
new ol.layer.Tile(
source: new ol.source.OSM(),
title: 'Open Street Map'
),
vector
];

var map = new ol.Map(
layers: layers,
target: 'map',
view: view,
controls: ol.control.defaults().extend([
new ol.control.OverviewMap()
])
);

var draw = new ol.interaction.Draw(
source: source,
type: 'LineString',
maxPoints: 2
);

draw.on('drawend', function(evt)
// log the coordinates and lon/lat
var coordinates = evt.feature.getGeometry().getCoordinates();
var llCoordinates = evt.feature.getGeometry().clone().transform(view.getProjection(), 'EPSG:4326').getCoordinates();
console.log(coordinates);
alert(JSON.stringify(llCoordinates, null, 2));
);

map.addInteraction(draw);





share|improve this answer













The vector layer needs to be included in the map and you will need to process information from your drawn lines somewhere, the drawend event would be the best place to do that. Views default to EPSG:3857 so you don't need to define it.



var view = new ol.View(
center: [1030534,5690437],
zoom: 12
)

var source = new ol.source.Vector();

var vector = new ol.layer.Vector(
title: 'Line',
source: source
);

var layers = [
new ol.layer.Tile(
source: new ol.source.OSM(),
title: 'Open Street Map'
),
vector
];

var map = new ol.Map(
layers: layers,
target: 'map',
view: view,
controls: ol.control.defaults().extend([
new ol.control.OverviewMap()
])
);

var draw = new ol.interaction.Draw(
source: source,
type: 'LineString',
maxPoints: 2
);

draw.on('drawend', function(evt)
// log the coordinates and lon/lat
var coordinates = evt.feature.getGeometry().getCoordinates();
var llCoordinates = evt.feature.getGeometry().clone().transform(view.getProjection(), 'EPSG:4326').getCoordinates();
console.log(coordinates);
alert(JSON.stringify(llCoordinates, null, 2));
);

map.addInteraction(draw);






share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









MikeMike

2,445139




2,445139












  • Thanks a lot! i really appreciate your help :)

    – BananaGrape
    2 days ago

















  • Thanks a lot! i really appreciate your help :)

    – BananaGrape
    2 days ago
















Thanks a lot! i really appreciate your help :)

– BananaGrape
2 days ago





Thanks a lot! i really appreciate your help :)

– BananaGrape
2 days ago










BananaGrape is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















BananaGrape is a new contributor. Be nice, and check out our Code of Conduct.












BananaGrape is a new contributor. Be nice, and check out our Code of Conduct.











BananaGrape is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f317272%2fhow-to-draw-a-line-with-openlayers-by-clicking-two-points%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

getting Checkpoint VPN SSL Network Extender working in the command lineHow to connect to CheckPoint VPN on Ubuntu 18.04LTS?Will the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayVPN SSL Network Extender in FirefoxLinux Checkpoint SNX tool configuration issuesCheck Point - Connect under Linux - snx + OTPSNX VPN Ububuntu 18.XXUsing Checkpoint VPN SSL Network Extender CLI with certificateVPN with network manager (nm-applet) is not workingWill the Linux ( red-hat ) Open VPNC Client connect to checkpoint or nortel VPN gateways?VPN client for linux machine + support checkpoint gatewayImport VPN config files to NetworkManager from command lineTrouble connecting to VPN using network-manager, while command line worksStart a VPN connection with PPTP protocol on command linestarting a docker service daemon breaks the vpn networkCan't connect to vpn with Network-managerVPN SSL Network Extender in FirefoxUsing Checkpoint VPN SSL Network Extender CLI with certificate

Cannot Extend partition with GParted The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election ResultsCan't increase partition size with GParted?GParted doesn't recognize the unallocated space after my current partitionWhat is the best way to add unallocated space located before to Ubuntu 12.04 partition with GParted live?I can't figure out how to extend my Arch home partition into free spaceGparted Linux Mint 18.1 issueTrying to extend but swap partition is showing as Unknown in Gparted, shows proper from fdiskRearrange partitions in gparted to extend a partitionUnable to extend partition even though unallocated space is next to it using GPartedAllocate free space to root partitiongparted: how to merge unallocated space with a partition

Marilyn Monroe Ny fiainany manokana | Jereo koa | Meny fitetezanafanitarana azy.