I am drawing Geoserver layers on google map using Leaflet. Base database for Geoserver is PostgreSQL.In PostgreSQL there is a table that stores Millions of polygon.These polygons are in the form of square of 300 sq. feet each. I want to search polygons within 50 Miles in circular radius from map's center location. For this I need center location of map whenever user zoom in/out or drag the map.So that I can do a query in PostgreSQL on the basis of location. Here is the code.

   //layersearcharea = user will enter this value.
  //layersearcharea is area that will be used in circular radius search 
  //algorithm or say KNN algorithm

    var viewparams = ['searcharea:' + layersearcharea,'centerlat:'+map.getCenter().lat,'centerlong:' + map.getCenter().lng];

                    var baseURL="http://localhost:8080/geoserver/wms?";
                     var baseURL=encodeURI(baseURL+"viewparams="+viewparams.join(';'));

                var wmsLayer = L.tileLayer.wms(baseURL, {
                    layers: 'MyWorkSpace:GetCellLayer',
                    transparent: true,

Question is How and where can I update centerlat and centerlong in viewparams whenever user change zoom level of map ?

1 Answer 1


You just listen to the event that is fired on zoomend, as described at http://leafletjs.com/reference-1.2.0.html#map-event

From what you've described, you probably also need to listen for the moveend event, because that would also change your centre point and hence the query you want to make.

  • In this event , do I need to rebind the WMS layer to map.
    – M.Sharma
    Commented Sep 11, 2017 at 10:21
  • From what you've described, you're not changing the base map, but doing a separate postgis query.
    – BradHards
    Commented Sep 11, 2017 at 10:29
  • map.on('moveend',function(){ //CODE THAT i HAVE CALLED ABOVE IN QUESTION }) Are you saying to do this?
    – M.Sharma
    Commented Sep 11, 2017 at 11:08
  • Your implementation is up to you. The best way to do it depends on the rest of your code.
    – BradHards
    Commented Sep 11, 2017 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.