mapinfo开发 >> MapXtreme开发基础教程三,MapXtreme常用代码
最后编辑时间:2021年07月23日 18:49:23【内容简介】MapXtreme基础教程三:MapXtreme加载地图,MapXtreme当前鼠标位置的二维坐标转成地图坐标,MapXtreme设置图层可选、可编辑,MapXtreme控制地图的缩放范围.这里罗列了MapXtreme常用代码示例。
1.MapXtreme加载地图(MapXtreme最常用代码)
public static string mapFileName = system.Environment.CurrentDirectory + @"mapscar.mws";
//MapXtreme加载各种类型地图文件
(这也是MapXtreme的最常用代码)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.MapXtreme当前鼠标位置的二维坐标转成地图坐标
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.MapXtreme设置图层可选、可编辑
FeatureLayer myFeaLayer = (FeatureLayer)this.myMapControl.Map.Layers[aliasName];
if(myFeaLayer != null)
{
LayerHelper.SetEditable(myFeaLayer, true);
LayerHelper.SetSelectable(myFeaLayer, true);
}
4.MapXtreme设置图层可视
FeatureLayer myFeaLayer = (FeatureLayer)this.myMapControl.Map.Layers[aliasName];
if (myFeaLayer != null)
{
myFeaLayer.Enabled = true;
}
5.创建内置MapInfo符号图标
SimpleVectorPointStyle vStyle = new SimpleVectorPointStyle();
vStyle.Code = 67;
vStyle.Color = Color.Red;
vStyle.PointSize = Convert.ToInt16(48);
vStyle.Attributes = StyleAttributes.PointAttributes.BaseAll;
vStyle.SetApplyAll();
6.创建自定义位图样式
//位图相对于位置C:Program FilesCommon FilesMapInfoMapXtreme.8.0CustSymb
string fileName = @"AMBU1-32.BMP";
BitmapPointStyle bStyle = new BitmapPointStyle(fileName);
bStyle.PointSize = Convert.ToInt16(24);
bStyle.NativeSize = true;
bStyle.Attributes = StyleAttributes.PointAttributes.BaseAll;
bStyle.SetApplyAll();
point = new DPoint(140, 55);
geoPoint = new MapInfo.Geometry.Point(myMap.GetDisplayCoordSys(), point);
feature = new Feature(geoPoint, bStyle);
key = fLyr.Table.InsertFeature(feature);
7.MapXtreme控制地图的缩放范围
//视图事件
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);
}
}
}
分享到: 豆瓣 新浪微博 百度贴吧 QQ空间 QQ好友
复制链接分享给好友或者自己收藏!这里会及时更新中。。。