# Get coordinates of polyline

257
2
04-15-2024 08:40 AM
Occasional Contributor III

Hi all,

I want to get screenPoints of polyline (maybe array of points, no problem). For point features i use code:

let screenPoint = view.toScreen(feature.geometry);
console.log(screenPoint.x, screenPoint.y);
}

and it works fine, but how can achieve this for polyline?

1 Solution

Accepted Solutions
by
New Contributor III

A polyLine is just a series of points inside a 'paths' array.

You can just use a javascript .forEach() to go through each point in the array create a new Point from the coordinates and to output the screenPoint.
Note polyLines can have multiple line segments so you'll want to check each line segment as well.

//pseduoCode example
const polyLine = feature.geometry // Your polyLine feature.
polyLine.paths.forEach((path) => { //for each line segment inside your polyLine
path.forEach((coordinates) => { //for each coordinate in your line segment
const newPoint = new Point(...) //create a new Point using the path coordinates
let screenPoint = view.toScreen(newPoint );
console.log(screenPoint.x, screenPoint.y);
})
})

James from www.landkind.com
2 Replies
by
New Contributor III

A polyLine is just a series of points inside a 'paths' array.

You can just use a javascript .forEach() to go through each point in the array create a new Point from the coordinates and to output the screenPoint.
Note polyLines can have multiple line segments so you'll want to check each line segment as well.

//pseduoCode example
const polyLine = feature.geometry // Your polyLine feature.
polyLine.paths.forEach((path) => { //for each line segment inside your polyLine
path.forEach((coordinates) => { //for each coordinate in your line segment
const newPoint = new Point(...) //create a new Point using the path coordinates
let screenPoint = view.toScreen(newPoint );
console.log(screenPoint.x, screenPoint.y);
})
})

James from www.landkind.com
Occasional Contributor III

Hi @JamesIng, thank you. your answer is what i was looking for