0
votes

I am currently trying to layer some markers over a OSM map using OpenLayers. 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.

My current approach is to simpy use <meta http-equiv="refresh" content="3"> which reloads the page and subsequently the script. Problem with this approach is, that the OSM map is reset, i.e. zoom, view port, etc.

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.

<!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?

1

1 Answers

2
votes

OpenLayers.Layer.Text does not support strategies.

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);