为了使用ArcMobile实现量测功能,LZ自定义了一个MapGraphicLayer用于绘图,代码如下:

using System.Drawing;
using ESRI.ArcGIS.Mobile;
using ESRI.ArcGIS.Mobile.Geometries; namespace LandInspections
{
public class MeasureLayer : MapGraphicLayer
{
/// Defines a symbol class instance for displaying point
private Symbol drawSymbol; /// Defines a CoordinateCollection instance to store custom features
private CoordinateCollection coordinateCollection = new CoordinateCollection(); /// Get or set coordinate collection stored in custom graphic layer
public CoordinateCollection Coordinates
{
get
{
return coordinateCollection;
}
set
{
coordinateCollection = value;
}
} /// Initializes a new instance of custom graphic layer
public MeasureLayer()
: base("MeasureLayer")
{
drawSymbol = new Symbol(
new PointPaintOperation(
Color.Green, 3, 0, Color.LightGreen, 100, 25, PointPaintStyle.Circle));
} /// Draw method being called after adding to map
protected override void Draw(Display display)
{
//return if display is null
if (display == null)
return;
//return if drawing is cancelled
if (display.DrawingCanceled)
return;
drawSymbol.DrawArea(display, coordinateCollection);
} protected override void Dispose(bool disposing)
{
//Dispose symbol implementing IDisposible
try
{
if (disposing)
{
if (drawSymbol != null)
drawSymbol.Dispose();
drawSymbol = null; coordinateCollection = null;
}
}
finally
{
base.Dispose(disposing);
}
}
}
}

  在“量测”按钮所在的窗体代码中,添加如下代码:

/// <summary>
/// MapGraphicLayer的坐标点
/// </summary>
private CoordinateCollection coords = new CoordinateCollection(); private BtnMeasure_Click(object sender, EventArgs e)
{
if (this.txtMeasure.Visible)//txtMeasure是用来显示量测结果的控件
{
this.txtMeasure.Visible = false;
this.coords.Clear();
this.map1.MapGraphicLayers.Remove(this.measureLayer);
this._mapAction = MyMapAction.None;
}
else
{
this.txtMeasure.Text = "量测:\r\n长度:0.000 米\r\n面积:0.000 平方米";
this.measureLayer = new MeasureLayer();
this.measureLayer.Coordinates = this.coords;
this.map1.MapGraphicLayers.Add(this.measureLayer);
this._mapAction == MyMapAction.Measure;
this.txtMeasure.Visible = true;
}
} private void map1_MouseUp(object sender, MapMouseEventArgs e)
{
if (this._mapAction == MyMapAction.Measure)
{
Coordinate coord = this.map1.ToMap(e.X, e.Y);
this.coords.Add(coord);
Polyline line = new Polyline(this.measureLayer.Coordinates);
Polygon area = new Polygon(this.measureLayer.Coordinates);
this.txtMeasure.Text = String.Format("量测:\r\n长度:{0} 米\r\n面积:{1} 平方米",
line.GetLength().ToString("f3"), area.GetArea().ToString("f3"));
this.map1.Refresh();
}
}

  运行后,发现顺时针点击屏幕的绘图结果正确,但是逆时针点击屏幕时绘图结果错误。

  检查后发现是因为ESRI.ArcGIS.Mobile.Geometries.CoordinateCollection类的Add方法对点集的顺序进行了自动调整,在this.coords.Add(coord)时点集的顺序已经被改变了。

  添加点1:CoordinateCollection中点集为[1]。

  添加点2:CoordinateCollection中点集为[1,2,1]。

  添加点3:CoordinateCollection中点集为[1,3,2,1]。

  添加点4:CoordinateCollection中点击为[1,4,2,3,1]。

  为解决这一问题,LZ在程序中定义一个List<Coordinate> coords,用来存储正确顺序的点集,在自定义的MapGraphicLayer中添加一个SetCoordinate方法,用来保证以顺时针方向添加节点到CoordinateCollection中。代码如下:

public void SetCoordinate(System.Collections.Generic.List<Coordinate> coords)
{
if (coords == null || coords.Count < 1)
return;
if (this.coordinateCollection == null)
this.coordinateCollection = new CoordinateCollection();
  this.coordinateCollection.Clear();
//若为顺时针,则直接赋值
for (int i = 0; i < coords.Count; i++)
{
this.coordinateCollection.Add(new Coordinate(coords[i].X, coords[i].Y));
if (this.coordinateCollection.IsCounterClockwise == true)
break;
}
//若为逆时针,则反向以顺时针方式将点集添加到CoordinateCollection中
if (this.coordinateCollection.IsCounterClockwise == true)
{
this.coordinateCollection.Clear();
this.coordinateCollection.Add(new Coordinate(coords[0].X, coords[0].Y));
for (int i = coords.Count - 1; i > 0; i--)
{
this.coordinateCollection.Add(new Coordinate(coords[i].X, coords[i].Y));
}
}
}

  相应的,“量测”功能所在窗体的代码也需要进行相应的修改,代码如下:

/// <summary>
/// MapGraphicLayer的坐标点
/// </summary>
private List<Coordinate> coords = new List<Coordinate>(); private BtnMeasure_Click(object sender, EventArgs e)
{
if (this.txtMeasure.Visible)//txtMeasure是用来显示量测结果的控件
{
this.txtMeasure.Visible = false;
this.coords.Clear();
this.map1.MapGraphicLayers.Remove(this.measureLayer);
this._mapAction = MyMapAction.None;
}
else
{
this.txtMeasure.Text = "量测:\r\n长度:0.000 米\r\n面积:0.000 平方米";
this.measureLayer = new MeasureLayer();
this.measureLayer.Coordinates.Clear();
this.map1.MapGraphicLayers.Add(this.measureLayer);
this._mapAction == MyMapAction.Measure;
this.txtMeasure.Visible = true;
}
} private void map1_MouseUp(object sender, MapMouseEventArgs e)
{
if (this._mapAction == MyMapAction.Measure)
{
Coordinate coord = this.map1.ToMap(e.X, e.Y);
this.coords.Add(coord);
this.measureLayer.SetCoordinate(this.coords);
Polyline line = new Polyline(this.measureLayer.Coordinates);
Polygon area = new Polygon(this.measureLayer.Coordinates);
this.txtMeasure.Text = String.Format("量测:\r\n长度:{0} 米\r\n面积:{1} 平方米",
line.GetLength().ToString("f3"), area.GetArea().ToString("f3"));
this.map1.Refresh();
}
}

  经过测试,这种方式是可行的。之后大概是因为无聊,也可能是为了验证自己的想法,又分别测试了完全反向插入节点和完全正向插入节点的情况,结果出乎意料,竟然都是正确的???

  完全反向插入节点:即将[1,2,3,4]以[1,4,3,2]的顺序插入,无论节点顺序是顺时针还是逆时针。测试代码如下:

public void SetCoordinate(System.Collections.Generic.List<Coordinate> coords)
{
if (coords == null || coords.Count < 1)
return;
if (this.coordinateCollection == null)
this.coordinateCollection = new CoordinateCollection();
this.coordinateCollection.Clear();
//反向插入节点
this.coordinateCollection.Add(new Coordinate(coords[0].X, coords[0].Y));
for (int i = coords.Count - 1; i > 0; i--)
{
this.coordinateCollection.Add(new Coordinate(coords[i].X, coords[i].Y));
}
}

  完全正向插入节点:即将[1,2,3,4]以[1,2,3,4]的顺序插入,无论节点顺序是顺时针还是逆时针。测试代码如下:

public void SetCoordinate(System.Collections.Generic.List<Coordinate> coords)
{
if (coords == null || coords.Count < 1)
return;
if (this.coordinateCollection == null)
this.coordinateCollection = new CoordinateCollection();
this.coordinateCollection.Clear();
for (int i = 0; i < coords.Count; i++)
{
this.coordinateCollection.Add(new Coordinate(coords[i].X, coords[i].Y));
}
}

  最后的事实证明,只要每次绘图时,将所有的节点重新添加一次即可,于是LZ最终选择了完全正向插入节点的方式。

ArcMobile的CoordinateCollection在逆时针添加点时自动调整节点顺序的问题的更多相关文章

  1. Android Tips: 在给drawable中添加图片资源时,文件名必须全小写

    在给drawable中添加图片资源时,文件名必须全小写

  2. 使用mx:Repeater在删除和添加item时列表闪烁

    使用mx:Repeater在删除和添加item时列表闪烁 不可能在用户界面上闪闪的吧,recycleChildren属性可帮助我们 recycleChildren属性==缓存,设为true就可以了 本 ...

  3. servers中添加server时,看不到运行环境的选择。

    servers中添加server时,看不到运行环境的选择. 主要原因是tomcat目录中的配置文件格式不对.

  4. phpcmsv9如何实现添加栏目时不在首页内容区显示只在导航栏显示

    之前王晟璟一直使用PHPCMSV9系统建过自己的个人门户网站,同时也建立了一个其他类型的网站,感觉非常不错,我不得不说PHPCMSV9的功能非常齐全,非常强大. 但有一点时常让王晟璟感到很烦脑,那就是 ...

  5. 30.怎样在Swift中添加运行时属性?

    和OC一样,Swift中也可以添加运行时属性.下面将提供一个完整的例子,演示如何给按钮点击事件添加运行时属性. 1.示例 import UIKit var s_GofButtonTouchDownKe ...

  6. oracle添加数据时主键自动增长

    CREATE TABLE STUDENT( --创建学生表  ID NUMBER(10) PRIMARY KEY,   --主键ID  SNAME VARCHAR2(20), ); 此时给学生表添加数 ...

  7. mybatis添加记录时返回主键id

    参考:mybatis添加记录时返回主键id 场景 有些时候我们在添加记录成功后希望能直接获取到该记录的主键id值,而不需要再执行一次查询操作.在使用mybatis作为ORM组件时,可以很方便地达到这个 ...

  8. 使用mybatis注解@Options实现添加记录时返回主键值

    官网:http://www.mybatis.org/mybatis-3/index.html 在使用mybatis作为ORM框架时,我通常更喜欢使用注解而非xml配置文件的方式.业务场景:添加记录之后 ...

  9. cmd中mysql主键id自增,在添加信息时发生错误,再次成功添加时,id已经跳过错误的信息继续自增。

    id 自增,在往这个表里添加信息时 发生错误,再次添加 id数值已经跳过之前

随机推荐

  1. kafka安装流程

    本文是作者原创,版权归作者所有.若要转载,请注明出处. 安装前的环境准备 1.由于Kafka是用Scala语言开发的,运行在JVM上,在安装之前需要先安装JDK(省略) 2.kafka依赖zookee ...

  2. B树的进化版----B+树

    C++为什么叫C plus plus?这是由于C++相当于继承C的语法后,增加了各方面的能力,所扩展出的一种新语法.在软件领域中 plus 有增加的味道.在这里B +树也一样,是B树的增强版.在学习B ...

  3. Pytorch 中张量的理解

    张量是一棵树 长久以来,张量和其中维度的概念把我搞的晕头转向. 一维的张量是数组,二维的张量是矩阵,这也很有道理. 但是给一个二维张量,让我算出它每一行的和,应该用 sum(dim=0) 还是 sum ...

  4. mongodb简单运用

    mongodb NoSQL(Not Only SQL),意思是"不仅仅是 SQL",指的是非关系型数据库,是对不同于传统的关系型数据库的数据库管理系统的统称. NoSQL 用于超大 ...

  5. 前端知识(一)04 Vue.js入门-谷粒学院

    目录 一.介绍 1.Vue.js 是什么 2.初识Vue.js 二.基本语法 1.基本数据渲染和指令 2.双向数据绑定 3.事件 4.修饰符 5.条件渲染 6.列表渲染 7.实例生命周期 一.介绍 1 ...

  6. 转 Fiddler2 下断点修改HTTP报文

    文章转自:https://www.cnblogs.com/zhengna/p/10861893.html 一 Fiddler中设置断点修改HTTP请求 方法1:全局断点.Rules-->Auto ...

  7. Vue整合swiper报错Could not compile template .....swiper\dist\css\swiper.css解决办法

    问题描述 今天做一个前端项目,安装幻灯片插件vue-awesome-swiper后 运行npm run dev 后报错如下: `ERROR Could not compile template E:\ ...

  8. RabbitMq消费者在初始配置之后进行数据消费

    RabbitMq消费者在初始配置之后进行数据消费 问题背景 在写一个消费rabbitmq消息的程序是,发现了一个问题,消费者的业务逻辑里面依赖这一些配置信息,但是当项目启动时,如果队列里面有积压数据的 ...

  9. Linux网络数据包的揭秘以及常见的调优方式总结

    https://mp.weixin.qq.com/s/boRWlx1R7TX0NLuI2sZBfQ 作为业务 SRE,我们所运维的业务,常常以 Linux+TCP/UDP daemon 的形式对外提供 ...

  10. LOJ2632

    题目描述 译自 BalticOI 2011 Day1 T3「Switch the Lamp On」有一种正方形的电路元件,在它的两组相对顶点中,有一组会用导线连接起来,另一组则不会.有  个这样的元件 ...