Question

Im trying to figure out how to set the minimum someone can zoom out. I dont want people to be able to zoom all the way out. Here is my code.

{

    OpenLayers.ImgPath = "http://js.mapbox.com/theme/dark/";
    var map, vectors, controls;
    //map = new OpenLayers.Map('map');
    //var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
    //"http://vmap0.tiles.osgeo.org/wms/vmap0?", {layers: 'basic'});

    vectors = new OpenLayers.Layer.Vector("Vector Layer");
    // Build the map
    var options = {
      projection: new OpenLayers.Projection("EPSG:900913"),
      displayProjection: new OpenLayers.Projection("EPSG:4326"),
      units: "m",
      numZoomLevels: 10,
      maxResolution: 156543.0339,
      maxExtent: new OpenLayers.Bounds(
        -20037500,
        -20037500,
        20037500,
        20037500
      )
    };
    map = new OpenLayers.Map('map', options);

    // Layer definitions
    var wms = new OpenLayers.Layer.TMS(
      "MapBox Layer",
      [ "http://mylayerurl.com/" ],
      { 'layername': 'luring', 'type': 'png' }
    );


    // Add layers to the map
    map.addLayers([wms, vectors]);
    map.addControl(new OpenLayers.Control.LayerSwitcher());
    map.addControl(new OpenLayers.Control.MousePosition());
    //map.addControl(new OpenLayers.Control.Click());

    map.events.register("click", map, function(e) {
      var position = map.getLonLatFromPixel(e.xy);
    });

    var markers = new OpenLayers.Layer.Markers( "Markers" );
    map.addLayer(markers);
    var size = new OpenLayers.Size(10,10);
    var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
    var icon = new OpenLayers.Icon('/themes/fairfood/images/dotmarker.png',size,offset);
    var markets = [];
    //alert(markets[0].field_longitude_value);

    // Create objects from our data.
    function addMarker(lng, lat, info) {
        var pt = new OpenLayers.LonLat(lng, lat).transform(new OpenLayers.Projection("EPSG:4326"),map.getProjectionObject());
            // Convert to google's coordinates(approx.)
            //- Marker build
            marker = new OpenLayers.Marker(new OpenLayers.LonLat(pt.lon,pt.lat),icon.clone());
            marker.id = j;
            marker.title = markets[j].title;
            marker.location = markets[j].field_address_value;
            marker.daysandtimes = markets[j].field_daysandtimes_value;
            markers.addMarker(marker);
            marker.events.register("mousedown", marker, function() {
            document.getElementById('markettitle').innerHTML = this.title;
            document.getElementById('location').innerHTML = this.location;
            document.getElementById('daysandtimes').innerHTML = this.daysandtimes;
        });
    }

    for(j=0;j<markets.length;j++) {
        addMarker(markets[j].field_longitude_value,markets[j].field_latitude_value);
    }

    // Set the starting point of the map.
    map.setCenter(new OpenLayers.LonLat(-9676648.014, 5283869.595), 6);


</script>

}

Was it helpful?

Solution

Zooming out can be limited at least by specifying available resolutions for the TMS layer:

var wms = new OpenLayers.Layer.TMS(
  "MapBox Layer",
  resolutions:    [
      360/(1<<(8+9)), 
      360/(1<<(8+10)),
      360/(1<<(8+11)),
      360/(1<<(8+12)),
      360/(1<<(8+13)),
      360/(1<<(8+14))
  ],
  [ "http://mylayerurl.com/" ],
  { 'layername': 'luring', 'type': 'png' }
);

This would restrict access to zoom levels other than 9-14. You probably also have to add a getURL function to your layer, and add the offset 9 to the zoom level passed in the URL, because otherwise it will still start at 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top