最近在用MapXtreme做项目,随笔记录备忘。

声明:PubMapPara 静态类,后缀为静态类成员变量

1.加载地图

/// <summary>
/// 地图工作空间文件路径
/// </summary>
public static string mapFileName = system.Environment.CurrentDirectory + @"\地图6\矿车定位.mws"; //加载不同类型的地图文件
public void LoadMap()
{
if (mapFileName.ToLower().EndsWith(MapLoader.FileExtensionGST))//判断加载地图文件类型
{
this.myMapControl.Map.Load(new MapGeosetLoader(mapFileName));//后缀名为gst文件
}
else if (mapFileName.ToLower().EndsWith(MapLoader.FileExtensionWOR))//后缀名为mws文件
{
this.myMapControl.Map.Load(new MapWorkSpaceLoader(mapFileName));
}
else
{
this.myMapControl.Map.Load(new MapTableLoader(mapFileName));
}
}

2.当前鼠标位置的二维坐标转成地图坐标

void myMapControl_MouseClick(object sender, MouseEventArgs e)
{
DPoint myPoint = new DPoint();
//浮点数的二维坐标
PointF mousePoint = new PointF(e.X, e.Y);
//获取当前地图可用于将地图和图层坐标转换成显示坐标以及相反的转换
DisplayTransform myDisplayTransform = this.myMapControl.Map.DisplayTransform;
//坐标转换
myDisplayTransform.FromDisplay(mousePoint, out myPoint);
}

3.设置图层可选、可编辑

FeatureLayer myFeaLayer = (FeatureLayer)this.myMapControl.Map.Layers[aliasName];
if(myFeaLayer != null)
{
LayerHelper.SetEditable(myFeaLayer, true);
LayerHelper.SetSelectable(myFeaLayer, true);
}

4.设置图层可视

FeatureLayer myFeaLayer = (FeatureLayer)this.myMapControl.Map.Layers[aliasName];
if (myFeaLayer != null)
{
myFeaLayer.Enabled = true;
}

5.控制地图的缩放范围

//地图视图发生变化
myMapControl.Map.ViewChangedEvent += new ViewChangedEventHandler(Map_ViewChangedEvent); /// <summary>
/// 地图视图发生变化
/// </summary>
void Map_ViewChangedEvent(object sender, ViewChangedEventArgs e)
{
//设置地图缩放比例
LimitZoomValue();
} /// <summary>
/// 限制地图缩放比例
/// 防止出现地图由于无限缩小无法找到地图的现象
/// </summary>
private void LimitZoomValue()
{
if (myMapControl.Map != null)
{
if (myMapControl.Map.Zoom.Value < PubMapPara.zoomMax)
{
myMapControl.Map.Zoom = new MapInfo.Geometry.Distance(PubMapPara.zoomMax, this.myMapControl.Map.Zoom.Unit);
}
if (myMapControl.Map.Zoom.Value > PubMapPara.zoomMin)
{
myMapControl.Map.Zoom = new MapInfo.Geometry.Distance(PubMapPara.zoomMin, this.myMapControl.Map.Zoom.Unit);
}
}
}
04-30 03:47