3

I have a BBOX strategy in my Vector layer, which is sending HTTP GET Requests to my server in order to get the features I wish to display. The projection of the layer is EPSG:4326, so it is my understanding that the bounding box should contain the coordinates of the lower left corner and the upper right corner in longitude/latitude format.

It appears that the bounding box extends beyond the current view of the map on all sides. This makes sense as a way to allow for small lateral shifts of the map view without the need for a new data request. However, for some zoom levels I am also getting numbers outside of the range of -180 to 180 degrees for longitude and -90 to 90 degrees for latitude (e.g., -453). Am I correct in assuming that these still represent degrees, and that I should just compute their value modulo 360 or 180, etc?

I just want to make sure that I am understanding what OpenLayers is providing for the BBOX, since I have so far been unable to find it written down anywhere.

2 Answers 2

1

I think this is what you want to do:

var fromProjection = new OpenLayers.Projection("EPSG:4326");   
var toProjection   = new OpenLayers.Projection("EPSG:900913"); 

map = new OpenLayers.Map( 'map',  {units: "km"} );
map.addLayer(new OpenLayers.Layer.OSM("Main Map"));

function loadCities() {
    var mapbounds = map.getExtent();
    mapbounds.transform(toProjection, fromProjection );
    bbox = mapbounds.toArray();
    var bleft = bbox[0];
    var bbottom = bbox[1];
    var bright = bbox[2];
    var btop = bbox[3];
    showLoaderDiv();
    var srcURL = "getCities?bleft=" + bleft + "&bbottom=" + bbottom + "&bright=" + bright + "&btop=" + btop;
    $.ajax({
            url: srcURL,
            dataType: "json"
    }).always(function() { hideLoaderDiv(); }).done(function(data) {
        data = eval( data );
        var geojson_format = new OpenLayers.Format.GeoJSON({
            'internalProjection': map.baseLayer.projection,
            'externalProjection': new OpenLayers.Projection("EPSG:4326")
        });
        citiesLayer.removeAllFeatures();
        citiesLayer.addFeatures(geojson_format.read(data));
    });     
}

The code

  var mapbounds = map.getExtent();
  mapbounds.transform(toProjection, fromProjection );

Gets the map viewport extent and makes a transform. Be sure you have the correct map and data projection in your bbox. Try to Alert() my bbox in your map to see if its all correct. Maybe you want to play a little with my code and compare the results.

Please note my map is a OSM map in Google projection (900913) and my data (4326) needs to be transformed. Pay attention in your map projection too. In same way, queries to database (so the bbox) needs to be transformed back to 4326 before do the search in database.

1
  • Oops, I forgot to say : You will need JQuery to try the ajax part of my code.
    – Magno C
    Jan 8, 2013 at 14:24
1

By default, the bbox strategy requests twice the bounds of current map view hence it will not be the lower left corner and upper right corner values. As of getting values more than 90 or 360, it may not have handled the bounds getting out of normal possible values. you should try the ratio property of BBOX strategy to understand more. when ratio is set to 1, the bounds will be the upper right ans lower left corner of the current map.

Your Answer

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

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