Mapxtreme使用心得

xiaoxiao2021-02-28  51

1、在工程中使用时提示无法找到dll,可能是由于添加引用的时候“复制到本地”是true,而vs没有c盘下对应目录的读取权限,就会出错。解决方法是将对应dll拷贝出来,或者是该设置项改为false。

2、InitializeComponent()提示dll加载有问题,是由于Mapxtreme的二进制与当前工程不兼容造成,我的Mapxtreme是x86的,工程的“目标平台”也要改为x86。

3、附上我写的两个地图操作类

using System; using System.Collections.Generic; using System.Text; using MapInfo.Engine; using MapInfo.Geometry; using MapInfo.Mapping; using MapInfo.Windows.Controls; using MapInfo.Windows.Dialogs; using System.Drawing; using System.Windows.Forms; using MapInfo.Data; using MapInfo.Styles; using System.Collections; namespace AUV_PROJECT { public enum MapState { Range,//测距 Move,//移动 Path,//路径规划 }; public class MapWrapper { MapOperation mo;//地图操作类 private double EARTH_RADIUS = 6378137;//赤道半径(单位m) private MapState state;//当前状态 private MapControl mc;//封装的地图控件 private List<DPoint> rangePoint = new List<DPoint>();//测距功能要用到的点,屏幕坐标 public MapWrapper(MapControl mapControl) { this.state = MapState.Move;//默认设置为移动,此时可以拖动地图 this.mc = mapControl; SetupMap(); mc.Map.Zoom = new Distance(10000, DistanceUnit.Meter);//设置默认比例尺 //在地图上创建临时绘图层 mo = new MapOperation(mc.Map); mo.CreatTmpTable(); mo.ClearFeature(); } public MapState State { get { return state; } set { state = value; } } private void SetupMap()//装载地图文件 { string s = @"D:\map\"; Session.Current.TableSearchPath.Path = s; string geoSetName = "中国海图.gst"; try { mc.Map.Load(new MapGeosetLoader(s + geoSetName)); } catch (Exception) { //MessageBox.Show("Geoset " + geoSetName + " not found."); } mc.Map.Center = new DPoint(121.71, 30.55); } public Distance Zoom { get { return mc.Map.Zoom; } set { mc.Map.Zoom = value; } } //点击按钮移动地图,通过调整地图中心实现 public void Move(double x, double y) { DPoint p = mc.Map.Center; p.x += x; p.y += y; mc.Map.Center = p; } public DPoint Screen2MapCoord(System.Drawing.Point screenP) { //将鼠标xy坐标转为mapX的地图经纬度坐标 System.Drawing.PointF DisplayPoint = new PointF(screenP.X, screenP.Y); MapInfo.Geometry.DPoint MapPoint = new MapInfo.Geometry.DPoint(); MapInfo.Geometry.DisplayTransform converter = this.mc.Map.DisplayTransform; converter.FromDisplay(DisplayPoint, out MapPoint); return MapPoint; } //添加测距点 public void AddRangePoint(System.Drawing.Point p) { DPoint tmp = Screen2MapCoord(p); rangePoint.Add(tmp); } public int GetRangePointCount() { return rangePoint.Count; } //绘制测距矢量线 public void DrawRangeVectorLine(System.Drawing.Point p) { if (rangePoint.Count > 0) { Graphics g = mc.CreateGraphics(); Pen rangePen = new Pen(new SolidBrush(Color.Red), 3); //绘制列表中最后一个点和传入点(当前鼠标位置) //g.DrawLine(rangePen, rangePoint[rangePoint.Count - 1], p); rangePen.Dispose(); } } //居中功能 public void DoCentral(DPoint p) { mc.Map.Center = p; } //居中功能 public void DoCentral(System.Drawing.Point p) { mc.Map.Center = Screen2MapCoord(p); } //获取测距总距离 public double GetTotleRange() { double range = 0; DPoint lastP = new DPoint(); for (int i = 0; i < rangePoint.Count; i++) { if (i == 0) { lastP = rangePoint[i]; continue; } range += Range(lastP.x, lastP.y, rangePoint[i].x, rangePoint[i].y); lastP = rangePoint[i]; } return range; } //测距功能(更改map临时图层) public void DoRange() { //mo.ClearFeature(); if (rangePoint.Count > 0)//最少需要1个点 { DPoint lastP = new DPoint(); for (int i = 0; i < rangePoint.Count; i++) { if (i == 0) { lastP = rangePoint[i]; mo.DrawPoint(lastP, 20, Color.FromArgb(0, 255, 0)); continue; } DPoint start = lastP; DPoint end = rangePoint[i]; mo.DrawLine(new DPoint[] { start, end });//绘制线段 //计算中点 DPoint middP = new DPoint((start.x + end.x) / 2, (start.y + end.y) / 2); //画端点 mo.DrawPoint(end, 10, Color.FromArgb(0,0,0)); //输出距离文字 double range = Range(start.x, start.y, end.x, end.y); //g.RotateTransform( (float)( deg(Math.Atan2(end.y - start.y, end.x - start.x)+90) ) ); mo.DrawText(range.ToString("F0") + "m", middP, 14); //g.ResetTransform(); lastP = rangePoint[i]; } } } //测距功能(gdi) public void DoRange2() { //if (rangePoint.Count > 1)//最少需要两个点 //{ // Graphics g = mc.CreateGraphics(); // Pen rangePen = new Pen(new SolidBrush(Color.Red), 3);//线段笔画 // Pen pointPen = new Pen(new SolidBrush(Color.Green), 3);//端点笔画 // System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);//距离文字字体 // SolidBrush drawBrush = new SolidBrush(System.Drawing.Color.Black);//距离文字画刷 // //g.DrawLines(rangePen, rangePoint.ToArray()); // System.Drawing.Point lastP = new System.Drawing.Point(); // for (int i = 0; i < rangePoint.Count; i++) // { // g.DrawEllipse(pointPen, rangePoint[i].X, rangePoint[i].Y, 5, 5);//绘制端点 // if (i == 0) // { // lastP = rangePoint[i]; // continue; // } // g.DrawLine(rangePen, lastP, rangePoint[i]);//绘制线段 // //计算中点 // System.Drawing.Point middP = new System.Drawing.Point((rangePoint[i].X + lastP.X) / 2, (rangePoint[i].Y + lastP.Y) / 2); // //输出距离文字 // DPoint start = Screen2MapCoord(lastP); // DPoint end = Screen2MapCoord(rangePoint[i]); // double range = Range(start.x, start.y, end.x, end.y); // //g.RotateTransform( (float)( deg(Math.Atan2(end.y - start.y, end.x - start.x)+90) ) ); // g.DrawString(range.ToString("F0") + "m", drawFont, drawBrush, middP); // //g.ResetTransform(); // lastP = rangePoint[i]; // } //} } //停止测距 public void StopRange() { state = MapState.Move; rangePoint.Clear(); mo.ClearFeature(); } /** * 转化为弧度(rad) * */ private double rad(double d) { return d * Math.PI / 180.0; } private double deg(double r) { return r * 180 / Math.PI; } /** * 基于余弦定理求两经纬度距离 * @param lon1 第一点的精度 * @param lat1 第一点的纬度 * @param lon2 第二点的精度 * @param lat3 第二点的纬度 * @return 返回的距离,单位km * */ private double Range(double lon1, double lat1, double lon2, double lat2) { double radLat1 = rad(lat1); double radLat2 = rad(lat2); double radLon1 = rad(lon1); double radLon2 = rad(lon2); if (radLat1 < 0) radLat1 = Math.PI / 2 + Math.Abs(radLat1);// south if (radLat1 > 0) radLat1 = Math.PI / 2 - Math.Abs(radLat1);// north if (radLon1 < 0) radLon1 = Math.PI * 2 - Math.Abs(radLon1);// west if (radLat2 < 0) radLat2 = Math.PI / 2 + Math.Abs(radLat2);// south if (radLat2 > 0) radLat2 = Math.PI / 2 - Math.Abs(radLat2);// north if (radLon2 < 0) radLon2 = Math.PI * 2 - Math.Abs(radLon2);// west double x1 = EARTH_RADIUS * Math.Cos(radLon1) * Math.Sin(radLat1); double y1 = EARTH_RADIUS * Math.Sin(radLon1) * Math.Sin(radLat1); double z1 = EARTH_RADIUS * Math.Cos(radLat1); double x2 = EARTH_RADIUS * Math.Cos(radLon2) * Math.Sin(radLat2); double y2 = EARTH_RADIUS * Math.Sin(radLon2) * Math.Sin(radLat2); double z2 = EARTH_RADIUS * Math.Cos(radLat2); double d = Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2)); //余弦定理求夹角 double theta = Math.Acos((EARTH_RADIUS * EARTH_RADIUS + EARTH_RADIUS * EARTH_RADIUS - d * d) / (2 * EARTH_RADIUS * EARTH_RADIUS)); double dist = theta * EARTH_RADIUS; return dist; } } } using System; using System.Collections.Generic; using System.Text; using MapInfo.Engine; using MapInfo.Geometry; using MapInfo.Mapping; using MapInfo.Windows.Controls; using MapInfo.Windows.Dialogs; using System.Drawing; using System.Windows.Forms; using MapInfo.Data; using MapInfo.Styles; using System.Collections; namespace AUV_PROJECT { class MapOperation { public MapInfo.Mapping.Map map; public Catalog Cat; public MapInfo.Data.Table tblTemp; private string tmpTableName = "temp"; public MapOperation(Map map) { this.map = map; Cat = MapInfo.Engine.Session.Current.Catalog; tblTemp = Cat.GetTable(tmpTableName); } //创建临时图层 public void CreatTmpTable() { TableInfoMemTable tblInfoTemp = new TableInfoMemTable(tmpTableName); //创建GPS终端小车集图层 if (tblTemp != null) //Table exists close it { Cat.CloseTable(tmpTableName); } tblInfoTemp.Columns.Add(ColumnFactory.CreateFeatureGeometryColumn(map.GetDisplayCoordSys())); tblInfoTemp.Columns.Add(ColumnFactory.CreateStyleColumn()); tblInfoTemp.Columns.Add(ColumnFactory.CreateStringColumn("Name", 40)); tblInfoTemp.Columns.Add(ColumnFactory.CreateStringColumn("Dept", 15)); tblInfoTemp.Columns.Add(ColumnFactory.CreateIntColumn("Level")); tblTemp = Cat.CreateTable(tblInfoTemp); FeatureLayer lyr = new FeatureLayer(tblTemp); map.Layers.Add(lyr); } private void Draw(FeatureGeometry geometry, Style style) { Feature f; FeatureLayer flayer; try { flayer = map.Layers[tmpTableName] as FeatureLayer; if (flayer == null) return; Table fTable = flayer.Table; fTable.BeginAccess(TableAccessMode.Write); f = new Feature(fTable.TableInfo.Columns); f.Geometry = geometry; f.Style = style; fTable.InsertFeature(f); fTable.EndAccess(); } catch (Exception ex) { //GlobalHelper.ShowError("绘制隐患图元错误,原因:" + ex.Message); } } //添加点 public void DrawPoint(DPoint p, double size, Color color) { MapInfo.Geometry.Point point = new MapInfo.Geometry.Point(map.GetDisplayCoordSys(), p); SimpleVectorPointStyle style = new SimpleVectorPointStyle(); style.PointSize = size; style.Color = color; Draw(point, style); } //添加文字 public void DrawText(string txt, DPoint start, double size) { //设置样式 // Create a SimpleLineStyle to be used as the callout line's style. MapInfo.Styles.SimpleLineStyle lineStyle = new MapInfo.Styles.SimpleLineStyle( new MapInfo.Styles.LineWidth(2.0, MapInfo.Styles.LineWidthUnit.Pixel)); // Construct a TextStyle with a SimpleLineStyle for the callout line. MapInfo.Styles.TextStyle textStyle = new MapInfo.Styles.TextStyle(new MapInfo.Styles.Font("arial", size), lineStyle); // TextStyle of the CalloutLine is also allowed to be null. textStyle.CalloutLine = null; //画文字 MapInfo.Geometry.DRect rect = new MapInfo.Geometry.DRect(start.x, start.y, start.x + 0.006, start.y + 0.006); MapInfo.Geometry.LegacyText ltxt = new MapInfo.Geometry.LegacyText(map.GetDisplayCoordSys(), rect, txt); Draw(ltxt, textStyle); } //添加线段 public void DrawLine(DPoint[] points) { SimpleLineStyle style = new SimpleLineStyle(); style.Color = Color.FromArgb(255, 0, 0); style.Width = new LineWidth(5, LineWidthUnit.Pixel); style.SetApplyAll(); //画线段 MultiCurve multiCurve = new MultiCurve(map.GetDisplayCoordSys(), CurveSegmentType.Linear, points); Draw(multiCurve, style); } //添加点 public void InsertFeature(string fileName, double x, double y, string GPS_NUMBER) { BitmapPointStyle bStyle = new BitmapPointStyle(fileName); //fileName格式为@"gpscar2_p2.bmp" 这个@代表C:\Program Files\Common Files\MapInfo\MapXtreme\6.7.1\CustSymb bStyle.PointSize = Convert.ToInt16(24); bStyle.NativeSize = true; bStyle.Attributes = StyleAttributes.PointAttributes.BaseAll; bStyle.SetApplyAll(); FeatureGeometry pt = new MapInfo.Geometry.Point(map.GetDisplayCoordSys(), new DPoint(y, x)) as FeatureGeometry; Feature ftr = new Feature(tblTemp.TableInfo.Columns); ftr.Geometry = pt; //图元地理位置设置 ftr.Style = bStyle; //图元为位图样式 ftr["Name"] = "aaaa"; ftr["Dept"] = GPS_NUMBER; //GPS终端号 ftr["Level"] = 2; tblTemp.InsertFeature(ftr); //插入图元 } //加图层标注 public void CreatMark() { MapInfo.Data.Table tblTemp = Cat.GetTable(tmpTableName); LabelSource labelSource = new LabelSource(tblTemp); //给所创建的临时表Animation中的图元加标注 //指定要标准字段所在的列 labelSource.DefaultLabelProperties.Caption = "Name"; //所要标注的列名 labelSource.DefaultLabelProperties.Layout.Offset = 8; //标注偏移 labelSource.DefaultLabelProperties.Layout.Alignment = MapInfo.Text.Alignment.TopRight;//标注对齐方式 labelSource.DefaultLabelProperties.Style.Font.BackColor = System.Drawing.Color.White; //字体背景 labelSource.DefaultLabelProperties.Style.Font.ForeColor = System.Drawing.Color.Red; //字体颜色 labelSource.DefaultLabelProperties.Style.Font.TextEffect = MapInfo.Styles.TextEffect.Box; //边缘效果 labelSource.DefaultLabelProperties.Style.Font.FontWeight = MapInfo.Styles.FontWeight.Bold; //粗体 MapInfo.Styles.SimpleLineStyle simpleLineStyle = new MapInfo.Styles.SimpleLineStyle(0); //标注注释线 labelSource.DefaultLabelProperties.Style.CalloutLine.ApplyStyle(simpleLineStyle); //取消标注注释线 LabelLayer labelLayer = new LabelLayer(); labelLayer.Name = "jcbz";//设置标注图层的名称 labelLayer.Sources.Append(labelSource);//往地图中加入该标注层 map.Layers.Add(labelLayer); } //清除临时图层中所有的图元 public void ClearFeature() { SearchInfo si; IResultSetFeatureCollection ifs; MapInfo.Data.Table Tm; Tm = Cat.GetTable(tmpTableName); if (Tm != null) //Table exists close it { Tm.BeginAccess(TableAccessMode.Write); si = MapInfo.Data.SearchInfoFactory.SearchWhere(""); ifs = MapInfo.Engine.Session.Current.Catalog.Search(tmpTableName, si); foreach (Feature ft in ifs) Tm.DeleteFeature(ft);//删除所有该图层上的图元 Tm.EndAccess(); } } } }

转载请注明原文地址: https://www.6miu.com/read-46519.html

最新回复(0)