Maintain extents when changing maps

From Ka-Map Wiki

Jump to: navigation, search

By default, kaMap defaults to the extents of the map as defined in the map file. This behaviour works if the maps are not related geographically. However, in many cases it is desirable to have many maps of the same geographic area and allow the user to switch between them without changing the current user extents.

There are two ways of doing this. First, you can combine your maps into a single map file and separate the layers into GROUPs. The user interface for this would simply turn groups on and off in the interface (similar to the existing layer control). The second approach is using multiple map files. In this approach, you need to call the kaMap.selectMap() function with the name (from config.php) of the new map file. You can pass a second argument to selectMap which is an array or three or four values. If you pass three values, then it assumes this is the center point and scale to automatically zoom to. If you pass four values, then it assumes this is the bounding box to zoom to.

The most reliable way of doing this would be to pass the center point and scale rather than the extents because rounding issues can cause the map to zoom out one scale level (it tries to fit the extents inside view).

Here is a sample function that does the necessary tricks:

function mySwitchMap(map) {
  var extents = myKaMap.getGeoExtents();
  var cx = (extents[2] + extents[0])/2;
  var cy = (extents[3] + extents[1])/2;
  var scale = myKaMap.getCurrentScale();
  myKaMap.selectMap(map, [cx, cy, scale]);
}
Personal tools