|
bokelund
Senior Member
   
121 Posts |
Posted - 07/21/2006 : 04:07:52
|
GpsTools 2.30 or later
GpsTools Guide # 3 - How to Use ZoomByBoundingBox
1. Introduction
This guide will attempt to show how you can use the
ZoomByBoundingBox so that you can
add a user-friendly zoom feature to your application.
The guide will show how to zoom a particular shape in a ShapeFile and
also
how to zoom a given area.
2. Getting Started
First we need to open and view a ShapeFile on a Map using
the MapShapeFile.
If you are not familiar with this, have a look at Guide #1 - How
to draw a shapefile with GpsTools
3. The ZoomByBoundingBox method
The ZoomByBoundingBox
lets you zoom an area of the Map so that the area is maximized
within the Map but without changing the proportions of the Map.
The Method has three overloads. One where the arguments are two points,
one where the arguments
are two positions and one where the arguments are the minimum and
maximum value in x- and y-coordinates .
You can choose whichever overload that suits best for your application
but they will all do the same thing.
4. Zoom a Shape
When working with ShapFile, it can sometimes be quite convenient to be
able to zoom a particular shape.
Let's say that you are showing a ShapeFile picturing the world and you
want to zoom in a particular country.
In this section, we will learn how to do just that.
What we want to do is for the user to click on a shape and then zoom
it. To do this we use the OnMouseDown
event on the Map. So we start by hooking up our event handler method
like this:
map1.OnMouseDown += new
GpsViewNET.OnMouseDown(map1_OnMouseDown);
|
The event
handler method is defined like this:
private void map1_OnMouseDown(MouseButtons Button, short Shift, GpsToolsNET.Position objPosition, GpsViewNET.Point objPoint)
|
As you can see, we get both the point and the position
where the user has clicked. We will use the point in this example.
The next step is to create copy of the ShapeFile as it is already being
used by the MapShapeFile object for drawing.
To get the shape the user has clicked on, we will use the SelectByIsInside
method. After that, we get
the selected shape by calling the Read
method. It looks like this in code:
// Get a copy of the used ShapeFile.
GpsShapeNET.ShapeFile shapeFile = mapShapeFile.ShapeFile.Copy();
// Select a position where we want search for a shape.
shapeFile.SelectByIsInside(objPosition);
// Read the shape if there is any at the given position.
GpsShapeNET.Shape shape = shapeFile.Read();
|
Note! It is not certain that we
get a shape from Read. Make sure to check.
Next is to get the coordinates of the selected shape. That is done like
this.
// Get the bounding box from the shapefile.
double dMinX, dMaxX, dMinY, dMaxY, dMinM, dMaxM, dMinZ, dMaxZ;
shape.GetBoundingBox(out dMinX, out dMaxX, out dMinY, out dMaxY, out dMinZ, out dMaxZ, out dMinM, out dMaxM);
|
We also need to specify which datum/grid the shape is defined in. We do
this by getting the DatumGridTemplate of the ShapFile:
// Get the DatumGrridTemplate form the shapefile
GpsToolsNET.Position template = shapeFile.DatumGridTemplate;
|
The final step is to call the ZoomByBoundingBox method:
// Zoom using the bouding box coodrinates.
map1.ZoomByBoundingBox(dMinX, dMaxX, dMinY, dMaxY, template);
map1.Update();
|
Note! Don't forget to call the
Update method. Otherwise nothing will happen.
5. Zoom
an Area Selected by a Rectangle
The example in this section is a bit more
advanced than the previouus one. However, the zoom it self is very
easy to do.
What we want to do is for the user to click on the map, hold down the
mouse button and drag the pointer
to a new location. When the user releases the mouse button, the
selected area should be zoom.
To do this we need to hook up to the follwoing events:
OnMouseDown, OnMouseUp, OnMouseMove and OnDraw on the Map object.:
map1.OnMouseDown += new GpsViewNET.OnMouseDown(map1_OnMouseDown);
map1.OnMouseUp += new GpsViewNET.OnMouseUp(map1_OnMouseUp);
map1.OnDraw += new GpsViewNET.OnDraw(map1_OnMapPaint);
map1.OnMouseMove += new GpsViewNET.OnMouseMove(map1_OnMouseMove);
|
When the user clicks on the first
position, we store it:
// This is the first
point the user clicked.
m_pointFirst = objPoint;
|
And when the
mouse point is being moved, we store the current position
(m_pointLast) and calls the Refresh method on the map.
if ( m_pointFirst != null )
{
m_pointLast = objPoint;
map1.Refresh();
}
|
When the Refresh method is called, the form calls the OnMapPaint
method. So this is where we put the code that
does the actual drawing of the rectangle.
System.Drawing.Pen pen = new System.Drawing.Pen(Color.BlueViolet, 2);
if (m_pointFirst != null && m_pointLast != null)
{
// Get Maps position (which is centered)
GpsToolsNET.Position mapPos = map1.Position;
// Maps center in pixels seen from the origin of the bitmap
// (not from the Control)
// This value changes when Map.Zoom or Map.Rotation changes
GpsViewNET.Point mapPoint = map1.Position2Point(mapPos);
// This is the offset that differs the Control's orgio
// and the raster maps (bitmaps) origin
int offset_x = mapPoint.X - map1.Width / 2;
int offset_y = mapPoint.Y - map1.Height / 2;
// Draw a rectangle
e.FillRectangle(new System.Drawing.SolidBrush(Color.FromArgb(128, Color.Blue)),
m_pointFirst.X - offset_x, m_pointFirst.Y - offset_y, m_pointLast.X - m_pointFirst.X,
m_pointLast.Y - m_pointFirst.Y);
}
|
And now to the actual zooming. We want
the map to start zooming when the mouse button has been released.
So this is done in the map1_OnMouseUp method.
// Check if we have a valid zoom if ( m_pointLast != null)
{
map1.ZoomByBoundingBox(m_pointFirst, m_pointLast);
map1.Update();
}
|
5. The Sample
All the code in this
guide is
demonstrated in
ShapFileViwer_ZoomGuide sample which can be downloaded for free.
Feedback
Feel free to comment this guide in this
topic. What was good and what was bad?
Resources
1. GpsTools SDK
(at least version 2.30).
2. A ESRI compatible shapefile.
3. The ShapeFileViewer_ZoomGuide
sample.Download here!
|
|