研究mapx一周了,总算会些基本操作了。
这是Form1.cs代码页面:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace mapx22
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
创建测量距离的工具
axMap1.CreateCustomTool(99, MapXLib.ToolTypeConstants.miToolTypePoly, MapXLib.CursorConstants.miArrowCursor, null, null, null);
创建测量面积的工具
axMap1.CreateCustomTool(98, MapXLib.ToolTypeConstants.miToolTypePoly,MapXLib.CursorConstants.miCrossCursor, null, null, null);
}
private void axMap1_PolyToolUsed(object sender, AxMapXLib.CMapXEvents_PolyToolUsedEvent e)
{
if (e.toolNum == 99)//测量距离
{
MapXLib.Points pts = (MapXLib.Points)e.points;
MapXLib.Point pt1, pt2;
double d = 0.0;
//计算顺序两个点距离,累计得到总距离
for (int i = 1; i < pts.Count; i++)
{
pt1 = pts._Item(i);
pt2 = pts._Item(i + 1);
d += axMap1.Distance(pt1.X, pt1.Y, pt2.X, pt2.Y);
}
this.label3.Text = d.ToString();
}
else if (e.toolNum == 98)//面积
{
MapXLib.Points pts = (MapXLib.Points)e.points;
//偷懒了但是很正确
MapXLib.FeatureFactory dd = axMap1.FeatureFactory;
MapXLib.Style style = axMap1.DefaultStyle;
this.label4.Text=dd.CreateRegion(pts, style).Area.ToString();
}
}
private void button1_Click(object sender, EventArgs e)
{
axMap1.CurrentTool = (MapXLib.ToolConstants)99;
}
private void button2_Click(object sender, EventArgs e)
{
axMap1.CurrentTool = (MapXLib.ToolConstants)98;
}
private void button3_Click(object sender, EventArgs e)
{
MapXLib.Layer lyr;
lyr = axMap1.Layers.CreateLayer("MyLayer", "e://set//MyLayer.Tab", 0, 32, axMap1.DisplayCoordSys);
}
private void button4_Click(object sender, EventArgs e)
{
MapXLib.LayerInfo li;
li = new MapXLib.LayerInfoClass();
li.Type = MapXLib.LayerInfoTypeConstants.miLayerInfoTypeTab;
li.AddParameter("FileSpec", "e://set//MyLayer.Tab");
li.AddParameter("Visible", false);
li.AddParameter("AutoCreateDataset", true);
li.AddParameter("DatasetName", "MyLayer");
axMap1.Layers.Add(li, 0);
axMap1.Layers.LayersDlg("", "");
axMap1.SaveMapAsGeoset("测试", "e://set//MyMap.GST");
}
private void button5_Click(object sender, EventArgs e)
{
int lyrind = 0;
axMap1.Layers.LayersDlg("", "");
for (int i = 1; i < axMap1.Layers.Count; i++)
{
if (axMap1.Layers[i]._Name.Trim() == "MyLayer")
{
lyrind = i;
break;
}
}
axMap1.Layers.Remove(lyrind);
axMap1.Layers.LayersDlg("", "");
}
private void button6_Click(object sender, EventArgs e)
{
MapXLib.LayerInfoClass li = new MapXLib.LayerInfoClass();
MapXLib.Features ftrs = null;
MapXLib.FieldsClass flds = new MapXLib.FieldsClass();
MapXLib.Fields Myflds = null;
MapXLib.Dataset dts = null;
flds.Add("State", "State_Name", MapXLib.AggregationFunctionConstants.miAggregationSum,MapXLib.FieldTypeConstants.miTypeString);
dts = axMap1.DataSets.Add(MapXLib.DatasetTypeConstants.miDataSetLayer, axMap1.Layers._Item(1), "MyLayer", 0, 0, 0, flds, false);
Myflds = dts.Fields;
ftrs = axMap1.Layers._Item("USA").Selection.Clone();
li.Type = MapXLib.LayerInfoTypeConstants.miLayerInfoTypeTemp;
li.AddParameter("Name", "USA Temp Layer");
li.AddParameter("Fields", Myflds);
li.AddParameter("Features", ftrs);
axMap1.Layers.Add(li, 1);
axMap1.Layers.LayersDlg("", "");
}
private void button7_Click(object sender, EventArgs e)
{
// axMap1.Bounds = axMap1.Layers._Item("USA").Bounds;
axMap1.CtlBounds = axMap1.Layers._Item("USA").Bounds;
}
private void button8_Click(object sender, EventArgs e)
{
axMap1.Layers._Item("USA Temp Layer").Editable = true;
axMap1.Layers.InsertionLayer = axMap1.Layers._Item("USA Temp Layer");
axMap1.CurrentTool = MapXLib.ToolConstants.miAddLineTool;
}
private void button9_Click(object sender, EventArgs e)
{
MapXLib.Style sty = new MapXLib.StyleClass();
MapXLib.Feature ftr = new MapXLib.FeatureClass();
ftr.Attach(axMap1.GetOcx());
ftr.Type = MapXLib.FeatureTypeConstants.miFeatureTypeText;
sty.TextFontColor = 255;
sty.TextFont.Size = 12;
ftr.Style = sty;
ftr.Caption = "New Feature";
ftr.Point.Set(axMap1.CenterX, axMap1.CenterY);
axMap1.Layers._Item("US Top 20 Cities").Style = sty;
ftr = axMap1.Layers._Item("US Top 20 Cities").AddFeature(ftr, new MapXLib.RowValuesClass());
ftr.Update(ftr, new MapXLib.RowValuesClass());
}
private void button10_Click(object sender, EventArgs e)
{
MapXLib.FindFeature fRes = null;
fRes = axMap1.Layers._Item("US Top 20 Cities").Find.Search("New York", "");
if (fRes.FindRC % 10 == 1)
{
axMap1.CenterX = fRes.CenterX;
axMap1.CenterY = fRes.CenterY;
axMap1.Layers._Item("US Top 20 Cities").Selection.Add(fRes);
}
}
private void button11_Click(object sender, EventArgs e)
{
axMap1.Layers._Item("US Top 20 Cities").UpdateFeature("New Feature",null,null);
// axMap1.Layers._Item("US Top 20 Cities").DeleteFeature("New Feature");
}
}
}