Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

openlayers-3

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?

like image 435
nix86 Avatar asked May 27 '16 16:05

nix86


1 Answers

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()
like image 53
nix86 Avatar answered Nov 11 '22 10:11

nix86