Mo在c中的使用.docx
《Mo在c中的使用.docx》由会员分享,可在线阅读,更多相关《Mo在c中的使用.docx(14页珍藏版)》请在冰豆网上搜索。
![Mo在c中的使用.docx](https://file1.bdocx.com/fileroot1/2023-1/23/dfd1063a-81f9-40a3-a2ad-e65bddb19796/dfd1063a-81f9-40a3-a2ad-e65bddb197961.gif)
Mo在c中的使用
1.将MapObject2.1的控件加入控件TabTools中
选择COMComponents=>MapObjects2.1MapControl,加进来后是AxMapObject2.AxMap控件
2.加载地图
MapObjects2.MapLayerlayer;//图层
MapObjects2.GeoDatasetgds;//图层对应的数据
MapObjects2.DataConnectionm_db=newMapObjects2.DataConnection();//图层集连接
//打开图层集连接:
设置.shp文件所在的目录路径
m_db.Database=IconApplication.Instance.GetMapPath();
m_db.Connect();
//查找图层:
sample1.shp地图(如果没有找到,则为空)
gds=m_db.FindGeoDataset("sample1");
//将图层加载到AxMapObject2.AxMap控件中=>axMap1
layer=newMapObjects2.MapLayer();
layer.GeoDataset=gds;
layer.Symbol.Color=ConvertColorToMapSymbolColor(Color.Red);//layer中的颜色与C#定义不同,使用了自定义的方法转换
layer.Symbol.Size=1;//图层线宽或点的粗细度
layer.Visible=true;//图层是否可视
axMap1.Layers.Add(layer);//这个方法实际上是Insert(0,layer),所以后面加载的地图在图层中的下标更小,显示级别更高(同区域,会由更高的级别覆盖低级别的图元)
//自定义转换颜色方法
privateUInt32ConvertColorToMapSymbolColor(Colorcolor)
{
returnConvert.ToUInt32(color.R+color.G*256+color.B*256*256);
}
3.各种操作
a.放大
//设置当前操作的图标
//(是为了后面MouseDownEvent事件区分当前操作是放大、缩小、平移、选择等。
//可以不设置,用其他变量记录也可以,主要是让操作人容易看清当前的操作状态)
axMap1.MousePointer=MapObjects2.MousePointerConstants.moZoomIn;
privatevoidaxMap1_MouseDownEvent(objectsender,AxMapObjects2._DMapEvents_MouseDownEvente)
{
switch(axMap1.MousePointer)
{
caseMapObjects2.MousePointerConstants.moZoomIn:
{
//选择放大到的区域
MapObjects2.Rectanglerect=axMap1.TrackRectangle();
if(rect==null||rect.Width<0.00005||rect.Height<0.00005)
{
//如果选择区域很小,可以认为是单击、点选。
就在当前范围下,放大1.5倍(1/1.5=0.6667)
rect=axMap1.Extent;
rect.ScaleRectangle(0.6667);
}
//当选择的区域设置为当前显示区域
axMap1.Extent=rect;
break;
}
case....
b.缩小
缩小与放大相似
axMap1.MousePointer=MapObjects2.MousePointerConstants.moZoomOut;
privatevoidaxMap1_MouseDownEvent(objectsender,AxMapObjects2._DMapEvents_MouseDownEvente)
{
switch(axMap1.MousePointer)
{
case...
caseMapObjects2.MousePointerConstants.moZoomOut:
{
MapObjects2.Rectanglerect=axMap1.TrackRectangle();
if(rect==null||rect.Width<0.00005||rect.Height<0.00005)
{
rect=axMap1.Extent;
rect.ScaleRectangle(1.5);//缩小为原来的2/3(1/(2/3)=3/2=1.5)
}
else
{
//计算缩小率
doubledRate=axMap1.Extent.Width/rect.Width*10;
rect.ScaleRectangle(dRate);
}
axMap1.Extent=rect;
break;
}
c.平移、拖动
axMap1.MousePointer=MapObjects2.MousePointerConstants.moPan;
privatevoidaxMap1_MouseDownEvent(objectsender,AxMapObjects2._DMapEvents_MouseDownEvente)
{
switch(axMap1.MousePointer)
{
case...
caseMapObjects2.MousePointerConstants.moPan:
{
axMap1.Pan();
break;
}
d.选择图元
axMap1.MousePointer=MapObjects2.MousePointerConstants.moDefault;
privatevoidaxMap1_MouseDownEvent(objectsender,AxMapObjects2._DMapEvents_MouseDownEvente)
{
switch(axMap1.MousePointer)
{
case...
caseMapObjects2.MousePointerConstants.moDefault:
{
MapObjects2.RectangletrackRect=axMap1.TrackRectangle();
//获取当前选择的图层(只在一层图层中查)
MapObjects2.MapLayerlayer=axMap.Layers.Item(currentLayerIndex)asMapObjects2.MapLayer;;
//获取的图元集合=>selectRecs
MapObjects2.RecordsetselectRecs;
//根据不同的图层类型(点、线、面),有不同的查找方式
switch(layer.shapeType)
{
caseMapObjects2.ShapeTypeConstants.moShapeTypePoint:
caseMapObjects2.ShapeTypeConstants.moShapeTypePolygon:
{
selectRecs=layer.SearchShape(trackRect,MapObjects2.SearchMethodConstants.moAreaIntersect,string.Empty);
break;
}
caseMapObjects2.ShapeTypeConstants.moShapeTypeLine:
default:
{
selectRecs=layer.SearchByDistance(trackRect,axMap1.ToMapDistance(1.5F),string.Empty);
break;
}
}
break;
}
e.获取图层属性字段列表
MapObjects2.TableDesctableDesc;
tableDesc=layer.Records.TableDesc;
for(shortk=0;k{
fieldname=tableDesc.get_FieldName(k);
}
e.通过图层属性数据查找数据
stringexpression;
field=layer.Records.Fields.Item(<地图属性的字段>);
switch(field.Type)
{
caseMapObjects2.FieldTypeConstants.moLong:
caseMapObjects2.FieldTypeConstants.moDouble:
{
expression=string.Format("{0}={1}",<地图属性的字段>,<属性值>);
break;
}
caseMapObjects2.FieldTypeConstants.moString:
{
expression=string.Format("{0}='{1}'",<地图属性的字段>,<属性值>);
break;
}
default:
{
expression=string.Empty;
break;
}
}
if(expression==string.Empty)
{
continue;
}
MapObjects2.Recordsetrecs=layer.SearchExpression(expression);
f.高亮显示选择的图元
privatevoidhighlightLayerRecordset(stringfieldName,MapObjects2.MapLayerlayer,MapObjects2.RecordsetselectRecs,intlayerIndex)
{
//MapObjects2.GroupRenderer是定义一组颜色相同的图元列表。
同一个图层允许有多个颜色显示定义组。
//这里是利用GroupRenderer定义一组高亮显示的图元列表
MapObjects2.GroupRenderergRen=(MapObjects2.GroupRenderer)layer.Renderer;
MapObjects2.GroupRendererrenColor;
if(gRen==null)
{
gRen=newMapObjects2.GroupRenderer();
gRen.DrawBackground=true;
layer.Renderer=gRen;
}
if(gRen.Count==0)
{
renColor=newMapObjects2.GroupRenderer();
gRen.Add(renColor);
}
else
{
renColor=(MapObjects2.GroupRenderer)gRen.get_Renderer(0);
}
//先清除原来选择的图元的选择颜色
for(shorti=Convert.ToInt16(renColor.Count-1);i>=0;i--)
{
renColor.Remove(i);
}
objectv;
intselectCount=selectRecs.Count;
//有时候MapObjects2.Recordset对象的Count的值是-1,但实际上有数据
if(selectCount<=0)
{
selectCount=0;
selectRecs.MoveFirst();
while(selectRecs.EOF==false)
{
selectCount++;
selectRecs.MoveNext();
}
}
MapObjects2.ValueMapRenderervmRen=newMapObjects2.ValueMapRenderer();
MapObjects2.Symbolsym;
vmRen.UseDefault=false;
vmRen.SymbolType=layer.Symbol.SymbolType;//与图层相同的样式类型:
点、线、面
vmRen.Field=fieldName
vmRen.ValueCount=Convert.ToInt16(selectCount);//初设图元个数
selectRecs.MoveFirst();
intindex=0;
while(selectRecs.EOF==false)
{
v=selectRecs.Fields.Item(fieldName).Value;
try
{
if(v.ToString()=="")
{
continue;
}
vmRen.set_Value(Convert.ToInt16(index),v.ToString());
sym=vmRen.get_Symbol(Convert.ToInt16(index));
sym.Size=layer.Symbol.Size;
sym.Color=ConvertColorToMapSymbolColor(Color.Yellow);//设置为黄色
index++;
}
finally
{
selectRecs.MoveNext();
}
}
//有时候,在上面循环中会将一些不需要的图元排除,这里可以再次重设
vmRen.ValueCount=Convert.ToInt16(index);
if(vmRen.ValueCount>0)
{
renColor.Add(vmRen);
}
axMap1.RefreshLayer(Convert.ToInt16(layerIndex));
}
h.闪烁图元3秒
///
///闪烁图元3秒
///
///图元列表
///是否自动将图元的区域移到中心区域位置显示
privatevoidFlashShape(MapObjects2.MapLayerlayer,ArrayListsymbols,booltoCenter)
{
MapObjects2.RectangleshapeExtent=null;
objectshape=null;
shape=symbols[0];
switch(layer.shapeType)
{
caseMapObjects2.ShapeTypeConstants.moShapeTypePolygon:
{
#region//将面型(多边形)图元合并为一个面型图元
MapObjects2.Polygonp;
p=shapeasMapObjects2.Polygon;
for(inti=1;i{
p=p.Union(symbols[i],axMap1.FullExtent)asMapObjects2.Polygon;
}
shape=p;
//得到合并后的新图元的区域,是为了后面将当前新图元居中显示用
shapeExtent=p.Extent;
break;
#endregion
}
caseMapObjects2.ShapeTypeConstants.moShapeTypeLine:
{
#region//合并线性图元
MapObjects2.LineL;
L=shapeasMapObjects2.Line;
for(inti=1;i{
L=L.Union(symbols[i],axMap1.FullExtent)asMapObjects2.Line;
}
shape=L;
shapeExtent=L.Extent;
break;
#endregion
}
caseMapObjects2.ShapeTypeConstants.moShapeTypePoint:
default:
{
MapObjects2.Pointsp;
p=shapeasMapObjects2.Points;
//点没有宽度,所以将设为全图的2%大小
shapeExtent=newMapObjects2.Rectangle();
shapeExtent.Left=p.Extent.Center.X-axMap1.FullExtent.Width/100;
shapeExtent.Right=p.Extent.Center.X+axMap1.FullExtent.Width/100;
shapeExtent.Top=p.Extent.Center.Y+axMap1.FullExtent.Height/100;
shapeExtent.Bottom=p.Extent.Center.Y-axMap1.FullExtent.Height/100;
break;
}
}
if(shape!
=null)
{
if(toCenter)
{
//放大至少达到当前范围的5%
MapObjects2.RectanglerectExtent=axMap1.Extent;
doublefactor,factorX,factorY;
factorX=(shapeExtent.Width)*20/(rectExtent.Width);
factorY=(shapeExtent.Height)*20/(rectExtent.Height);
factor=(factorX>factorY)?
factorX:
factorY;
if(factor<1&&factor>0)
{
rectExtent.ScaleRectangle(factor);
}
//居中
doubledeltaX,deltaY;
deltaX=(shapeExtent.Center.X)-(rectExtent.Center.X);
deltaY=(shapeExtent.Center.Y)-(rectExtent.Center.Y);
if(rectExtent.IsPointIn(shapeExtent.Center)==false)
{
rectExtent.Offset(deltaX,deltaY);
}
axMap1.Extent=rectExtent;
axMap1.Refresh();
}
//合并的图元闪烁3秒
axMap1.FlashShape(shape,3);
}
}