基本需求:地图绘制多边形,获取多边形的extent并传给后台查询出范围内的数据后,在去前端遍历判断是否在多边形内。
前端获取范围并传入extentObj代码如下
on(toolBar, "draw-complete", function (result) {
//关闭绘图工具
toolBar.deactivate();
//获得绘图得到的面
var geometry=result.geometry;
var symbol;
symbol = new SimpleFillSymbol();
graphicDraw = new Graphic(geometry, symbol);
console.log("ehehhe"+geometry.contains(new Point(103.78, 36.05)));
//extendObj范围
geometry = webMercatorUtils.webMercatorToGeographic(geometry)
var extent =geometry.getExtent();
extentObj={
"Xmin":extent.xmin,
"Ymin":extent.ymin,
"Xmax":extent.xmax,
"Ymax":extent.ymax
}
QueryincludeExtentData(extentObj,geometry);
console.log("Xmin:"+extentObj.Xmin+" Ymin:"+extentObj.Ymin+" Xmax:"+extentObj.Xmax+" Ymax:"+extentObj.Ymax)
map.graphics.add(graphicDraw);//框选结束后在地图上保留框选范围
});
/**
*
* @param extentObj 画出多边形后,获取其范围矩形传入后台
* @param geometry 多边形面传入后用于判断点是否在多边形内
* @constructor
*/
function QueryincludeExtentData(extentObj,geometry) {
var gridDataUrl = "/move/spatialQuery";
$.getJSON(gridDataUrl,extentObj,function(data){//根据矩形范围查出初步值后判断这些值
var gridData = data.gridData;
for(var i = 0;i<gridData.length;i++){
var point = new Point(gridData[i].grid_lng,gridData[i].grid_lat);
if(geometry.contains(point)){
var simpleMarkerSymbol = new SimpleMarkerSymbol();
simpleMarkerSymbol.setColor(new Color([255,0,0,0.8]));
simpleMarkerSymbol.setSize(10);
drawResultPoint = new Graphic(point, simpleMarkerSymbol);
pointGraphicLayer.add(drawResultPoint);
}
}
});
}
后台处理代码如下:
@ResponseBody
@RequestMapping("/spatialQuery")
public HxJsonObject spatialQuery(@RequestParam String Xmin, @RequestParam String Ymin,@RequestParam String Xmax, @RequestParam String Ymax){
System.out.println("Xmin"+Xmin);
//TODO
return null;
}