ArcGIS API for Flex 扩展 - 获取Polygon的中心点坐标

ArcGIS API for Flex主要构建于ArcGIS Server REST API之上,客户端的功能比较有限,很多高级功能都通过Geometry Service 和 Geoprocessing Service实现。但是,有时候一些简单的功能,比如获取Polygon的中心点,在这个点上弹出一个自定义的窗口之类的,如果都通过服务来交互,可能用户体验就会变差。所以,你可以自己编写一些代码,在客户端实现这些功能。

这是我写的从一个Polygon获取中心点坐标的代码:
/**
* Author:Wu Yongfeng
* MSN/Email:[email protected]
*/
package wuyf
{
import com.esri.ags.geometry.Extent;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.geometry.Polygon;

public class GeometryUtil
{
/**
* 获取多边形的重心
* @param polygon 多边形
* @return 重心点
*
*/
static public function getGravityCenter(polygon polygon):MapPoint
{
var ext:Extent = polygon.extent;
var p0:MapPoint = new MapPoint(ext.xmin, ext.ymin);

var momentX:Number = 0;
var momentY:Number = 0;
var weight:Number = 0;
for (var i:int=0; i {
var pts:Array = polygon.rings as Array;
for ( var j:int=0; j {
var p1:MapPoint = polygon.getPoint(i, j);
var p2:MapPoint;
if( j==pts.length-1 )
{
p2 = polygon.getPoint(i, 0);
}
else
{
p2 = polygon.getPoint(i, j+1);
}

var dWeight:Number = (p1.x-p0.x)*(p2.y-p1.y)
- (p1.x-p0.x)*(p0.y-p1.y)/2
- (p2.x-p0.x)*(p2.y-p0.y)/2
- (p1.x-p2.x)*(p2.y-p1.y)/2;
weight += dWeight;

var pTmp:MapPoint = new MapPoint((p1.x+p2.x)/2, (p1.y+p2.y)/2);
var gravityX:Number = p0.x + (pTmp.x-p0.x)*2/3;
var gravityY:Number = p0.y + (pTmp.y-p0.y)*2/3;
momentX += gravityX*dWeight;
momentY += gravityY*dWeight;
}
}

return new MapPoint(momentX/weight, momentY/weight);
}

}
}


看看效果:

你可能感兴趣的:(api,function,Flex,service,扩展,import)