How can I get the coordinates of a feature in OpenLayers 3
Asked Answered
P

1

16

So far I have tried with this code

feature.getGeometry().getCoordinates()

but it doesn't work. The error I get is "Property getCoordinates does not exist on type Geometry". May it have something to do with the fact that I'm using TypeScript?

Plumbic answered 27/5, 2016 at 16:18 Comment(1)
"many say"? The documentation should be your reference point and it clearly doesn't specify the getCoordinates method on the Geometry object, it's defined on individual subclasses. You didn't even say what kind of feature you were interested in.Ammon
P
24

Yes it has to do with the fact that I'm using TypeScript. In fact in TypeScript you must proceed like this:

let p: ol.geom.Point = <ol.geom.Point>feature.getGeometry();
let c: ol.Coordinate = p.getCoordinates();

Basically you first have to carry out a cast from Geometry to Point. Then you get get the coordinates.

Edit ol v6.13.0

They do now work with typescript, the feature should be declared as follow

feature: Feature<Point> = new Feature()

feature.getGeometry().getCoordinates()
Plumbic answered 30/5, 2016 at 7:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.