There is a form to within a web page, map of OpenLayers is displayed in the form. I look at the source code of Html, and further a hierarchical state in the form. My goal is to get the Polygon at the bottom, but do not know how to get the map object of OpenLayers.

Html source code

<form id="map_form" method="get" action="#">
  <div id="map_content" class="section map">
   <div id="map_object" class="section object">
    <div id="map" class="olMap">
     <div id="OpenLayers.Map_7_OpenLayers_ViewPort">
      <div id="OpenLayers.Map_7_events">
       <div id="OpenLayers.Map_7_OpenLayers_Container">
        <div id="webtis.Layer.BaseMap_43">
         <div id="OpenLayers.Layer.Vector_67">
          <svg id="OpenLayers.Layer.Vector_67_svgRoot">
           <g id="OpenLayers.Layer.Vector_67_root" style="visibility: visible;" transform="">
            <g id="OpenLayers.Layer.Vector_67_vroot">
             <path id="OpenLayers.Geometry.Polygon_3503" d=" M ...

Also tried the following methods.

1. map = new OpenLayers.Map (document.getElementById ('OpenLayers.Map_7_OpenLayers_ViewPort'))
2. map = new OpenLayers.Map (document.getElementById ('map_content'))
3. map = new OpenLayers.Map (document.getElementById ('map_object'))

Which method but also map.layers.length was 0.

How can I get the OpenLayers's map object?

  • The source code is generated by OpenLayers when the map is created. Do you really want to get the Polygon SVG element or a JavaScript object with the feature from a vector layer?
    – Jorge Sanz
    Jan 20 '15 at 13:54
  • Yes,In the Polygon SVG element's innerTEXT,Have been described coordinate value of each vertex.I want to get these coordinates.
    – T J
    Jan 21 '15 at 2:05

You don't have to create a new OL map. The OpenLayers map has already been created inside the form and if everything is ok, the SVG elements will be there. Take in care tha OL can render also on canvas instead of SVG, it depends on how the map is coded.

If you really want to take the drawing coordinates from the generated SVG you can just explore the DOM inside the form (based on its id) to look for a "path" tag.

  • Thank your answer.Sorry,My explanation was not enough. If get shape through the DOM, because only shapes that are currently displayed in the screen can not be obtained, So I want to get through the Map object of OpenLayers.
    – T J
    Jan 25 '15 at 1:42
  • Can you explain why you want to grab the coordinates from the DOM and not from the vector layer object?
    – Thomas B
    Jan 25 '15 at 11:31
  • If you want to take the features of a vector layer you should first get a reference to that layer and then add a listener to get a trigger when the features are loaded as this process is usually asynchronous. The code will be a little bit different from OL2 to OL3 but the procedure is more or less the same.
    – Jorge Sanz
    Jan 26 '15 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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