简体   繁体   English

自动重新加载OpenLayers.Layer.Text标记的数据

[英]Reload data for OpenLayers.Layer.Text marker automatically

I am currently trying to layer some markers over a OSM map using OpenLayers. 我目前正在尝试使用OpenLayers在OSM地图上分层一些标记。 The page should refresh in a certain interval and load new data from the DB. 该页面应以一定的间隔刷新,并从数据库加载新数据。

I feed the data from the DB to the OpenLayers.Layer.Text through a local servlet which constructs the input file from the records in the DB. 我通过本地servlet从数据库将数据提供给OpenLayers.Layer.Text,该servlet从数据库中的记录构造输入文件。

My current approach is to simpy use <meta http-equiv="refresh" content="3"> which reloads the page and subsequently the script. 我当前的方法是使用<meta http-equiv="refresh" content="3">进行简单化,这将重新加载页面并随后重新加载脚本。 Problem with this approach is, that the OSM map is reset, ie zoom, view port, etc. 这种方法的问题是OSM映射被重置,即缩放,查看端口等。

I have thus tried to use a refresh strategy from OpenLayers but this does not seem to reload the data from the servlet for the corresponding layer. 因此,我尝试使用OpenLayers的刷新策略,但这似乎并未从servlet的相应层重新加载数据。

<!DOCTYPE html>
<html>
    <head>
        <title>OSM View/title>
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <!--meta http-equiv="refresh" content="3"-->
    </head>
    <body>
        <div id="mapdiv" style="height:750px"></div>
        <script src="http://www.openlayers.org/api/OpenLayers.js"></script>
        <script>
            var lat = 52;
            var lon = 10;
            var zoom = 18;

            map = new OpenLayers.Map("mapdiv", {
                controls: [
                    new OpenLayers.Control.Navigation(),
                    new OpenLayers.Control.PanZoomBar(),
                    new OpenLayers.Control.LayerSwitcher()
                ],
                projection: 'EPSG:900913',
                displayProjection: 'EPSG:4326',
                numZoomLevels: 18,
                units: 'm'
            });

            mapnik = new OpenLayers.Layer.OSM("Karte");
            map.addLayer(mapnik);

            var marker = new OpenLayers.Layer.Text("marker", {
                location: "http://localhost:8080/marker",
                projection: map.displayProjection,
                strategies: [new OpenLayers.Strategy.Refresh({interval: 3000, force: true})]
            });
            map.addLayer(marker);

            var fromProjection = new OpenLayers.Projection("EPSG:4326"); // Transform from WGS 1984
            var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection
            var position = new OpenLayers.LonLat(lon, lat).transform(fromProjection, toProjection);

            map.setCenter(position, zoom);
        </script>
    </body>
</html>

Is there a way to achieve what I am trying to do? 有没有办法实现我想做的事情?

OpenLayers.Layer.Text does not support strategies. OpenLayers.Layer.Text不支持策略。

Looking at the code, there isn't any function to reload the data, but you should be able to reload layer data using: 查看代码,没有任何功能可以重新加载数据,但是您应该能够使用以下方法重新加载图层数据:

setInterval(function() {
    layer.clearFeatures();
    layer.loaded = false;
    layer.loadText();
}, 3 * 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM