Pregunta

I want to set a minimum and a maximum zoom level in my map.

My first idea was to listen to 'zoomstart' events, but the org.gwtopenmaps.openlayers.client.Map class doesn't implement any listener with such event type. Then I tried to listen to 'zoomend' events. My idea was to check the zoomlevel after the zoom event and if it is higher/lower than a threshold value than i zoom to that threshold value. Example code:

    @Override
    public void onMapZoom(MapZoomEvent eventObject) {
        if (eventObject.getSource().getZoom() > 18) {
            eventObject.getSource().zoomTo(18);
        }
    }

But i found, the zoomTo event doesn't fire in this case. Has anybody got a solution to this problem?

¿Fue útil?

Solución

Great idea Imreking.

I have added this to the GWT-Openlayers library. So if you download the latest version from github now you can do :

map.setMinMaxZoomLevel(6, 8);

And you no longer need some javascript method in your own code.

I actually also added a showcase but having difficulties uploading it to our website.

Uploading the new showcase has now succeeded. See http://demo.gwt-openlayers.org/gwt_ol_showcase/GwtOpenLayersShowcase.html?example=Min%20max%20zoom%20example to see an example of newly added Map.setMinMaxZoomLevel(minZoom, maxZoom).

Otros consejos

I don't think this is possible in OpenLayers (normal and GWT). According to me two solutions are available.

Option 1 This is ugly for the user. As he sees the map getting zoomed, and just after this going back to the previous zoomlevel. The Timer is needed to give OL the chance to animate the zoom.

    map.addMapZoomListener(new MapZoomListener()
    {
        @Override
        public void onMapZoom(final MapZoomEvent eventObject)
        {
            Timer t = new Timer()
            {

                @Override
                public void run()
                {
                    if (eventObject.getSource().getZoom() > 15)
                    {
                      map.zoomTo(15);
                    }
                    else if (eventObject.getSource().getZoom() < 10)
                    {
                        map.zoomTo(10);
                    }
                }
            };
            t.schedule(500);
        }
    });

Option 2 Don't use the zoom default zoom control but create your own zoom buttons (using normal GWT), and putting these on top of the map. If you want you can style these buttons in the same way as the normal buttons. The trick 'create in normal GWT, and make it look like OL' is a trick I use a lot (for example to create a much more advanced layer switcher).

Note : I am one of the developers of GWT-OpenLayers, if you want I can add an example to our showcase displaying how to do 'Option 2'.

Knarf, thank you for your reply. I tried the 'Option 1' and it worked, but i found another solution which is maybe more acceptable for the users.

My solution is:

    map.isValidZoomLevel = function(zoomLevel) {
       return ((zoomLevel != null) &&
          (zoomLevel >= minZoomLevel) && 
          (zoomLevel <= maxZoomLevel) &&
          (zoomLevel < this.getNumZoomLevels()));
    }

I overrode the isValidZoomLevel method. The minZoomLevel and maxZoomLevel variables were set when the application started. I don't like calling javascript from GWT code, but here i didn't have any other opportunity.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top