1)移动地图代码
axMapControl1.Pan();
2)拉框放大地图代码
axMapControl1.Extent = axMapControl1.TrackRectangle();
3)拉框缩小地图代码
double newMapExtentWidth;
double newMapExtentHeight;
double newMapExtentXmin;
double newMapExtentYmin;
double newMapExtentXmax;
double newMapExtentYmax;
double zoomInWidthFactor;
double zoomInHeightFactor;
IEnvelope pDrawRectangle;
IEnvelope pNewEnvelope;
IEnvelope pCurrentExtent;
pDrawRectangle = axMapControl1.TrackRectangle();
pNewEnvelope = new EnvelopeClass();
pCurrentExtent = axMapControl1.Extent;
if (pDrawRectangle.IsEmpty ==false)
{
if ((pDrawRectangle.Width != 0) &&(pDrawRectangle.Height!=0))
{
zoomInWidthFactor = pCurrentExtent.Width / pDrawRectangle.Width;
zoomInHeightFactor = pCurrentExtent.Height / pDrawRectangle.Height;
newMapExtentWidth = pCurrentExtent.Width * zoomInWidthFactor;
newMapExtentHeight = pCurrentExtent.Height * zoomInHeightFactor;
newMapExtentXmin = pCurrentExtent.XMin - (pDrawRectangle.XMin - pCurrentExtent.XMin) * zoomInWidthFactor;
newMapExtentYmin = pCurrentExtent.YMin - (pDrawRectangle.YMin - pCurrentExtent.YMin) *zoomInHeightFactor;
newMapExtentXmax = newMapExtentXmin + newMapExtentWidth;
newMapExtentYmax = newMapExtentYmin + newMapExtentHeight;
pNewEnvelope.PutCoords(newMapExtentXmin, newMapExtentYmin, newMapExtentXmax, newMapExtentYmax);
}
}
axMapControl1.Extent = pNewEnvelope;
4)中心放大地图代码
IEnvelope pEnvelope;
pEnvelope = axMapControl1.Extent;
pEnvelope.Expand(0.5, 0.5, true);
axMapControl1.Extent = pEnvelope;
5)中心缩小地图代码
IEnvelope pEnvelope;
pEnvelope = axMapControl1.Extent;
pEnvelope.Expand(2, 2, true);
axMapControl1.Extent = pEnvelope;
6)全图显示
axMapControl1.Extent = axMapControl1.FullExtent;
7)历史视图切换代码
①前一视图代码
if(axMapControl1.ActiveView.ExtentStack.CanUndo())
{
axMapControl1.ActiveView.ExtentStack.Undo();
}
②后一视图代码
if(axMapControl1.ActiveView.ExtentStack.CanRedo())
{
axMapControl1.ActiveView.ExtentStack.Redo();
}
移动地图和放大缩小会遇到从按钮开始的情况采用以下的办法解决
int x = 0;
private void fun()
{
if (x == 3)
{
axMapControl1.Extent = axMapControl1.TrackRectangle();
}
if (x == 2)
{
axMapControl1.Pan();
}
if (x == 5)
{
double newMapExtentWidth;
double newMapExtentHeight;
double newMapExtentXmin;
double newMapExtentYmin;
double newMapExtentXmax;
double newMapExtentYmax;
double zoomInWidthFactor;
double zoomInHeightFactor;
IEnvelope pDrawRectangle;
IEnvelope pNewEnvelope;
IEnvelope pCurrentExtent;
pDrawRectangle = axMapControl1.TrackRectangle();
pNewEnvelope = new EnvelopeClass();
pCurrentExtent = axMapControl1.Extent;
if (pDrawRectangle.IsEmpty == false)
{
if ((pDrawRectangle.Width != 0) && (pDrawRectangle.Height != 0))
{
zoomInWidthFactor = pCurrentExtent.Width / pDrawRectangle.Width;
zoomInHeightFactor = pCurrentExtent.Height / pDrawRectangle.Height;
newMapExtentWidth = pCurrentExtent.Width * zoomInWidthFactor;
newMapExtentHeight = pCurrentExtent.Height * zoomInHeightFactor;
newMapExtentXmin = pCurrentExtent.XMin - (pDrawRectangle.XMin - pCurrentExtent.XMin) * zoomInWidthFactor;
newMapExtentYmin = pCurrentExtent.YMin - (pDrawRectangle.YMin - pCurrentExtent.YMin) * zoomInHeightFactor;
newMapExtentXmax = newMapExtentXmin + newMapExtentWidth;
newMapExtentYmax = newMapExtentYmin + newMapExtentHeight;
pNewEnvelope.PutCoords(newMapExtentXmin, newMapExtentYmin, newMapExtentXmax, newMapExtentYmax);
}
}
axMapControl1.Extent = pNewEnvelope;
}
}
private void button2_Click(object sender, EventArgs e)
{
x = 3;
}
private void button3_Click(object sender, EventArgs e)
{
x = 5;