如何使用 Openlayers 显示此代码中的纬度和经度?

发布于 2024-10-19 16:14:43 字数 4715 浏览 1 评论 0原文

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
        <title>Drag Feature Example</title>

        <link rel="stylesheet" href="../theme/default/style.css" type="text/css" />
        <link rel="stylesheet" href="style.css" type="text/css" />
        <style type="text/css">
            #controls {
                width: 512px;
            }
            #controlToggle {
                padding-left: 1em;
            }
            #controlToggle li {
                list-style: none;
            }
        </style>

        <script src="../OpenLayers.js"></script>
        <script type="text/javascript">
            var map, vectors, controls;
            function init(){
                map = new OpenLayers.Map('map');
                var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
                    "http://vmap0.tiles.osgeo.org/wms/vmap0?", {layers: 'basic'});

                vectors = new OpenLayers.Layer.Vector("Vector Layer");

                map.addLayers([wms, vectors]);
                map.addControl(new OpenLayers.Control.LayerSwitcher());
                map.addControl(new OpenLayers.Control.MousePosition());
        //map.addControl(new OpenLayers.Control.Click());

                controls = {
                    point: new OpenLayers.Control.DrawFeature(vectors,
                                OpenLayers.Handler.Point),

                    line: new OpenLayers.Control.DrawFeature(vectors,
                                OpenLayers.Handler.Path),
                    polygon: new OpenLayers.Control.DrawFeature(vectors,
                                OpenLayers.Handler.Polygon),
                    drag: new OpenLayers.Control.DragFeature(vectors)
                };

                for(var key in controls) {
                    map.addControl(controls[key]);
           // map.addControl(click);
            }

                map.setCenter(new OpenLayers.LonLat(0, 0), 3);
                document.getElementById('noneToggle').checked = true;
            }

            function toggleControl(element) {
                for(key in controls) {
                    var control = controls[key];
                    if(element.value == key && element.checked) {
                        control.activate();
                    } else {
                        control.deactivate();
                    }
                }
            }
        </script>
    </head>
    <body onload="init()">
        <h1 id="title">Drag Feature Example</h1>

        <div id="tags">
            point, line, linestring, polygon, digitizing, geometry, draw, drag
        </div>

        <p id="shortdesc">
            Demonstrates point, line and polygon creation and editing.
        </p>

        <div id="map" class="smallmap"></div>

        <div id="controls">
            <ul id="controlToggle">
                <li>
                    <input type="radio" name="type" value="none" id="noneToggle"
                           onclick="toggleControl(this);" checked="checked" />

                    <label for="noneToggle">navigate</label>
                </li>
                <li>
                    <input type="radio" name="type" value="point" id="pointToggle" onclick="toggleControl(this);" />
                    <label for="pointToggle">draw point</label>
                </li>
                <li>
                    <input type="radio" name="type" value="line" id="lineToggle" onclick="toggleControl(this);" />

                    <label for="lineToggle">draw line</label>
                </li>
                <li>
                    <input type="radio" name="type" value="polygon" id="polygonToggle" onclick="toggleControl(this);" />
                    <label for="polygonToggle">draw polygon</label>
                </li>
                <li>
                    <input type="radio" name="type" value="drag" id="dragToggle"
                           onclick="toggleControl(this);" />

                    <label for="dragToggle">drag feature</label>
                </li>
            </ul>
        </div>

        <div id="docs"></div>
    </body>
</html>

我想在文本框中显示每次鼠标单击的纬度和经度。我无法理解应该从哪里得出纬度和经度?

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
        <title>Drag Feature Example</title>

        <link rel="stylesheet" href="../theme/default/style.css" type="text/css" />
        <link rel="stylesheet" href="style.css" type="text/css" />
        <style type="text/css">
            #controls {
                width: 512px;
            }
            #controlToggle {
                padding-left: 1em;
            }
            #controlToggle li {
                list-style: none;
            }
        </style>

        <script src="../OpenLayers.js"></script>
        <script type="text/javascript">
            var map, vectors, controls;
            function init(){
                map = new OpenLayers.Map('map');
                var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
                    "http://vmap0.tiles.osgeo.org/wms/vmap0?", {layers: 'basic'});

                vectors = new OpenLayers.Layer.Vector("Vector Layer");

                map.addLayers([wms, vectors]);
                map.addControl(new OpenLayers.Control.LayerSwitcher());
                map.addControl(new OpenLayers.Control.MousePosition());
        //map.addControl(new OpenLayers.Control.Click());

                controls = {
                    point: new OpenLayers.Control.DrawFeature(vectors,
                                OpenLayers.Handler.Point),

                    line: new OpenLayers.Control.DrawFeature(vectors,
                                OpenLayers.Handler.Path),
                    polygon: new OpenLayers.Control.DrawFeature(vectors,
                                OpenLayers.Handler.Polygon),
                    drag: new OpenLayers.Control.DragFeature(vectors)
                };

                for(var key in controls) {
                    map.addControl(controls[key]);
           // map.addControl(click);
            }

                map.setCenter(new OpenLayers.LonLat(0, 0), 3);
                document.getElementById('noneToggle').checked = true;
            }

            function toggleControl(element) {
                for(key in controls) {
                    var control = controls[key];
                    if(element.value == key && element.checked) {
                        control.activate();
                    } else {
                        control.deactivate();
                    }
                }
            }
        </script>
    </head>
    <body onload="init()">
        <h1 id="title">Drag Feature Example</h1>

        <div id="tags">
            point, line, linestring, polygon, digitizing, geometry, draw, drag
        </div>

        <p id="shortdesc">
            Demonstrates point, line and polygon creation and editing.
        </p>

        <div id="map" class="smallmap"></div>

        <div id="controls">
            <ul id="controlToggle">
                <li>
                    <input type="radio" name="type" value="none" id="noneToggle"
                           onclick="toggleControl(this);" checked="checked" />

                    <label for="noneToggle">navigate</label>
                </li>
                <li>
                    <input type="radio" name="type" value="point" id="pointToggle" onclick="toggleControl(this);" />
                    <label for="pointToggle">draw point</label>
                </li>
                <li>
                    <input type="radio" name="type" value="line" id="lineToggle" onclick="toggleControl(this);" />

                    <label for="lineToggle">draw line</label>
                </li>
                <li>
                    <input type="radio" name="type" value="polygon" id="polygonToggle" onclick="toggleControl(this);" />
                    <label for="polygonToggle">draw polygon</label>
                </li>
                <li>
                    <input type="radio" name="type" value="drag" id="dragToggle"
                           onclick="toggleControl(this);" />

                    <label for="dragToggle">drag feature</label>
                </li>
            </ul>
        </div>

        <div id="docs"></div>
    </body>
</html>

I want to display the latitude and longtiude of every mouse click in a text box. I am not able to understand from where I should derive the latitude and longitude?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不语却知心 2024-10-26 16:14:43

这就是您所需要的:

map.events.register("click", map, function(e) {
  var position = map.getLonLatFromPixel(e.xy);

  OpenLayers.Util.getElement("mydiv").innerHTML = 
        position.lon.toFixed(3) + ', ' + position.lat.toFixed(3);

});

您可以在此处看到正在运行的代码: http://jsbin.com/uyuka6

所以,你的代码应该看起来:

function init(){

      var map, vectors, controls;
      map = new OpenLayers.Map('map');
      var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
        "http://vmap0.tiles.osgeo.org/wms/vmap0?", {layers: 'basic'});

      vectors = new OpenLayers.Layer.Vector("Vector Layer");

      map.addLayers([wms, vectors]);
      map.addControl(new OpenLayers.Control.LayerSwitcher());
      map.addControl(new OpenLayers.Control.MousePosition());
      //map.addControl(new OpenLayers.Control.Click());

      map.events.register("click", map, function(e) {
         var position = map.getLonLatFromPixel(e.xy);

         OpenLayers.Util.getElement("shortdesc").innerHTML =
             position.lon.toFixed(3) + ', ' + position.lat.toFixed(3);

      });

      controls = {
           point: new OpenLayers.Control.DrawFeature(vectors,
             OpenLayers.Handler.Point),

           line: new OpenLayers.Control.DrawFeature(vectors,
             OpenLayers.Handler.Path),
           polygon: new OpenLayers.Control.DrawFeature(vectors,
             OpenLayers.Handler.Polygon),
           drag: new OpenLayers.Control.DragFeature(vectors)
       };

       var poly = controls['polygon'].handler;
       poly.callbacks.point = function(point){
           OpenLayers.Util.getElement("docs").innerHTML = point.toString();
       };

       var line = controls['line'].handler;
       line.callbacks.point = function(point){
          OpenLayers.Util.getElement("docs").innerHTML = point.toString();
       };

       for(var key in controls) {
          map.addControl(controls[key]);
          // map.addControl(click);
       }

       map.setCenter(new OpenLayers.LonLat(0, 0), 3);
       document.getElementById('noneToggle').checked = true;
}

This is what you need:

map.events.register("click", map, function(e) {
  var position = map.getLonLatFromPixel(e.xy);

  OpenLayers.Util.getElement("mydiv").innerHTML = 
        position.lon.toFixed(3) + ', ' + position.lat.toFixed(3);

});

You can see here the code working: http://jsbin.com/uyuka6

So, your code should looks:

function init(){

      var map, vectors, controls;
      map = new OpenLayers.Map('map');
      var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
        "http://vmap0.tiles.osgeo.org/wms/vmap0?", {layers: 'basic'});

      vectors = new OpenLayers.Layer.Vector("Vector Layer");

      map.addLayers([wms, vectors]);
      map.addControl(new OpenLayers.Control.LayerSwitcher());
      map.addControl(new OpenLayers.Control.MousePosition());
      //map.addControl(new OpenLayers.Control.Click());

      map.events.register("click", map, function(e) {
         var position = map.getLonLatFromPixel(e.xy);

         OpenLayers.Util.getElement("shortdesc").innerHTML =
             position.lon.toFixed(3) + ', ' + position.lat.toFixed(3);

      });

      controls = {
           point: new OpenLayers.Control.DrawFeature(vectors,
             OpenLayers.Handler.Point),

           line: new OpenLayers.Control.DrawFeature(vectors,
             OpenLayers.Handler.Path),
           polygon: new OpenLayers.Control.DrawFeature(vectors,
             OpenLayers.Handler.Polygon),
           drag: new OpenLayers.Control.DragFeature(vectors)
       };

       var poly = controls['polygon'].handler;
       poly.callbacks.point = function(point){
           OpenLayers.Util.getElement("docs").innerHTML = point.toString();
       };

       var line = controls['line'].handler;
       line.callbacks.point = function(point){
          OpenLayers.Util.getElement("docs").innerHTML = point.toString();
       };

       for(var key in controls) {
          map.addControl(controls[key]);
          // map.addControl(click);
       }

       map.setCenter(new OpenLayers.LonLat(0, 0), 3);
       document.getElementById('noneToggle').checked = true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文