1

I implemented a drawing feature (OpenLayers2) on an OpenStreetMap.

I searched about how to get coordinates from a drawing point. But I'm still stuck at this point.

I tried this code in order to get the coordinates instantly after drawing a new point but with no success:

 // HOW TO ADD A DRAWING FEATURE
 var DrawMyPoint = new OpenLayers.Control.DrawFeature( MyPointLayer, OpenLayers.Handler.Point)//,
     //{feature:GetPointCoordinates});
     // Point for creating point, polygon creating polygon, and so on.

     map.addControl(DrawMyPoint);

 // EVENT LINKED
 document.getElementById("DrawCheckBox").checked = PushOnButtonDraw();
 document.getElementById("DrawCheckBox").checked =GetPointCoordinates();           

 // FUNCTION TO GET COORDINATES OF THE POINT
 function GetPointCoordinates(){
    var coordinates = MyPointLayer.geometry.getBounds();
    console.log(coordinates);
    alert(coordinates);
 }

Here is a link where I saw the information about getBounds or getCoordinates : Openlayers get coordinates from polygon

I am not sure if I may link two functions with the same event, or is there another solution about it?

With getting the coordinates of each newest drawing point (which is my initial question), I would center the map to this coordinates too.

Do you have any idea?

3
  • With no answers now is the ideal time to revise your question to ask only about where you are currently stuck.
    – PolyGeo
    Commented Jun 22, 2017 at 20:06
  • I found the answer to my problem. Here is the code i used : / / HOW TO LINK THIS POINT LAYER INTO AN EVENT MyPointLayer.events.on({ featuresadded : GetCoordinatePoint }); function GetCoordinatePoint(event) { var Mycoordonate = event.features[0].geometry.getVertices(); var stringVertices = ""; for (var x in Mycoordonate) { stringVertices += "(" + Mycoordonate[x].x + "," + Mycoordonate[x].y + ")"; } console.log(stringVertices); }
    – Karro
    Commented Jun 23, 2017 at 9:22
  • 1
    @Karro post it as an answer so people can finds it more easily :)
    – LaughU
    Commented Jun 23, 2017 at 9:42

1 Answer 1

1

I found the answer to my problem. Here is the code i used :

MyPointLayer.events.on({ featuresadded : GetCoordinatePoint }); 
function GetCoordinatePoint(event) { var Mycoordonate = event.features[0].geometry.getVertices(); var stringVertices = ""; 

for (var x in Mycoordonate) { stringVertices += "(" + Mycoordonate[x].x + "," + Mycoordonate[x].y + ")"; }

console.log(stringVertices); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.