[OpenLayers-Users] center crosshairs?

Arnd Wippermann arnd.wippermann at web.de
Tue Sep 9 17:30:02 EDT 2008


Hi,

To get a visible center on the map, i create a vector layer with a
horizontal and a vertical line updated through
map.events.register('move',...).

You can test it with the following code. Run it in the firebug console.

Arnd Wippermann

map.events.register('move', map, function()
{
    point = map.getCenter();
    setCenterGrid(map, point.lon, point.lat);
});

function setCenterGridLayer(mapObj, strokeWidth, strokeColor)
{
    var flag=false;

    //Look for layer Fadenkreuz
    for(var i=0;i<mapObj.layers;i++)
        if(mapObj.layers[i].Fadenkreuz)
            flag=true;

    //layer Fadenkreuz not there, then create
    if(flag==false)
    {   
        var vector = new OpenLayers.Layer.Vector("Fadenkreuz");
        vector.styleMap = new OpenLayers.StyleMap({'default':{'strokeWidth':
strokeWidth, 'strokeColor': strokeColor}});
        vector.Fadenkreuz = true;
        map.addLayer(vector);
    }
}

function setCenterGrid(mapObj, x, y)
{
    var idx=-1;

    //get index for layer Fadenkreuz
    for(var i=0;i<mapObj.layers.length;i++)
        if(mapObj.layers[i].Fadenkreuz)
            idx=i;

    if(idx==-1)
        return;

    mapObj.layers[idx].destroyFeatures();

    var pointAw = new OpenLayers.Geometry.Point(mapObj.getExtent().left, y);
    var pointBw = new OpenLayers.Geometry.Point(mapObj.getExtent().right,
y);
    var pointAs = new OpenLayers.Geometry.Point(x, mapObj.getExtent().top);
    var pointBs = new OpenLayers.Geometry.Point(x,
mapObj.getExtent().bottom);

    var lineFeatureW = new OpenLayers.Feature.Vector(new
OpenLayers.Geometry.LineString([pointAw, pointBw]),null,null);
    var lineFeatureS = new OpenLayers.Feature.Vector(new
OpenLayers.Geometry.LineString([pointAs, pointBs]),null,null);
    mapObj.layers[idx].addFeatures([lineFeatureW, lineFeatureS]);
}

setCenterGridLayer(map, 1, "white");




More information about the Users mailing list