您的位置:首页 > 移动开发 > Android开发

Android Arcgis入门(七)、利用GeometryEngine坐标转换、计算距离与面积等

2017-05-06 15:37 603 查看
GeometryEngine是Arcgis的重要工具类,利用此工具类,可以计算地图上的距离、面积,将点、线、面转化为Json数据,将Json转化为点线面,坐标转换作用非常强大。

一、坐标转化

将用到方法 GeometryEngine.project(Geometry geometry, SpatialReference inputSR, SpatialReference outputSR),第二个为Geometry的坐标,第三个参数为要转换的坐标。如果将84坐标转换为墨卡托坐标代码如下:

Point point1 = new Point(113,23);
Point point2 =(Point)GeometryEngine.project(point1,SpatialReference.create(SpatialReference.WKID_WGS84), SpatialReference.create(SpatialReference.WKID_WGS84_WEB_MERCATOR));


二、GeometryEngine计算两点的距离

用到 GeometryEngine.geodesicDistance(Geometry geometry1, Geometry geometry2, SpatialReference spatialReference, LinearUnit distanceUnit)方法。注意最好不要用GeometryEngine.distance方法,此方法是计算2D长度的,计算不准确。代码如下:

Point point1 = new Point(113,23);
Point point2 = new Point(113,24);
double distance = GeometryEngine.geodesicDistance(point3,point4, SpatialReference.create(SpatialReference.WKID_WGS84), new LinearUnit(LinearUnit.Code.KILOMETER));
Log.i("TAG","distance ==="+distance );


将上面的代码运行,发现distance为110.75107527319621,我们带入的单位是km也就是110km,而1纬度相差110KM左右。计算还是比较准确的。

三、GeometryEngine计算面积

用到的方法是 GeometryEngine.geodesicArea(Geometry geometry, SpatialReference spatialReference, AreaUnit areaUnit),做个测试的代码。

Polygon polygon = new Polygon();
polygon.startPath(new Point(110,13));
polygon.lineTo(new Point(115,13));
polygon.lineTo(new Point(115,23));
double area = GeometryEngine.geodesicArea(polygon,SpatialReference.create(SpatialReference.WKID_WGS84),new AreaUnit(AreaUnit.Code.SQUARE_METER));//单位为平方米


四、计算周长

如果我们要计算一条线或一个面的周长是,可以用到这个方法 GeometryEngine.geodesicLength(Geometry geometry, SpatialReference spatialReference, LinearUnit lengthUnit),直接上代码:

Polyline polyline = new Polyline();
polyline.startPath(new Point(110,13));
polyline.lineTo(new Point(115,13));
polyline.lineTo(new Point(115,23));
double length = GeometryEngine.geodesicLength(polyline,SpatialReference.create(SpatialReference.WKID_WGS84),new LinearUnit(LinearUnit.Code.METER));


另外,GeometryEngine还有许多的方法都非常实用,这里就不一一讲解了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: