[GIS] Problem with panning and zooming in OpenLayers

javascriptopenlayers-2web-mapping

I'm interested in detecting when a user moves or pans a map so that I can run a new query on a database based on their location. However, the following code causes the panning and zooming controls to go crazy:

map.events.register("moveend", map, function(){              
    getValues(map.center);               
    });

If I comment out these lines, there appears to be no problem. If they are included, after two pans or zooms, the map suddenly jumps to half-way around the world (it looks similar to 0 lat, 0 long). I'm unsure what I am doing incorrectly.

The other problem I have regarding the pan/zooming is that the movend does not really seem to wait until the user has finished moving or panning. Queries seem to be fired off even as the map is being moved and panned. Again, I'm not sure if this is a problem with how I am detecting this but it does not seem to behave as I thought it would.

EDIT: Including getValues function also:

getValues = function(c){

    c.transform(map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326") )
    console.log(c.lat, c.lon)

    $.ajax({ 
            type: "POST",
            url: "php/query.php" ,  
            data:{x: c.lat, y:c.lon},  
            async: true,
            success: function(r){
                    console.log(r);
            }, 
        dataType:'json'
        }); 

    }

Best Answer

I've used this event successfully. A delay method similar to the one here can help any thrashing. The actual method looks like this:

delay: function( timeout, id, callback )
    {
            this.delays = this.delays || [];
            var delay = this.delays[ id ];
            if ( delay )
            {
                    clearTimeout ( delay );
            }
            delay = setTimeout ( callback, timeout );
            this.delays[ id ] = delay;
    }

Usage Example:

delay(1000, "uniqueId", function() {
    getValues(map.getCenter()); 
});

I suspect your getValues method is modifying the map causing the moveend event to fire.