关于vb中的经纬度转换控制坐标的函数frommappoint的剪辑

来源:互联网 发布:金庸小说主角知乎 编辑:程序博客网 时间:2024/03/29 05:36

Private Sub MapRectToPixels(r As MapObjects2.Rectangle, xMin As Integer, yMin As Integer, xMax As Integer, yMax As Integer) Dim p As New Point Dim xc As Single, yc As Single p.x = r.Left p.y = r.Top m_map.FromMapPoint p, xc, yc ' convert to pixels xMin = m_map.Parent.ScaleX(xc, vbTwips, vbPixels) yMin = m_map.Parent.ScaleY(yc, vbTwips, vbPixels) p.x = r.Right p.y = r.Bottom m_map.FromMapPoint p, xc, yc ' convert to pixels xMax = m_map.Parent.ScaleX(xc, vbTwips, vbPixels) yMax = m_map.Parent.ScaleY(yc, vbTwips, vbPixels) End Sub Private Sub DrawIconicMarker(pt As MapObjects.Point, pic As PictureBox, hdc As Long) Dim x As Single, y As Single mapMain.FromMapPoint pt, x, y Dim xPixels As Integer, yPixels As Integer xPixels = Me.ScaleX(x, vbTwips, vbPixels) xPixels = Me.ScaleX(y, vbTwips, vbPixels) BitBlt hdc, xPixels - 16, yPixels - 16, 32, 32, pic, hdc, 0, 0, SRCCOPY End Sub Point pointPixel = map.fromMapPoint(x2, y2); map.centerAt(pointPixel.getX(),pointPixel.getY()); /// /// 转换为 地图 。 /// ///////// private double ConvertPixelsToMapUnits(IActiveView pActiveView , double pixelUnits) { // Uses the ratio of the size of the map in pixels to map units to do the conversion IPoint p1 = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.UpperLeft; IPoint p2 = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.UpperRight; int x1, x2, y1, y2; pActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(p1, out x1, out y1); pActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(p2, out x2, out y2); double pixelExtent = x2 - x1; double realWorldDisplayExtent = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width; //转载请注明 arcengine吧 http://tieba.baidu.com/f?kw=arcengine double sizeOfOnePixel = realWorldDisplayExtent / pixelExtent; return pixelUnits * sizeOfOnePixel; } mo+c#中鹰眼的实现 //类 using System; namespace WindowsApplication68 { /// /// DragFeedback 的摘要说明。 /// public class DragFeedback { public DragFeedback() { // // TODO: 在此处添加构造函数逻辑 // } [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")] private static extern bool Rectangle(IntPtr hdc,int ulCornerX, int ulCornerY,int lrCornerX, int lrCornerY); [System.Runtime.InteropServices.DllImport("user32.dll") ] private static extern IntPtr GetDC(IntPtr hwnd); [System.Runtime.InteropServices.DllImport("gdi32.dll") ] private static extern int SetROP2(IntPtr hdc,int fnDrawMode); public MapObjects2.Rectangle dragfinish(float x,float y) { Rectangle (m_hdc,m_xmin,m_ymin,m_xmax,m_ymax); r = new MapObjects2.RectangleClass(); // return the rectangle p = new MapObjects2.PointClass(); p =m_map.ToMapPoint(m_xmin,m_ymin); r.Left =p.X ; r.Top =p.Y; p =m_map.ToMapPoint(m_xmax,m_ymax); r.Right =p.X ; r.Bottom =p.Y ; return r; } public void dragmove(float x,float y) { float xnext; float ynext; xnext=x; ynext=y; Rectangle(m_hdc,m_xmin,m_ymin,m_xmax,m_ymax); m_xmin =m_xmin + Convert.ToInt32 (xnext - m_xprev); m_xmax =m_xmax + Convert.ToInt32 (xnext - m_xprev); m_ymin =m_ymin + Convert.ToInt32 (ynext - m_yprev); m_ymax =m_ymax + Convert.ToInt32 (ynext - m_yprev); Rectangle(m_hdc,m_xmin,m_ymin,m_xmax,m_ymax); m_xprev =Convert.ToInt32(xnext); m_yprev =Convert.ToInt32(ynext); } public void dragstart(MapObjects2.IMoRectangle rect,AxMapObjects2.AxMap map,float x,float y) { float xmin,ymin; float xmax,ymax; m_map =map; // initialize the hwnd and hdc variables m_hwnd =m_map.Handle ; m_hdc =GetDC(m_map.Handle ); SetROP2(m_hdc,10); // raster op for inverting p =new MapObjects2.PointClass(); p.X =rect.Left; p.Y =rect.Top; xmin=0; ymin=0; xmax=0; ymax=0; m_map.FromMapPoint(p,ref xmin,ref ymin); p.X =rect.Right; p.Y =rect.Bottom; m_map.FromMapPoint(p,ref xmax,ref ymax); m_xmin =Convert.ToInt32 (xmin); m_ymin =Convert.ToInt32 (ymin); m_xmax =Convert.ToInt32 (xmax); m_ymax =Convert.ToInt32 (ymax); // draw the rectangle Rectangle(m_hdc,m_xmin,m_ymin,m_xmax,m_ymax); // remember the click position m_xprev =Convert.ToInt32 (x); m_yprev =Convert.ToInt32(y); } protected MapObjects2.Point p ; protected AxMapObjects2.AxMap m_map; protected System.IntPtr m_hdc; protected System.IntPtr m_hwnd; protected int m_xmin; protected int m_ymin; protected int m_xmax; protected int m_ymax; protected int m_xprev; protected int m_yprev; public MapObjects2.Rectangle r ; } } //程序主体 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace WindowsApplication68 { /// /// Form1 的摘要说明。 /// public class Form1 : System.Windows.Forms.Form { private AxMapObjects2.AxMap axMap1; private AxMapObjects2.AxMap axMap2; private System.Windows.Forms.Button button1; public DragFeedback DragFeedbk ; /// /// 必需的设计器变量。 /// private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// /// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.axMap1 = new AxMapObjects2.AxMap(); this.axMap2 = new AxMapObjects2.AxMap(); this.button1 = new System.Windows.Forms.Button(); ((System.ComponentModel.ISupportInitialize)(this.axMap1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.axMap2)).BeginInit(); this.SuspendLayout(); // // axMap1 // this.axMap1.Location = new System.Drawing.Point(16, 8); this.axMap1.Name = "axMap1"; this.axMap1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMap1.OcxState"))); this.axMap1.Size = new System.Drawing.Size(296, 336); this.axMap1.TabIndex = 0; this.axMap1.MouseDownEvent += new AxMapObjects2._DMapEvents_MouseDownEventHandler(this.axMap1_MouseDownEvent); this.axMap1.AfterLayerDraw += new AxMapObjects2._DMapEvents_AfterLayerDrawEventHandler(this.axMap1_AfterLayerDraw); this.axMap1.MouseUpEvent += new AxMapObjects2._DMapEvents_MouseUpEventHandler(this.axMap1_MouseUpEvent); // // axMap2 // this.axMap2.Location = new System.Drawing.Point(328, 24); this.axMap2.Name = "axMap2"; this.axMap2.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMap2.OcxState"))); this.axMap2.Size = new System.Drawing.Size(184, 176); this.axMap2.TabIndex = 1; this.axMap2.MouseDownEvent += new AxMapObjects2._DMapEvents_MouseDownEventHandler(this.axMap2_MouseDownEvent); this.axMap2.AfterTrackingLayerDraw += new AxMapObjects2._DMapEvents_AfterTrackingLayerDrawEventHandler(this.axMap2_AfterTrackingLayerDraw); this.axMap2.MouseMoveEvent += new AxMapObjects2._DMapEvents_MouseMoveEventHandler(this.axMap2_MouseMoveEvent); this.axMap2.MouseUpEvent += new AxMapObjects2._DMapEvents_MouseUpEventHandler(this.axMap2_MouseUpEvent); // // button1 // this.button1.Location = new System.Drawing.Point(376, 264); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(88, 32); this.button1.TabIndex = 2; this.button1.Text = "full "; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(528, 365); this.Controls.Add(this.button1); this.Controls.Add(this.axMap2); this.Controls.Add(this.axMap1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.axMap1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.axMap2)).EndInit(); this.ResumeLayout(false); } #endregion /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { MapObjects2.IMoDataConnection dc; MapObjects2.IMoMapLayer layer; dc=new MapObjects2.DataConnectionClass(); dc.Database=@"f:/data/usa"; if (dc.Connect() ) { layer=new MapObjects2.MapLayerClass(); layer.GeoDataset=dc.FindGeoDataset("STATES"); layer.Symbol.Color=Convert.ToUInt32(MapObjects2.ColorConstants.moPaleYellow) ; axMap1.Layers.Add(layer); layer=new MapObjects2.MapLayerClass(); layer.GeoDataset=dc.FindGeoDataset("ushigh"); layer.Symbol.Color=Convert.ToUInt32(MapObjects2.ColorConstants.moDarkGreen) ; axMap1.Layers.Add(layer); layer=new MapObjects2.MapLayerClass(); layer.GeoDataset=dc.FindGeoDataset("uslakes"); layer.Symbol.Color=Convert.ToUInt32(MapObjects2.ColorConstants.moBlue) ; axMap1.Layers.Add(layer); layer=new MapObjects2.MapLayerClass(); layer.GeoDataset=dc.FindGeoDataset("STATES"); layer.Symbol.Color=Convert.ToUInt32(MapObjects2.ColorConstants.moPaleYellow) ; axMap2.Layers.Add(layer); DragFeedbk =new DragFeedback(); } } private void button1_Click(object sender, System.EventArgs e) { axMap1.Extent=axMap1.FullExtent; } private void axMap1_MouseDownEvent(object sender, AxMapObjects2._DMapEvents_MouseDownEvent e) { MapObjects2.Rectangle r; r=new MapObjects2.RectangleClass(); if (e.button==1) { r=axMap1.TrackRectangle(); axMap1.Extent=r; } else { this.Cursor=Cursors.Hand ; axMap1.Pan(); } } private void axMap1_AfterLayerDraw(object sender, AxMapObjects2._DMapEvents_AfterLayerDrawEvent e) { if (e.index ==0) { axMap2.TrackingLayer.Refresh(true,0); } } private void axMap2_AfterTrackingLayerDraw(object sender, AxMapObjects2._DMapEvents_AfterTrackingLayerDrawEvent e) { MapObjects2.Symbol sym; sym=new MapObjects2.SymbolClass(); sym.OutlineColor=Convert.ToUInt32 (MapObjects2.ColorConstants.moRed); sym.Style =1 ; axMap2.DrawShape(axMap1.Extent,sym); } private void axMap2_MouseDownEvent(object sender, AxMapObjects2._DMapEvents_MouseDownEvent e) { MapObjects2.Point p; p=new MapObjects2.PointClass(); p=axMap2.ToMapPoint(e.x,e.y ); if (axMap1.Extent.IsPointIn(p)) { DragFeedbk .dragstart(axMap1.Extent ,axMap2,e.x,e.y ); } } private void axMap2_MouseMoveEvent(object sender, AxMapObjects2._DMapEvents_MouseMoveEvent e) { if (e.button==1) { DragFeedbk.dragmove(e.x,e.y ); } } private void axMap1_MouseUpEvent(object sender, AxMapObjects2._DMapEvents_MouseUpEvent e) { } private void axMap2_MouseUpEvent(object sender, AxMapObjects2._DMapEvents_MouseUpEvent e) { axMap2.CtlRefresh(); axMap1.Extent=DragFeedbk.dragfinish(e.x,e.y ); } } } 参见 From Map Point Method,To Map Distance Method,To Map Point Method. 范例 此例运用 From Map Distance 法将你在一地图上画的线的长度转为控制单位.试运行此例,将程序粘贴到一有空白区的文件的声明段,其包含一至少含有一张地图层的称为 Map1 的地图.按F5键并在地图上敲拉一条线. FromMapPoint Method 应用于 Map Object 说明 将在地图坐标系中一点转为控制空间坐标系中一点. 语法 object.FromMapDistance From Map Point 法语句有以下的目标限定语及自变量: 部分 说明 Object 要求的.为 Applies To 表列中一个目标赋值的目标表达式. Map Point 为一点赋值的一目标表达式. x Control Map Point 在控制坐标系中的X坐标. y Control Map Point 在控制坐标系中的Y坐标. 参见 From Map Distance 法, To Map Distance 法, To Map Point 法. 范例 下例运用 To Map Distance 法来举例说明如何得到地图上一点以控制单位表示的位置.试运行此例, 将程序粘贴到一有空白区的文件的声明段,其包含至少包含一 Map Layer 的称作 Map1 的一地图,然后按F5键并在地图上敲拉一个矩形. inXP Home + JDK1.4.0 + Eclipse 3.0 + ArcGIS Engine 9.0 原来提到Engine的java包中esriDisplay下的getDeviceFrame()导致整个Eclipse系统崩溃的问题,这个函数主要是需要将屏幕像素距离和地理坐标间距离相互转换时候用到,比如说处于编辑时需要snap的功能等。由于以上原因,不能直接采用getDeviceFrame()函数,可以通过以下的办法解决: //get the current visible bounds in real coordinates IEvelope pEnv = pDisplayTransformation.getVisibleBounds(); //get the lower left point IPoint pt = new Point(); pt.putCoords(pEnv.getXMin(), pEnv.getYMin()); //get the lower right point IPoint pt2 = new Point(); pt2.putCoords(pEnv.getXMax(), pEnv.getYMin()); //get the device point coordinates int xx = new int{1}; int yy = new int{1}; pDisplayTransformation.fromMapPoint(pt,xx,yy); int xx2 = new int{1}; int yy2 = new int{1}; pDisplayTransformation.fromMapPoint(pt2,xx2,yy2); //calculate the distance in device coordinate pixelExtent = xx2[0] - xx[0]; 然后利用visiblebounds的长度除以pixelExtent就可以得到每个pixel所代表的真实地理坐标大小,再乘以像素值就可以将屏幕像素距离转换为地理距离。 :) VisibleRegion Property Example This example uses the VisibleRegion change the shape of the map visible to the user. To try this example, paste the code into the Declarations section of a form containing a Map named Map1 containing at least one map layer, and then press F5. Click on the map to track a polygon. When you finish the polygon by double-clicking, the map should only be visible inside the polygon you just drew. Option Explicit Private Sub Map1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim poly As New MapObjects2.Polygon Set poly = Map1.TrackPolygon Dim polyPts As MapObjects2.Points Dim newPoly As New MapObjects2.Polygon Dim tempPt As New MapObjects2.Point Dim tempParts As New MapObjects2.Points Dim tempX As Single Dim tempY As Single For Each polyPts In poly.Parts Dim i As Integer For i = 0 To polyPts.Count - 1 Map1.FromMapPoint polyPts.Item(i), tempX, tempY tempPt.X = tempX tempPt.Y = tempY tempParts.Add tempPt Next i Next polyPts newPoly.Parts.Add tempParts Set Map1.VisibleRegion = newPoly End Sub 'x,y分别表示经度和纬度 Dim pt As MapObjects2.Point set pt=new mapobjects2.point Dim x, y As Double pt.x=30.5 pt.y=20.5 Map1.FromMapPoint pt, x, y Dim pt1 As new Point set pt1=new point pt1.x=x pt1.y=y Map1.TrackingLayer.AddEvent pt, 0 调试一下看看

原创粉丝点击