Mapscript Demo (3)
![]()
The final portion of this project involves adding some map functionality to our program. As a quick review, we first setup Mapserver on our computer and then created a simple desktop application (part 1, part 2) that used the Mapscript API.
Zooming
The main object in our program is Mapscript’s map object (mapObj). This object has a method called zoomPoint that we can use to zoom in or out. The method looks like this:
zoomPoint(int zoomfactor, pointObj imgpoint,
int width, int height, rectObj extent, rectObj maxextent)
In our program, we simplify things by only allowing the user to zoom in or out on the center of the map. A single zoom function can be used for both zooming in and out:
public void ZoomMap(int zoomFactor)
{
m_map.zoomPoint(zoomFactor,
new pointObj(picMap.Width / 2, picMap.Height / 2, 0, 0),
picMap.Width, picMap.Height, m_map.extent, null);
RefreshMap();
}
The zoomFactor value depends on the action. Values greater than 1 zoom in, values less than 1 zoom out. In our case, clicking on the Zoom In fires this code:
ZoomMap(2);
while zooming out is the opposite:
ZoomMap(-2);
Panning
Panning the map is very similar to zooming except that this is controlled by clicking on the map. In fact, we can use the same zoomPoint method as we discussed above. The difference being when the zoomFactor is 1, the map remains at the same scale and only changes location.
Capturing a mouse click on the map (which is really our picture box control) is as simple this:
private void picMap_MouseClick(object sender, MouseEventArgs e)
{
//--> pan map based on mouse click coordinates
m_map.zoomPoint(1,
new pointObj((double)e.X, (double)e.Y, 0, 0),
picMap.Width, picMap.Height, m_map.extent, null);
RefreshMap();
}
Conclusion
The source code is available to download below so feel free to test out the application as long as you have a valid map file and data available. There is still a lot of room for improvement on this application, including more functionality and error checking, but we need to move on to bigger and better projects. The primary purpose of this project was to demonstrate how Mapserver (via Mapscript) could be used to generate maps for a desktop mapping application. Our next demonstration project involves improving the visual aspects of our program in a way that hasn’t been done before. Stay tuned.
