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

Android GIS开发系列-- 入门季(14)FeatureLayer之范围查询

2017-10-29 23:54 495 查看
AndroidGIS开发系列--入门季(5),这篇文章中,我们知道如何去查找要素。现在有一个需求,查找某点5000米范围的要素,那如何来做呢?首先我们需要在地图上画个5000米半径的圆,然后根据QueryParameters来查询相关要素。具体如下:

一、画个5000米半径的圆

1.确定中心点:centerPoint2.半径为50003.将一个圆分为120个点(当然可以细分更多),比如第一个点角度为0,求出sin与cos值,再分别求出经度与纬度:
纬度=中心点的纬度+半径*cos值*每米的纬度偏移量
经度=中心点的经度+半径*sin值*每米的经度偏移量

相关代码如下:
/纬度每米偏移量(存在误差)
publicstaticfinaldoubleONE_METER_OFFSET=0.00000899322;
//计算当前纬度时经度的偏移量
publicstaticdoublecalcLongitudeOffset(doublelatitude){
returnONE_METER_OFFSET/Math.cos(latitude*Math.PI/180.0f);
}
//获取一个圆
privatePolygonaddGraphicCricle(PointcenterPoint,doublemeter){
List<Point>points=newArrayList<Point>();
doublesin;
doublecos;
doublelon;
doublelat;
for(doublei=0;i<120;i++){
//sin值
sin=Math.sin(Math.PI*2*i/120);
//cos值
cos=Math.cos(Math.PI*2*i/120);
//纬度=中心点的纬度+半径*cos值*每米的纬度偏移量
lat=centerPoint.getY()+meter*cos*ONE_METER_OFFSET;
//经度=中心点的经度+半径*sin值*每米的经度偏移量
lon=centerPoint.getX()+meter*sin*calcLongitudeOffset(lat);
Pointp=newPoint(lon,lat);
points.add(p);
}
Polygonpolygon=newPolygon();

for(inti=0;i<points.size();i++){
if(i==0){
polygon.startPath(points.get(i));
}else{
polygon.lineTo(points.get(i));
}
}

returnpolygon;
}
ViewCode
将上面的Polygon添加到GraphicsLayer中:
SimpleFillSymbolfillSymbol=newSimpleFillSymbol(Color.parseColor("#88ff0000"));
fillSymbol.setOutline(newSimpleLineSymbol(Color.TRANSPARENT,0));
Graphicg=newGraphic(polygon,fillSymbol);
mLayer.addGraphic(g);
效果如下:[/code]分别计算polygon的面积与长度:
doublearea=GeometryEngine.geodesicArea(polygon,SpatialReference.create(SpatialReference.WKID_WGS84),newAreaUnit(AreaUnit.Code.SQUARE_METER));
Log.e("huang","area==="+area);
doublelength=GeometryEngine.geodesicLength(polygon,SpatialReference.create(SpatialReference.WKID_WGS84),newLinearUnit(LinearUnit.Code.METER));
Log.e("huang","area==="+length);
结果值为与实际计算值有偏差,但偏差不是很大。[/code]
//面积:78128662.10079278
//长度:31343.779712156855
[/code]

二、通过圆polygon来查询FeatureLayer在此范围内的要素

privatevoidqueryFeature(Geometrygeometry){
try{
QueryParametersargs=newQueryParameters();
args.setReturnGeometry(true);//是否返回Geometry
args.setGeometry(geometry);//查询范围面
args.setInSpatialReference(SpatialReference
.create(SpatialReference.WKID_WGS84));
args.setSpatialRelationship(SpatialRelationship.WITHIN);
//获取查询结果result
Future<FeatureResult>result=featureLayer.getFeatureTable()
.queryFeatures(args,null);

}catch(Exceptione){
e.printStackTrace();
}

}

三、思考

其实FeatureLayer的getFeatureIDs(floatx,floaty,inttolerance)与getFeatureIDs(floatx,floaty,inttolerance,intnumberOfResults)、GraphicsLayer中的getGraphicIDs(floatx,floaty,inttolerance)与getGraphicIDs(floatx,floaty,inttolerance,intnumberOfResults)这几个查询方法也是通过范围去查询,只不过x,y是屏幕坐标,相当于中心坐标,而tolerance相当于半径,范围的大小由tolerance决定。那5dp的查找范围相当于多少范围呢?可以获取当前地图的比例尺*查找半径获取到。



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: