基于MapWinGis的开发探索(四)——图层窗口、地名检索的实现
此次开发将界面改成换成类型Office2007的,主要继承DevComponents。地图底层现在只添加一些地理图层如道路、河流、路名等,至于有实际应用的图层后期加入,这些ShapeFile文件从CAD图纸的dxf格式转换而来。
(一)图层窗口的实现
MapWinGis本身的图层窗口控件legend强大,源代码相当多,而且包含了很多方法,此次图层窗口的实现主要采用树形控件,将地理图层的图层ID、图层名加载绑定。
/// <summary>
/// 添加图层
/// </summary>
private void AddLayer()
{
//加载前清除树的节点
tvLayer.Nodes.Clear();
//获取所有图层
string[] files = MapCommon.LoadAllShpFile(Environment.CurrentDirectory + @"\mapfile");
if (files != null && files.Length > 0)
{
for (int i=0; i <files.Length ; i++)
{
Shapefile shpFile = new Shapefile();
bool bOpen= shpFile.Open(files[i], call);
int layerHandle= axMap1.AddLayer(shpFile, true);
DevComponents.AdvTree.Node node = new DevComponents.AdvTree.Node();
node.Text =LayerNames.En2Cn(MapCommon.GetFileName(files[i]));
node.Tag = layerHandle;
node.Image = Resources.Pic02;
tvLayer.Nodes.Add(node);
if (node.Text == LayerNames.RoadName_CN)
{//将路名的文本显示处理
ShowLable.GetLable(shpFile, layerHandle, axMap1);
}
}
map = axMap1;
}
//加载鼠标滚轮缩放事件
this.MouseWheel+=new MouseEventHandler(Map_MouseWheel);
}
/// 添加图层
/// </summary>
private void AddLayer()
{
//加载前清除树的节点
tvLayer.Nodes.Clear();
//获取所有图层
string[] files = MapCommon.LoadAllShpFile(Environment.CurrentDirectory + @"\mapfile");
if (files != null && files.Length > 0)
{
for (int i=0; i <files.Length ; i++)
{
Shapefile shpFile = new Shapefile();
bool bOpen= shpFile.Open(files[i], call);
int layerHandle= axMap1.AddLayer(shpFile, true);
DevComponents.AdvTree.Node node = new DevComponents.AdvTree.Node();
node.Text =LayerNames.En2Cn(MapCommon.GetFileName(files[i]));
node.Tag = layerHandle;
node.Image = Resources.Pic02;
tvLayer.Nodes.Add(node);
if (node.Text == LayerNames.RoadName_CN)
{//将路名的文本显示处理
ShowLable.GetLable(shpFile, layerHandle, axMap1);
}
}
map = axMap1;
}
//加载鼠标滚轮缩放事件
this.MouseWheel+=new MouseEventHandler(Map_MouseWheel);
}
(二)地名检索的实现
地名检索实现主要是遍历每个图层,寻找地名图层的dbf文件,遍历该文件的存放地名的那一列,将符合要求的地名检索出来,绑定在树形控件上。
/// <summary>
/// 模糊查找地名
/// </summary>
/// <param name="strWhere"></param>
private void Query(string strWhere)
{
//清空树节点
tvPlace.Nodes.Clear();
for (int i = 0; i < Map.NumLayers; i++)
{
MapWinGIS.Shapefile shpFile = (MapWinGIS.Shapefile)Map.get_GetObject(i);
if (System.IO.Path.GetFileNameWithoutExtension(shpFile.Filename) == LayerNames.RoadName)
{//获取路名的ShapeFile文件
for (int j = 0; j < shpFile.NumShapes; j++)
{
object obj = shpFile.get_CellValue(shpFile.NumFields - 1, j);
if (obj != null && obj.ToString().Contains(strWhere))
{//判断地名是否存在
Shape shp = shpFile.get_Shape(j);
DevComponents.AdvTree.Node tn = new DevComponents.AdvTree.Node();
tn.Text = obj.ToString();
tn.Tag = shp.SerializeToString();
tn.Image = Resources.Pic03;
tvPlace.Nodes.Add(tn);
}
}
}
}
}
/// 模糊查找地名
/// </summary>
/// <param name="strWhere"></param>
private void Query(string strWhere)
{
//清空树节点
tvPlace.Nodes.Clear();
for (int i = 0; i < Map.NumLayers; i++)
{
MapWinGIS.Shapefile shpFile = (MapWinGIS.Shapefile)Map.get_GetObject(i);
if (System.IO.Path.GetFileNameWithoutExtension(shpFile.Filename) == LayerNames.RoadName)
{//获取路名的ShapeFile文件
for (int j = 0; j < shpFile.NumShapes; j++)
{
object obj = shpFile.get_CellValue(shpFile.NumFields - 1, j);
if (obj != null && obj.ToString().Contains(strWhere))
{//判断地名是否存在
Shape shp = shpFile.get_Shape(j);
DevComponents.AdvTree.Node tn = new DevComponents.AdvTree.Node();
tn.Text = obj.ToString();
tn.Tag = shp.SerializeToString();
tn.Image = Resources.Pic03;
tvPlace.Nodes.Add(tn);
}
}
}
}
}
双击树形控件的节点,将地图定位在该地名上。这里主要涉及到地图定位算法的问题:先获取绑定在树形控件的Shape对象的属性,得到ShapeString属性,然后获得ShapeString里的Extents范围,增大Extents范围,将地图Extents范围设置成那个即可定位。
/// <summary>
/// 定位
/// </summary>
/// <param name="shapeString"></param>
public void GoTo(string shapeString)
{
Shape shape = new MapWinGIS.ShapeClass();
if (shape.CreateFromString(shapeString) == true)
{
SetMapBounds(shape.Extents.xMax, shape.Extents.yMax, shape.Extents.xMin, shape.Extents.yMin);
}
}
/// <summary>
/// 设置地图边界
/// </summary>
/// <param name="maxX"></param>
/// <param name="maxY"></param>
/// <param name="minX"></param>
/// <param name="minY"></param>
public void SetMapBounds(double maxX, double maxY, double minX, double minY)
{
maxX = maxX + 100;
minX = minX - 100;
maxY = maxY + 100;
minY = minY - 100;
MapWinGIS.Extents tExts = new Extents();
tExts.SetBounds(minX, minY, 0, maxX, maxY, 0);
GetMap().Extents = tExts;
}
/// 定位
/// </summary>
/// <param name="shapeString"></param>
public void GoTo(string shapeString)
{
Shape shape = new MapWinGIS.ShapeClass();
if (shape.CreateFromString(shapeString) == true)
{
SetMapBounds(shape.Extents.xMax, shape.Extents.yMax, shape.Extents.xMin, shape.Extents.yMin);
}
}
/// <summary>
/// 设置地图边界
/// </summary>
/// <param name="maxX"></param>
/// <param name="maxY"></param>
/// <param name="minX"></param>
/// <param name="minY"></param>
public void SetMapBounds(double maxX, double maxY, double minX, double minY)
{
maxX = maxX + 100;
minX = minX - 100;
maxY = maxY + 100;
minY = minY - 100;
MapWinGIS.Extents tExts = new Extents();
tExts.SetBounds(minX, minY, 0, maxX, maxY, 0);
GetMap().Extents = tExts;
}
定位的结果如下: